Documentation ¶
Overview ¶
Package connection contains the types for building and pooling connections that can speak the MongoDB Wire Protocol. Since this low level library is meant to be used in the context of either a driver or a server there are some extra identifiers on a connection so one can keep track of what a connection is. This package purposefully hides the underlying network and abstracts the writing to and reading from a connection to wireops.Op's. This package also provides types for listening for and accepting Connections, as well as some types for handling connections and proxying connections to another server.
Index ¶
- Variables
- type Addr
- type Connection
- type Dialer
- type DialerFunc
- type Error
- type Handler
- type Handshaker
- type HandshakerFunc
- type Listener
- type NetworkError
- type Option
- func WithAppName(fn func(string) string) Option
- func WithCompressors(fn func([]compressor.Compressor) []compressor.Compressor) Option
- func WithConnectTimeout(fn func(time.Duration) time.Duration) Option
- func WithDialer(fn func(Dialer) Dialer) Option
- func WithHandshaker(fn func(Handshaker) Handshaker) Option
- func WithIdleTimeout(fn func(time.Duration) time.Duration) Option
- func WithLifeTimeout(fn func(time.Duration) time.Duration) Option
- func WithReadTimeout(fn func(time.Duration) time.Duration) Option
- func WithTLSConfig(fn func(*TLSConfig) *TLSConfig) Option
- func WithWriteTimeout(fn func(time.Duration) time.Duration) Option
- type Pool
- type PoolError
- type Proxy
- type Server
- type TLSConfig
Constants ¶
This section is empty.
Variables ¶
var ErrConnectionClosed = Error{ConnectionID: "<closed>", /* contains filtered or unexported fields */}
ErrConnectionClosed is returned from an attempt to use an already closed connection.
var ErrPoolClosed = PoolError("pool is closed")
ErrPoolClosed is returned from an attempt to use a closed pool.
var ErrPoolConnected = PoolError("pool is connected")
ErrPoolConnected is returned from an attempt to connect an already connected pool
var ErrPoolDisconnected = PoolError("pool is disconnected or disconnecting")
ErrPoolDisconnected is returned from an attempt to disconnect an already disconnected or disconnecting pool.
var ErrSizeLargerThanCapacity = PoolError("size is larger than capacity")
ErrSizeLargerThanCapacity is returned from an attempt to create a pool with a size larger than the capacity.
Functions ¶
This section is empty.
Types ¶
type Addr ¶
type Addr string
Addr is a network address. It can be either an IP address or a DNS name.
func (Addr) Canonicalize ¶
Canonicalize creates a canonicalized address.
type Connection ¶
type Connection interface { WriteWireMessage(context.Context, wiremessage.WireMessage) error ReadWireMessage(context.Context) (wiremessage.WireMessage, error) Close() error Expired() bool Alive() bool ID() string }
Connection is used to read and write wire protocol messages to a network.
type Dialer ¶
type Dialer interface {
DialContext(ctx context.Context, network, address string) (net.Conn, error)
}
Dialer is used to make network connections.
DefaultDialer is the Dialer implementation that is used by this package. Changing this will also change the Dialer used for this package. This should only be changed why all of the connections being made need to use a different Dialer. Most of the time, using a WithDialer option is more appropriate than changing this variable.
type DialerFunc ¶
DialerFunc is a type implemented by functions that can be used as a Dialer.
func (DialerFunc) DialContext ¶
DialContext implements the Dialer interface.
type Handler ¶
type Handler interface {
HandleConnection(Connection)
}
Handler handles an individual Connection. Returning signals that the Connection is no longer needed and can be closed.
type Handshaker ¶
type Handshaker interface {
Handshake(context.Context, address.Address, wiremessage.ReadWriter) (description.Server, error)
}
Handshaker is the interface implemented by types that can perform a MongoDB handshake over a provided ReadWriter. This is used during connection initialization.
type HandshakerFunc ¶
type HandshakerFunc func(context.Context, address.Address, wiremessage.ReadWriter) (description.Server, error)
HandshakerFunc is an adapter to allow the use of ordinary functions as connection handshakers.
func (HandshakerFunc) Handshake ¶
func (hf HandshakerFunc) Handshake(ctx context.Context, addr address.Address, rw wiremessage.ReadWriter) (description.Server, error)
Handshake implements the Handshaker interface.
type Listener ¶
type Listener interface { // Accept waits for and returns the next Connection to the listener. Accept() (Connection, error) // Close closes the listener. Close() error // Addr returns the listener's network address. Addr() Addr }
Listener is a generic mongodb network protocol listener. It can return connections that speak the mongodb wire protocol.
Multiple goroutines may invoke methods on a Listener simultaneously.
TODO(GODRIVER-270): Implement this.
type NetworkError ¶
NetworkError represents an error that ocurred while reading from or writing to a network socket.
func (NetworkError) Error ¶
func (ne NetworkError) Error() string
type Option ¶
type Option func(*config) error
Option is used to configure a connection.
func WithAppName ¶
WithAppName sets the application name which gets sent to MongoDB when it first connects.
func WithCompressors ¶ added in v0.0.8
func WithCompressors(fn func([]compressor.Compressor) []compressor.Compressor) Option
WithCompressors sets the compressors that can be used for communication.
func WithConnectTimeout ¶
WithConnectTimeout configures the maximum amount of time a dial will wait for a connect to complete. The default is 30 seconds.
func WithDialer ¶
WithDialer configures the Dialer to use when making a new connection to MongoDB.
func WithHandshaker ¶
func WithHandshaker(fn func(Handshaker) Handshaker) Option
WithHandshaker configures the Handshaker that wll be used to initialize newly dialed connections.
func WithIdleTimeout ¶
WithIdleTimeout configures the maximum idle time to allow for a connection.
func WithLifeTimeout ¶
WithLifeTimeout configures the maximum life of a connection.
func WithReadTimeout ¶
WithReadTimeout configures the maximum read time for a connection.
func WithTLSConfig ¶
WithTLSConfig configures the TLS options for a connection.
type Pool ¶
type Pool interface { // Get must return a nil *description.Server if the returned connection is // not a newly dialed connection. Get(context.Context) (Connection, *description.Server, error) // Connect handles the initialization of a Pool and allow Connections to be // retrieved and pooled. Implementations must return an error if Connect is // called more than once before calling Disconnect. Connect(context.Context) error // Disconnect closest connections managed by this Pool. Implementations must // either wait until all of the connections in use have been returned and // closed or the context expires before returning. If the context expires // via cancellation, deadline, timeout, or some other manner, implementations // must close the in use connections. If this method returns with no errors, // all connections managed by this pool must be closed. Calling Disconnect // multiple times after a single Connect call must result in an error. Disconnect(context.Context) error Drain() error }
Pool is used to pool Connections to a server.
type Proxy ¶
type Proxy struct { Processor wiremessage.Transformer Pool Pool }
Proxy implements a MongoDB proxy. It will use the given pool to connect to a MongoDB server and proxy the traffic between connections it is given and the server. It will pass each of the wireops it reads from the handled connection to a Processor. If an error is returned from the processor, the wireop will not be forwarded onto the server. If there is not an error the returned message will be passed onto the server. If both the return message and the error are nil, the original wiremessage will be passed onto the server.
TODO(GODRIVER-268): Implement this.
func (*Proxy) HandleConnection ¶
func (*Proxy) HandleConnection(Connection)
HandleConnection implements the Handler interface.
type Server ¶
Server is used to handle incoming Connections. It handles the boilerplate of accepting a Connection and cleaning it up after running a Handler. This also makes it easier to build higher level processors, like proxies, by handling the life cycle of the underlying connection.
TODO(GODRIVER-269): Implement this.
func (*Server) ListenAndServe ¶
ListenAndServe listens on the network address srv.Addr and calls Serve to handle requests on incoming connections. If srv.Addr is blank, "localhost:27017" is used.
type TLSConfig ¶
TLSConfig contains options for configuring a TLS connection to the server.
func (*TLSConfig) AddCACertFromFile ¶
AddCACertFromFile adds a root CA certificate to the configuration given a path to the containing file.
func (*TLSConfig) AddClientCertFromFile ¶
AddClientCertFromFile adds a client certificate to the configuration given a path to the containing file and returns the certificate's subject name.
func (*TLSConfig) Clone ¶
Clone returns a shallow clone of c. It is safe to clone a Config that is being used concurrently by a TLS client or server.
func (*TLSConfig) SetClientCertDecryptPassword ¶
SetClientCertDecryptPassword sets a function to retrieve the decryption password necessary to read a certificate. This is a function instead of a string to provide greater flexibility when deciding how to retrieve and store the password.
func (*TLSConfig) SetInsecure ¶
SetInsecure sets whether the client should verify the server's certificate chain and hostnames.