New creates a pool from the given keys. All keys start in the valid state. Returns ErrNoKeys if keys is empty and ErrDuplicateKey if any key appears more than once.
(keys []string, clk quartz.Clock)
| 90 | // the valid state. Returns ErrNoKeys if keys is empty and |
| 91 | // ErrDuplicateKey if any key appears more than once. |
| 92 | func New(keys []string, clk quartz.Clock) (*Pool, error) { |
| 93 | if len(keys) == 0 { |
| 94 | return nil, ErrNoKeys |
| 95 | } |
| 96 | pool := &Pool{ |
| 97 | keys: make([]Key, len(keys)), |
| 98 | } |
| 99 | |
| 100 | seen := make(map[string]struct{}, len(keys)) |
| 101 | for i, val := range keys { |
| 102 | if _, exists := seen[val]; exists { |
| 103 | return nil, ErrDuplicateKey |
| 104 | } |
| 105 | seen[val] = struct{}{} |
| 106 | pool.keys[i] = Key{ |
| 107 | clock: clk, |
| 108 | value: val, |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return pool, nil |
| 113 | } |
| 114 | |
| 115 | // Value returns the key string. |
| 116 | func (k *Key) Value() string { |
no outgoing calls