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