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.

mode.go 1.6 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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
  7. import (
  8. "fmt"
  9. "strings"
  10. )
  11. // Mode indicates the user's preference on reads.
  12. type Mode uint8
  13. // Mode constants
  14. const (
  15. _ Mode = iota
  16. // PrimaryMode indicates that only a primary is
  17. // considered for reading. This is the default
  18. // mode.
  19. PrimaryMode
  20. // PrimaryPreferredMode indicates that if a primary
  21. // is available, use it; otherwise, eligible
  22. // secondaries will be considered.
  23. PrimaryPreferredMode
  24. // SecondaryMode indicates that only secondaries
  25. // should be considered.
  26. SecondaryMode
  27. // SecondaryPreferredMode indicates that only secondaries
  28. // should be considered when one is available. If none
  29. // are available, then a primary will be considered.
  30. SecondaryPreferredMode
  31. // NearestMode indicates that all primaries and secondaries
  32. // will be considered.
  33. NearestMode
  34. )
  35. // ModeFromString returns a mode corresponding to
  36. // mode.
  37. func ModeFromString(mode string) (Mode, error) {
  38. switch strings.ToLower(mode) {
  39. case "primary":
  40. return PrimaryMode, nil
  41. case "primarypreferred":
  42. return PrimaryPreferredMode, nil
  43. case "secondary":
  44. return SecondaryMode, nil
  45. case "secondarypreferred":
  46. return SecondaryPreferredMode, nil
  47. case "nearest":
  48. return NearestMode, nil
  49. }
  50. return Mode(0), fmt.Errorf("unknown read preference %v", mode)
  51. }