-
Notifications
You must be signed in to change notification settings - Fork 7
/
registry.go
53 lines (47 loc) · 1.4 KB
/
registry.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
// Code generated by atomix-go-framework. DO NOT EDIT.
package set
import (
primitiveapi "github.com/atomix/atomix-api/go/atomix/primitive"
set "github.com/atomix/atomix-api/go/atomix/primitive/set"
"github.com/atomix/atomix-go-framework/pkg/atomix/errors"
"sync"
)
// NewProxyRegistry creates a new ProxyRegistry
func NewProxyRegistry() *ProxyRegistry {
return &ProxyRegistry{
proxies: make(map[primitiveapi.PrimitiveId]set.SetServiceServer),
}
}
type ProxyRegistry struct {
proxies map[primitiveapi.PrimitiveId]set.SetServiceServer
mu sync.RWMutex
}
func (r *ProxyRegistry) AddProxy(id primitiveapi.PrimitiveId, server set.SetServiceServer) error {
r.mu.Lock()
defer r.mu.Unlock()
if _, ok := r.proxies[id]; ok {
return errors.NewAlreadyExists("proxy '%s' already exists", id)
}
r.proxies[id] = server
log.Debugf("Added proxy %s to registry", id)
return nil
}
func (r *ProxyRegistry) RemoveProxy(id primitiveapi.PrimitiveId) error {
r.mu.Lock()
defer r.mu.Unlock()
if _, ok := r.proxies[id]; !ok {
return errors.NewNotFound("proxy '%s' not found", id)
}
delete(r.proxies, id)
log.Debugf("Removed proxy %s from registry", id)
return nil
}
func (r *ProxyRegistry) GetProxy(id primitiveapi.PrimitiveId) (set.SetServiceServer, error) {
r.mu.RLock()
defer r.mu.RUnlock()
proxy, ok := r.proxies[id]
if !ok {
return nil, errors.NewNotFound("proxy '%s' not found", id)
}
return proxy, nil
}