You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

readpref.go 2.6 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. package readpref // import "go.mongodb.org/mongo-driver/mongo/readpref"
  7. import (
  8. "errors"
  9. "time"
  10. "go.mongodb.org/mongo-driver/tag"
  11. )
  12. var (
  13. errInvalidReadPreference = errors.New("can not specify tags or max staleness on primary")
  14. )
  15. var primary = ReadPref{mode: PrimaryMode}
  16. // Primary constructs a read preference with a PrimaryMode.
  17. func Primary() *ReadPref {
  18. return &primary
  19. }
  20. // PrimaryPreferred constructs a read preference with a PrimaryPreferredMode.
  21. func PrimaryPreferred(opts ...Option) *ReadPref {
  22. // New only returns an error with a mode of Primary
  23. rp, _ := New(PrimaryPreferredMode, opts...)
  24. return rp
  25. }
  26. // SecondaryPreferred constructs a read preference with a SecondaryPreferredMode.
  27. func SecondaryPreferred(opts ...Option) *ReadPref {
  28. // New only returns an error with a mode of Primary
  29. rp, _ := New(SecondaryPreferredMode, opts...)
  30. return rp
  31. }
  32. // Secondary constructs a read preference with a SecondaryMode.
  33. func Secondary(opts ...Option) *ReadPref {
  34. // New only returns an error with a mode of Primary
  35. rp, _ := New(SecondaryMode, opts...)
  36. return rp
  37. }
  38. // Nearest constructs a read preference with a NearestMode.
  39. func Nearest(opts ...Option) *ReadPref {
  40. // New only returns an error with a mode of Primary
  41. rp, _ := New(NearestMode, opts...)
  42. return rp
  43. }
  44. // New creates a new ReadPref.
  45. func New(mode Mode, opts ...Option) (*ReadPref, error) {
  46. rp := &ReadPref{
  47. mode: mode,
  48. }
  49. if mode == PrimaryMode && len(opts) != 0 {
  50. return nil, errInvalidReadPreference
  51. }
  52. for _, opt := range opts {
  53. err := opt(rp)
  54. if err != nil {
  55. return nil, err
  56. }
  57. }
  58. return rp, nil
  59. }
  60. // ReadPref determines which servers are considered suitable for read operations.
  61. type ReadPref struct {
  62. maxStaleness time.Duration
  63. maxStalenessSet bool
  64. mode Mode
  65. tagSets []tag.Set
  66. }
  67. // MaxStaleness is the maximum amount of time to allow
  68. // a server to be considered eligible for selection. The
  69. // second return value indicates if this value has been set.
  70. func (r *ReadPref) MaxStaleness() (time.Duration, bool) {
  71. return r.maxStaleness, r.maxStalenessSet
  72. }
  73. // Mode indicates the mode of the read preference.
  74. func (r *ReadPref) Mode() Mode {
  75. return r.mode
  76. }
  77. // TagSets are multiple tag sets indicating
  78. // which servers should be considered.
  79. func (r *ReadPref) TagSets() []tag.Set {
  80. return r.tagSets
  81. }