/
endpoints.go
45 lines (35 loc) · 884 Bytes
/
endpoints.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
package endpoints
import (
"errors"
"sync"
)
// EndpointDB contains a set of endpoints
type EndpointDB struct {
endpointsMutex sync.Mutex
endpoints map[string]*EndpointDialer
}
// NewEndpointsDB gives an EndpointDB instance
func NewEndpointsDB() *EndpointDB {
return &EndpointDB{
endpoints: map[string]*EndpointDialer{},
}
}
func (e *EndpointDB) Add(endpoint string, target *EndpointDialer) {
e.endpointsMutex.Lock()
defer e.endpointsMutex.Unlock()
e.endpoints[endpoint] = target
}
func (e *EndpointDB) Remove(endpoint string) {
e.endpointsMutex.Lock()
defer e.endpointsMutex.Unlock()
delete(e.endpoints, endpoint)
}
func (e *EndpointDB) Get(endpoint string) (*EndpointDialer, error) {
e.endpointsMutex.Lock()
defer e.endpointsMutex.Unlock()
ep, ok := e.endpoints[endpoint]
if !ok {
return nil, errors.New("endpoint not found")
}
return ep, nil
}