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 ¶
- Variables
- func ConfigureTransport(tr *http.Transport, proto, addr string) error
- func NewTCPSocket(addr string, tlsConfig *tls.Config) (net.Listener, error)
- func NewUnixSocket(path string, gid int) (net.Listener, error)
- func NewUnixSocketWithOpts(path string, opts ...SockOption) (net.Listener, error)
- type InmemSocket
- type SockOption
Constants ¶
This section is empty.
Variables ¶
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
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 ¶
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 ¶
NewUnixSocket creates a unix socket with the specified path and group.
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. By default, socket permissions are 0000 (i.e.: no access for anyone); pass WithChmod() and WithChown() to set the desired ownership and permissions.
This function temporarily changes the system's "umask" to 0777 to work around a race condition between creating the socket and setting its permissions. While this should only be for a short duration, it may affect other processes that create files/directories during that period.
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 is used to establish a connection with the in-mem server. It returns a net.ErrClosed if the connection is already closed.
type SockOption ¶ added in v0.5.0
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.
func WithChown ¶ added in v0.5.0
func WithChown(uid, gid int) SockOption
WithChown modifies the socket file's uid and gid