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