generated from streamingriver/template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
registry.go
60 lines (49 loc) · 890 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
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"sync"
"time"
)
func NewRegistry() *Registry {
return &Registry{
make(map[string]*Item),
&sync.RWMutex{},
}
}
type Registry struct {
registry map[string]*Item
mu *sync.RWMutex
}
type Item struct {
Port string
Host string
Seen int64
}
func (r Registry) getURL(ch, file string) *string {
r.mu.Lock()
defer r.mu.Unlock()
item, ok := r.registry[ch]
if !ok {
return nil
}
if item.Seen < time.Now().Unix() {
return nil
}
url := fmt.Sprintf("http://%s:%s/%s/%s", item.Host, item.Port, ch, file)
return &url
}
func (r *Registry) ping(ch, host, port string) {
r.mu.Lock()
defer r.mu.Unlock()
_, ok := r.registry[ch]
if !ok {
r.registry[ch] = &Item{
Port: port,
Host: host,
Seen: time.Now().Unix() + 3,
}
}
r.registry[ch].Port = port
r.registry[ch].Host = host
r.registry[ch].Seen = time.Now().Unix() + 3
}