forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
49 lines (42 loc) · 903 Bytes
/
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
package discovery
import (
"strings"
"time"
)
const (
registryRefreshSec = 15
)
type registry struct {
client Client
directory string
}
func newRegistry(client Client, directory string) *registry {
return ®istry{client, directory}
}
func (r *registry) Register(value string) <-chan error {
errChan := make(chan error, 1)
go r.register(value, errChan)
return errChan
}
func (r *registry) GetAll() ([]string, error) {
m, err := r.client.GetAll(r.directory)
if err != nil {
return nil, err
}
s := make([]string, len(m))
i := 0
for value := range m {
s[i] = strings.TrimPrefix(value, r.directory+"/")
i++
}
return s, nil
}
func (r *registry) register(value string, errChan chan<- error) {
for {
if err := r.client.Set(r.directory+"/"+value, value, registryRefreshSec*2); err != nil {
errChan <- err
return
}
time.Sleep(registryRefreshSec * time.Second)
}
}