...

Package description

import "go.mongodb.org/mongo-driver/mongo/description"
Overview
Index

Overview ▾

Package description contains types and functions for describing the state of MongoDB clusters.

Index ▾

Constants
type SelectedServer
type Server
    func NewDefaultServer(addr address.Address) Server
    func NewServer(addr address.Address, response bson.Raw) Server
    func NewServerFromError(addr address.Address, err error, tv *TopologyVersion) Server
    func (s Server) DataBearing() bool
    func (s Server) Equal(other Server) bool
    func (s Server) LoadBalanced() bool
    func (s Server) SetAverageRTT(rtt time.Duration) Server
    func (s Server) String() string
type ServerKind
    func (kind ServerKind) String() string
type ServerSelector
    func CompositeSelector(selectors []ServerSelector) ServerSelector
    func LatencySelector(latency time.Duration) ServerSelector
    func OutputAggregateSelector(rp *readpref.ReadPref) ServerSelector
    func ReadPrefSelector(rp *readpref.ReadPref) ServerSelector
    func WriteSelector() ServerSelector
type ServerSelectorFunc
    func (ssf ServerSelectorFunc) SelectServer(t Topology, s []Server) ([]Server, error)
type Topology
    func (t Topology) Equal(other Topology) bool
    func (t Topology) HasReadableServer(mode readpref.Mode) bool
    func (t Topology) HasWritableServer() bool
    func (t Topology) String() string
type TopologyKind
    func (kind TopologyKind) String() string
type TopologyVersion
    func NewTopologyVersion(doc bson.Raw) (*TopologyVersion, error)
    func (tv *TopologyVersion) CompareToIncoming(responseTV *TopologyVersion) int
type VersionRange
    func NewVersionRange(min, max int32) VersionRange
    func (vr *VersionRange) Equals(other *VersionRange) bool
    func (vr VersionRange) Includes(v int32) bool
    func (vr VersionRange) String() string

Package files

description.go server.go server_kind.go server_selector.go topology.go topology_kind.go topology_version.go version_range.go

Constants

Unknown is an unknown server or topology kind.

const Unknown = 0

type SelectedServer

SelectedServer augments the Server type by also including the TopologyKind of the topology that includes the server. This type should be used to track the state of a server that was selected to perform an operation.

type SelectedServer struct {
    Server
    Kind TopologyKind
}

type Server

Server contains information about a node in a cluster. This is created from hello command responses. If the value of the Kind field is LoadBalancer, only the Addr and Kind fields will be set. All other fields will be set to the zero value of the field's type.

type Server struct {
    Addr address.Address

    Arbiters          []string
    AverageRTT        time.Duration
    AverageRTTSet     bool
    Compression       []string // compression methods returned by server
    CanonicalAddr     address.Address
    ElectionID        primitive.ObjectID
    HeartbeatInterval time.Duration
    HelloOK           bool
    Hosts             []string
    IsCryptd          bool
    LastError         error
    LastUpdateTime    time.Time
    LastWriteTime     time.Time
    MaxBatchCount     uint32
    MaxDocumentSize   uint32
    MaxMessageSize    uint32
    Members           []address.Address
    Passives          []string
    Passive           bool
    Primary           address.Address
    ReadOnly          bool
    ServiceID         *primitive.ObjectID // Only set for servers that are deployed behind a load balancer.
    // Deprecated: Use SessionTimeoutMinutesPtr instead.
    SessionTimeoutMinutes    uint32
    SessionTimeoutMinutesPtr *int64
    SetName                  string
    SetVersion               uint32
    Tags                     tag.Set
    TopologyVersion          *TopologyVersion
    Kind                     ServerKind
    WireVersion              *VersionRange
}

func NewDefaultServer

func NewDefaultServer(addr address.Address) Server

NewDefaultServer creates a new unknown server description with the given address.

func NewServer

func NewServer(addr address.Address, response bson.Raw) Server

NewServer creates a new server description from the given hello command response.

func NewServerFromError

func NewServerFromError(addr address.Address, err error, tv *TopologyVersion) Server

NewServerFromError creates a new unknown server description with the given parameters.

func (Server) DataBearing

func (s Server) DataBearing() bool

DataBearing returns true if the server is a data bearing server.

func (Server) Equal

func (s Server) Equal(other Server) bool

Equal compares two server descriptions and returns true if they are equal

func (Server) LoadBalanced

func (s Server) LoadBalanced() bool

LoadBalanced returns true if the server is a load balancer or is behind a load balancer.

func (Server) SetAverageRTT

func (s Server) SetAverageRTT(rtt time.Duration) Server

SetAverageRTT sets the average round trip time for this server description.

func (Server) String

func (s Server) String() string

String implements the Stringer interface

type ServerKind

ServerKind represents the type of a single server in a topology.

type ServerKind uint32

These constants are the possible types of servers.

const (
    Standalone   ServerKind = 1
    RSMember     ServerKind = 2
    RSPrimary    ServerKind = 4 + RSMember
    RSSecondary  ServerKind = 8 + RSMember
    RSArbiter    ServerKind = 16 + RSMember
    RSGhost      ServerKind = 32 + RSMember
    Mongos       ServerKind = 256
    LoadBalancer ServerKind = 512
)

func (ServerKind) String

func (kind ServerKind) String() string

String returns a stringified version of the kind or "Unknown" if the kind is invalid.

type ServerSelector

ServerSelector is an interface implemented by types that can perform server selection given a topology description and list of candidate servers. The selector should filter the provided candidates list and return a subset that matches some criteria.

type ServerSelector interface {
    SelectServer(Topology, []Server) ([]Server, error)
}

func CompositeSelector

func CompositeSelector(selectors []ServerSelector) ServerSelector

CompositeSelector combines multiple selectors into a single selector by applying them in order to the candidates list.

For example, if the initial candidates list is [s0, s1, s2, s3] and two selectors are provided where the first matches s0 and s1 and the second matches s1 and s2, the following would occur during server selection:

1. firstSelector([s0, s1, s2, s3]) -> [s0, s1] 2. secondSelector([s0, s1]) -> [s1]

The final list of candidates returned by the composite selector would be [s1].

func LatencySelector

func LatencySelector(latency time.Duration) ServerSelector

LatencySelector creates a ServerSelector which selects servers based on their average RTT values.

func OutputAggregateSelector

func OutputAggregateSelector(rp *readpref.ReadPref) ServerSelector

OutputAggregateSelector selects servers based on the provided read preference given that the underlying operation is aggregate with an output stage.

func ReadPrefSelector

func ReadPrefSelector(rp *readpref.ReadPref) ServerSelector

ReadPrefSelector selects servers based on the provided read preference.

func WriteSelector

func WriteSelector() ServerSelector

WriteSelector selects all the writable servers.

type ServerSelectorFunc

ServerSelectorFunc is a function that can be used as a ServerSelector.

type ServerSelectorFunc func(Topology, []Server) ([]Server, error)

func (ServerSelectorFunc) SelectServer

func (ssf ServerSelectorFunc) SelectServer(t Topology, s []Server) ([]Server, error)

SelectServer implements the ServerSelector interface.

type Topology

Topology contains information about a MongoDB cluster.

type Topology struct {
    Servers []Server
    SetName string
    Kind    TopologyKind
    // Deprecated: Use SessionTimeoutMinutesPtr instead.
    SessionTimeoutMinutes    uint32
    SessionTimeoutMinutesPtr *int64
    CompatibilityErr         error
}

func (Topology) Equal

func (t Topology) Equal(other Topology) bool

Equal compares two topology descriptions and returns true if they are equal.

func (Topology) HasReadableServer

func (t Topology) HasReadableServer(mode readpref.Mode) bool

HasReadableServer returns true if the topology contains a server suitable for reading.

If the Topology's kind is Single or Sharded, the mode parameter is ignored and the function contains true if any of the servers in the Topology are of a known type.

For replica sets, the function returns true if the cluster contains a server that matches the provided read preference mode.

func (Topology) HasWritableServer

func (t Topology) HasWritableServer() bool

HasWritableServer returns true if a topology has a server available for writing.

If the Topology's kind is Single or Sharded, this function returns true if any of the servers in the Topology are of a known type.

For replica sets, the function returns true if the replica set contains a primary.

func (Topology) String

func (t Topology) String() string

String implements the Stringer interface.

type TopologyKind

TopologyKind represents a specific topology configuration.

type TopologyKind uint32

These constants are the available topology configurations.

const (
    Single                TopologyKind = 1
    ReplicaSet            TopologyKind = 2
    ReplicaSetNoPrimary   TopologyKind = 4 + ReplicaSet
    ReplicaSetWithPrimary TopologyKind = 8 + ReplicaSet
    Sharded               TopologyKind = 256
    LoadBalanced          TopologyKind = 512
)

func (TopologyKind) String

func (kind TopologyKind) String() string

String implements the fmt.Stringer interface.

type TopologyVersion

TopologyVersion represents a software version.

type TopologyVersion struct {
    ProcessID primitive.ObjectID
    Counter   int64
}

func NewTopologyVersion

func NewTopologyVersion(doc bson.Raw) (*TopologyVersion, error)

NewTopologyVersion creates a TopologyVersion based on doc

func (*TopologyVersion) CompareToIncoming

func (tv *TopologyVersion) CompareToIncoming(responseTV *TopologyVersion) int

CompareToIncoming compares the receiver, which represents the currently known TopologyVersion for a server, to an incoming TopologyVersion extracted from a server command response.

This returns -1 if the receiver version is less than the response, 0 if the versions are equal, and 1 if the receiver version is greater than the response. This comparison is not commutative.

type VersionRange

VersionRange represents a range of versions.

type VersionRange struct {
    Min int32
    Max int32
}

func NewVersionRange

func NewVersionRange(min, max int32) VersionRange

NewVersionRange creates a new VersionRange given a min and a max.

func (*VersionRange) Equals

func (vr *VersionRange) Equals(other *VersionRange) bool

Equals returns a bool indicating whether the supplied VersionRange is equal.

func (VersionRange) Includes

func (vr VersionRange) Includes(v int32) bool

Includes returns a bool indicating whether the supplied integer is included in the range.

func (VersionRange) String

func (vr VersionRange) String() string

String implements the fmt.Stringer interface.