-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpublisher.go
More file actions
66 lines (59 loc) · 1.64 KB
/
publisher.go
File metadata and controls
66 lines (59 loc) · 1.64 KB
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
61
62
63
64
65
66
package publisher
import (
"fmt"
"log"
"strconv"
"time"
"github.com/coreos/go-etcd/etcd"
)
// Publisher takes responsibility for regularly updating etcd with the host and port where this
// logger component is running. This permits other components to discover it.
type Publisher struct {
etcdClient *etcd.Client
etcdPath string
publishTTL uint64
logHost string
logPort int
ticker *time.Ticker
running bool
}
// NewPublisher returns a pointer to a new Publisher instance.
func NewPublisher(etcdHost string, etcdPort int, etcdPath string, publishInterval int,
publishTTL int, logHost string, logPort int) (*Publisher, error) {
etcdClient := etcd.NewClient([]string{fmt.Sprintf("http://%s:%d", etcdHost, etcdPort)})
ticker := time.NewTicker(time.Duration(publishInterval) * time.Second)
return &Publisher{
etcdClient: etcdClient,
etcdPath: etcdPath,
publishTTL: uint64(time.Duration(publishTTL) * time.Second),
logHost: logHost,
logPort: logPort,
ticker: ticker,
}, nil
}
// Start begins the publisher's main loop.
func (p *Publisher) Start() {
// Should only ever be called once
if !p.running {
p.running = true
go p.publish()
log.Println("publisher running")
}
}
func (p *Publisher) publish() {
for {
<-p.ticker.C
p.setEtcd("/host", p.logHost)
p.setEtcd("/port", strconv.Itoa(p.logPort))
}
}
func (p *Publisher) setEtcd(key string, value string) {
_, err := p.etcdClient.Set(fmt.Sprintf("%s%s", p.etcdPath, key), value, p.publishTTL)
if err != nil {
etcdErr, ok := err.(*etcd.EtcdError)
// Error code 105 is key already exists
if !ok || etcdErr.ErrorCode != 105 {
log.Println(err)
}
}
}