Skip to content

Commit 8927f43

Browse files
author
Matthew
committed
fix(logger): use one ticker for all time-related operations
In a separate goroutine, we were sleeping for the exact same time as publishInterval was set to. As @kalbasit mentioned in [a comment][1], it's better to use a ticker anyways. This removes one extra underlying system call to clock_gettime() by making both publishKeys() and the log drain poll share the same ticker event. [1]: deis/deis#3795 (comment)
1 parent 95932cd commit 8927f43

1 file changed

Lines changed: 13 additions & 29 deletions

File tree

logger/main.go

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func main() {
4444
flag.Parse()
4545

4646
client := etcd.NewClient([]string{"http://" + publishHost + ":" + publishPort})
47-
47+
ticker := time.NewTicker(time.Duration(publishInterval) * time.Second)
4848
signalChan := make(chan os.Signal, 1)
4949
drainChan := make(chan string)
5050
stopChan := make(chan bool)
@@ -59,15 +59,19 @@ func main() {
5959

6060
go syslogd.Listen(exitChan, cleanupChan, drainChan, fmt.Sprintf("%s:%d", logAddr, logPort))
6161
if enablePublish {
62-
go publishService(exitChan, client, publishHost, publishPath, strconv.Itoa(logPort), uint64(time.Duration(publishTTL)*time.Second))
62+
publishKeys(client, publishHost, publishPath, strconv.Itoa(logPort), uint64(time.Duration(publishTTL)*time.Second))
6363
}
6464

65-
// HACK (bacongobbler): poll etcd for changes in the log drain value
66-
// etcd's .Watch() implementation is broken when you use TTLs
67-
//
68-
// https://github.com/coreos/etcd/issues/2679
69-
go func() {
70-
for {
65+
for {
66+
select {
67+
case <-ticker.C:
68+
if enablePublish {
69+
publishKeys(client, publishHost, publishPath, strconv.Itoa(logPort), uint64(time.Duration(publishTTL)*time.Second))
70+
}
71+
// HACK (bacongobbler): poll etcd every publishInterval for changes in the log drain value.
72+
// etcd's .Watch() implementation is broken when you use TTLs
73+
//
74+
// https://github.com/coreos/etcd/issues/2679
7175
resp, err := client.Get(publishPath+"/drain", false, false)
7276
if err != nil {
7377
log.Printf("warning: could not retrieve drain URI from etcd: %v\n", err)
@@ -76,16 +80,11 @@ func main() {
7680
if resp != nil && resp.Node != nil {
7781
drainChan <- resp.Node.Value
7882
}
79-
time.Sleep(time.Duration(publishInterval) * time.Second)
80-
}
81-
}()
82-
83-
for {
84-
select {
8583
case <-signalChan:
8684
close(exitChan)
8785
stopChan <- true
8886
case <-cleanupChan:
87+
ticker.Stop()
8988
return
9089
}
9190
}
@@ -97,21 +96,6 @@ func publishKeys(client *etcd.Client, host, etcdPath, port string, ttl uint64) {
9796
setEtcd(client, etcdPath+"/port", port, ttl)
9897
}
9998

100-
// publishServices publishes keys immediately, then every publishInterval seconds until it receives
101-
// something on exitChan.
102-
func publishService(exitChan chan bool, client *etcd.Client, host string, etcdPath string, port string, ttl uint64) {
103-
publishKeys(client, host, etcdPath, port, ttl)
104-
t := time.NewTicker(time.Duration(publishInterval) * time.Second)
105-
for {
106-
select {
107-
case <-t.C:
108-
publishKeys(client, host, etcdPath, port, ttl)
109-
case <-exitChan:
110-
return
111-
}
112-
}
113-
}
114-
11599
func setEtcd(client *etcd.Client, key, value string, ttl uint64) {
116100
_, err := client.Set(key, value, ttl)
117101
if err != nil && !strings.Contains(err.Error(), "Key already exists") {

0 commit comments

Comments
 (0)