sockets

package
v0.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 27, 2026 License: Apache-2.0 Imports: 13 Imported by: 1,801

Documentation

Overview

Package sockets provides helper functions to create and configure Unix or TCP sockets.

Package sockets provides helper functions to create and configure Unix or TCP sockets.

Package sockets is a simple unix domain socket wrapper.

Usage

For example:

import(
	"fmt"
	"net"
	"os"
	"github.com/docker/go-connections/sockets"
)

func main() {
	l, err := sockets.NewUnixSocketWithOpts("/path/to/sockets",
		sockets.WithChown(0,0),sockets.WithChmod(0660))
	if err != nil {
		panic(err)
	}
	echoStr := "hello"

	go func() {
		for {
			conn, err := l.Accept()
			if err != nil {
				return
			}
			conn.Write([]byte(echoStr))
			conn.Close()
		}
	}()

	conn, err := net.Dial("unix", path)
	if err != nil {
		t.Fatal(err)
	}

	buf := make([]byte, 5)
	if _, err := conn.Read(buf); err != nil {
		panic(err)
	} else if string(buf) != echoStr {
		panic(fmt.Errorf("msg may lost"))
	}
}

Index

Constants

This section is empty.

Variables

View Source
var ErrProtocolNotAvailable = errors.New("protocol not available")

ErrProtocolNotAvailable is returned when a given transport protocol is not provided by the operating system.

Functions

func ConfigureTransport added in v0.1.3

func ConfigureTransport(tr *http.Transport, proto, addr string) error

ConfigureTransport configures the specified http.Transport according to the specified proto and addr.

If the proto is unix (using a unix socket to communicate) or npipe the compression is disabled. For other protos, compression is enabled. If you want to manually enable/disable compression, make sure you do it _after_ any subsequent calls to ConfigureTransport is made against the same http.Transport.

func NewTCPSocket

func NewTCPSocket(addr string, tlsConfig *tls.Config) (net.Listener, error)

NewTCPSocket creates a TCP socket listener with the specified address and the specified tls configuration. If TLSConfig is set, will encapsulate the TCP listener inside a TLS one.

func NewUnixSocket

func NewUnixSocket(path string, gid int) (net.Listener, error)

NewUnixSocket creates a Unix socket with the specified path and group.

On Unix platforms, the socket is owned by root:gid and has permissions 0660.

Abstract Unix sockets are not supported by this helper. Use NewUnixSocketWithOpts without filesystem permission options instead.

func NewUnixSocketWithOpts added in v0.5.0

func NewUnixSocketWithOpts(path string, opts ...SockOption) (net.Listener, error)

NewUnixSocketWithOpts creates a Unix socket with the specified options.

On Unix platforms, socket permissions are 0000 by default, i.e. no access for anyone. Pass WithChmod() and WithChown() to set the desired permissions and ownership.

On Windows, the socket uses Windows ACLs. Pass WithBasePermissions() to allow Administrators and LocalSystem full access, or WithAdditionalUsersAndGroups() to also grant generic read and write access to additional users or groups.

Abstract Unix sockets (Go's Linux-specific "@" shorthand and the native leading-NUL representation) are supported only on Linux. On other platforms, attempts to use abstract socket addresses return an error. Because abstract sockets have no filesystem representation, filesystem-specific socket options are not supported.

On platforms without abstract Unix socket support, attempts to use abstract socket addresses return an error wrapping errors.ErrUnsupported.

Types

type InmemSocket added in v0.1.3

type InmemSocket struct {
	// contains filtered or unexported fields
}

InmemSocket implements net.Listener using in-memory only connections.

func NewInmemSocket added in v0.1.3

func NewInmemSocket(addr string, bufSize int) *InmemSocket

NewInmemSocket creates an in-memory only net.Listener. The addr argument can be any string, but is used to satisfy the net.Listener.Addr part of the net.Listener interface

func (*InmemSocket) Accept added in v0.1.3

func (s *InmemSocket) Accept() (net.Conn, error)

Accept implements the Accept method in the Listener interface; it waits for the next call and returns a generic Conn. It returns a net.ErrClosed if the connection is already closed.

func (*InmemSocket) Addr added in v0.1.3

func (s *InmemSocket) Addr() net.Addr

Addr returns the socket's addr string to satisfy net.Listener

func (*InmemSocket) Close added in v0.1.3

func (s *InmemSocket) Close() error

Close closes the listener. It will be unavailable for use once closed.

func (*InmemSocket) Dial added in v0.1.3

func (s *InmemSocket) Dial(network, addr string) (net.Conn, error)

Dial establishes a connection with the in-memory listener.

The network and addr parameters are accepted for compatibility with conventional dialer APIs but are currently ignored.

It is equivalent to calling DialContext with context.Background(). It returns net.ErrClosed if the listener has already been closed.

func (*InmemSocket) DialContext added in v0.8.0

func (s *InmemSocket) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

DialContext establishes a connection with the in-memory listener.

The network and addr parameters are accepted for compatibility with conventional dialer APIs but are currently ignored.

If ctx is canceled before the connection is established, DialContext returns the context error. It returns net.ErrClosed if the listener has already been closed.

type SockOption added in v0.5.0

type SockOption func(string) error

SockOption sets up socket file's creating option

func WithChmod added in v0.5.0

func WithChmod(mask os.FileMode) SockOption

WithChmod modifies socket file's access mode.

Abstract Unix sockets have no filesystem representation, so this option returns an error wrapping errors.ErrUnsupported when used with an abstract socket.

func WithChown added in v0.5.0

func WithChown(uid, gid int) SockOption

WithChown modifies the socket file's uid and gid.

Abstract Unix sockets have no filesystem representation, so this option returns an error wrapping errors.ErrUnsupported when used with an abstract socket.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL