readpref

package
v1.16.0-rc0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2024 License: Apache-2.0 Imports: 6 Imported by: 3,507

Documentation

Overview

Package readpref defines read preferences for MongoDB queries.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidTagSet = errors.New("an even number of tags must be specified")

ErrInvalidTagSet indicates that an invalid set of tags was specified.

Functions

This section is empty.

Types

type Mode

type Mode uint8

Mode indicates the user's preference on reads.

const (

	// PrimaryMode indicates that only a primary is
	// considered for reading. This is the default
	// mode.
	PrimaryMode Mode
	// PrimaryPreferredMode indicates that if a primary
	// is available, use it; otherwise, eligible
	// secondaries will be considered.
	PrimaryPreferredMode
	// SecondaryMode indicates that only secondaries
	// should be considered.
	SecondaryMode
	// SecondaryPreferredMode indicates that only secondaries
	// should be considered when one is available. If none
	// are available, then a primary will be considered.
	SecondaryPreferredMode
	// NearestMode indicates that all primaries and secondaries
	// will be considered.
	NearestMode
)

Mode constants

func ModeFromString

func ModeFromString(mode string) (Mode, error)

ModeFromString returns a mode corresponding to mode.

func (Mode) IsValid added in v1.5.0

func (mode Mode) IsValid() bool

IsValid checks whether the mode is valid.

func (Mode) String added in v1.4.0

func (mode Mode) String() string

String returns the string representation of mode.

type Option

type Option func(*ReadPref) error

Option configures a read preference

func WithHedgeEnabled added in v1.4.0

func WithHedgeEnabled(hedgeEnabled bool) Option

WithHedgeEnabled specifies whether or not hedged reads should be enabled in the server. This feature requires MongoDB server version 4.4 or higher. For more information about hedged reads, see https://www.mongodb.com/docs/manual/core/sharded-cluster-query-router/#mongos-hedged-reads. If not specified, the default is to not send a value to the server, which will result in the server defaults being used.

func WithMaxStaleness

func WithMaxStaleness(ms time.Duration) Option

WithMaxStaleness sets the maximum staleness a server is allowed.

func WithTagSets

func WithTagSets(tagSets ...tag.Set) Option

WithTagSets specifies a list of tag sets used to match replica set members. If the list contains multiple tag sets, members are matched against each tag set in succession until a match is found. Once a match is found, the remaining tag sets are ignored. If no members match any of the tag sets, the read operation returns with an error. To avoid an error if no members match any of the tag sets, include an empty tag set as the last tag set in the list.

The last call to WithTags or WithTagSets overrides all previous calls to either method.

For more information about read preference tags, see https://www.mongodb.com/docs/manual/core/read-preference-tags/

Example

Configure a Client with a read preference that selects the nearest replica set member matching a set of tags. Try to match members in 3 stages:

  1. Match replica set members that include tags "region: South" and "datacenter: A".
  2. Match replica set members that includes tag "region: South".
  3. Match any replica set member.

Stage 3 is used to avoid errors when no members match the previous 2 stages.

package main

import (
	"context"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"go.mongodb.org/mongo-driver/mongo/readpref"
	"go.mongodb.org/mongo-driver/tag"
)

func main() {
	tagSetList := tag.NewTagSetsFromMaps([]map[string]string{
		{"region": "South", "datacenter": "A"},
		{"region": "South"},
		{},
	})

	rp := readpref.Nearest(readpref.WithTagSets(tagSetList...))

	opts := options.Client().
		ApplyURI("mongodb://localhost").
		SetReadPreference(rp)

	_, err := mongo.Connect(context.Background(), opts)
	if err != nil {
		panic(err)
	}
}
Output:

func WithTags

func WithTags(tags ...string) Option

WithTags specifies a single tag set used to match replica set members. If no members match the tag set, read operations will return an error. To avoid errors if no members match the tag set, use WithTagSets and include an empty tag set as the last tag set in the list.

The last call to WithTags or WithTagSets overrides all previous calls to either method.

For more information about read preference tags, see https://www.mongodb.com/docs/manual/core/read-preference-tags/

Example

Configure a Client with a read preference that selects the nearest replica set member that includes tags "region: South" and "datacenter: A".

package main

import (
	"context"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"go.mongodb.org/mongo-driver/mongo/readpref"
)

func main() {
	rp := readpref.Nearest(
		readpref.WithTags(
			"region", "South",
			"datacenter", "A"))

	opts := options.Client().
		ApplyURI("mongodb://localhost:27017").
		SetReadPreference(rp)

	_, err := mongo.Connect(context.Background(), opts)
	if err != nil {
		panic(err)
	}
}
Output:

type ReadPref

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

ReadPref determines which servers are considered suitable for read operations.

func Nearest

func Nearest(opts ...Option) *ReadPref

Nearest constructs a read preference with a NearestMode.

func New

func New(mode Mode, opts ...Option) (*ReadPref, error)

New creates a new ReadPref.

func Primary

func Primary() *ReadPref

Primary constructs a read preference with a PrimaryMode.

func PrimaryPreferred

func PrimaryPreferred(opts ...Option) *ReadPref

PrimaryPreferred constructs a read preference with a PrimaryPreferredMode.

func Secondary

func Secondary(opts ...Option) *ReadPref

Secondary constructs a read preference with a SecondaryMode.

func SecondaryPreferred

func SecondaryPreferred(opts ...Option) *ReadPref

SecondaryPreferred constructs a read preference with a SecondaryPreferredMode.

func (*ReadPref) HedgeEnabled added in v1.4.0

func (r *ReadPref) HedgeEnabled() *bool

HedgeEnabled returns whether or not hedged reads are enabled for this read preference. If this option was not specified during read preference construction, nil is returned.

func (*ReadPref) MaxStaleness

func (r *ReadPref) MaxStaleness() (time.Duration, bool)

MaxStaleness is the maximum amount of time to allow a server to be considered eligible for selection. The second return value indicates if this value has been set.

func (*ReadPref) Mode

func (r *ReadPref) Mode() Mode

Mode indicates the mode of the read preference.

func (*ReadPref) String added in v1.4.0

func (r *ReadPref) String() string

String returns a human-readable description of the read preference.

func (*ReadPref) TagSets

func (r *ReadPref) TagSets() []tag.Set

TagSets are multiple tag sets indicating which servers should be considered.

Jump to

Keyboard shortcuts

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