1// Copyright (C) MongoDB, Inc. 2017-present. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); you may 4// not use this file except in compliance with the License. You may obtain 5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 6 7package readpref // import "go.mongodb.org/mongo-driver/mongo/readpref" 8 9import ( 10 "errors" 11 "time" 12 13 "go.mongodb.org/mongo-driver/tag" 14) 15 16var ( 17 errInvalidReadPreference = errors.New("can not specify tags or max staleness on primary") 18) 19 20var primary = ReadPref{mode: PrimaryMode} 21 22// Primary constructs a read preference with a PrimaryMode. 23func Primary() *ReadPref { 24 return &primary 25} 26 27// PrimaryPreferred constructs a read preference with a PrimaryPreferredMode. 28func PrimaryPreferred(opts ...Option) *ReadPref { 29 // New only returns an error with a mode of Primary 30 rp, _ := New(PrimaryPreferredMode, opts...) 31 return rp 32} 33 34// SecondaryPreferred constructs a read preference with a SecondaryPreferredMode. 35func SecondaryPreferred(opts ...Option) *ReadPref { 36 // New only returns an error with a mode of Primary 37 rp, _ := New(SecondaryPreferredMode, opts...) 38 return rp 39} 40 41// Secondary constructs a read preference with a SecondaryMode. 42func Secondary(opts ...Option) *ReadPref { 43 // New only returns an error with a mode of Primary 44 rp, _ := New(SecondaryMode, opts...) 45 return rp 46} 47 48// Nearest constructs a read preference with a NearestMode. 49func Nearest(opts ...Option) *ReadPref { 50 // New only returns an error with a mode of Primary 51 rp, _ := New(NearestMode, opts...) 52 return rp 53} 54 55// New creates a new ReadPref. 56func New(mode Mode, opts ...Option) (*ReadPref, error) { 57 rp := &ReadPref{ 58 mode: mode, 59 } 60 61 if mode == PrimaryMode && len(opts) != 0 { 62 return nil, errInvalidReadPreference 63 } 64 65 for _, opt := range opts { 66 err := opt(rp) 67 if err != nil { 68 return nil, err 69 } 70 } 71 72 return rp, nil 73} 74 75// ReadPref determines which servers are considered suitable for read operations. 76type ReadPref struct { 77 maxStaleness time.Duration 78 maxStalenessSet bool 79 mode Mode 80 tagSets []tag.Set 81} 82 83// MaxStaleness is the maximum amount of time to allow 84// a server to be considered eligible for selection. The 85// second return value indicates if this value has been set. 86func (r *ReadPref) MaxStaleness() (time.Duration, bool) { 87 return r.maxStaleness, r.maxStalenessSet 88} 89 90// Mode indicates the mode of the read preference. 91func (r *ReadPref) Mode() Mode { 92 return r.mode 93} 94 95// TagSets are multiple tag sets indicating 96// which servers should be considered. 97func (r *ReadPref) TagSets() []tag.Set { 98 return r.tagSets 99} 100