forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_list.go
54 lines (43 loc) · 841 Bytes
/
host_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package apidef
import (
"errors"
"sync"
)
type HostList struct {
hMutex sync.RWMutex
hosts []string
}
func NewHostList() *HostList {
hl := HostList{}
hl.hosts = make([]string, 0)
return &hl
}
func NewHostListFromList(newList []string) *HostList {
hl := NewHostList()
hl.Set(newList)
return hl
}
func (h *HostList) Set(newList []string) {
h.hMutex.Lock()
defer h.hMutex.Unlock()
h.hosts = newList
}
func (h *HostList) All() []string {
return h.hosts
}
func (h *HostList) GetIndex(i int) (string, error) {
if i < 0 {
return "", errors.New("index must be positive int")
}
h.hMutex.RLock()
defer h.hMutex.RUnlock()
if i > len(h.hosts)-1 {
return "", errors.New("index out of range")
}
return h.hosts[i], nil
}
func (h *HostList) Len() int {
h.hMutex.RLock()
defer h.hMutex.RUnlock()
return len(h.hosts)
}