forked from fluxcd/flux
/
daemon.go
147 lines (133 loc) · 3.45 KB
/
daemon.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package http
import (
"net/http"
"net/url"
"os"
"time"
"github.com/go-kit/kit/log"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/go-kit/kit/metrics/prometheus"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"github.com/weaveworks/flux"
"github.com/weaveworks/flux/http/websocket"
"github.com/weaveworks/flux/platform"
"github.com/weaveworks/flux/platform/rpc"
)
// Daemon handles communication from the daemon to the service
type Daemon struct {
client *http.Client
ua string
token flux.Token
url *url.URL
endpoint string
platform platform.Platform
logger log.Logger
quit chan struct{}
ws websocket.Websocket
}
var (
ErrEndpointDeprecated = errors.New("Your fluxd version is deprecated - please upgrade, see https://github.com/weaveworks/flux/releases")
connectionDuration = prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Namespace: "flux",
Subsystem: "fluxd",
Name: "connection_duration_seconds",
Help: "Duration in seconds of the current connection to fluxsvc. Zero means unconnected.",
}, []string{"target"})
)
func NewDaemon(client *http.Client, ua string, t flux.Token, router *mux.Router, endpoint string, p platform.Platform, logger log.Logger) (*Daemon, error) {
u, err := MakeURL(endpoint, router, "RegisterDaemonV5")
if err != nil {
return nil, errors.Wrap(err, "constructing URL")
}
a := &Daemon{
client: client,
ua: ua,
token: t,
url: u,
endpoint: endpoint,
platform: p,
logger: logger,
quit: make(chan struct{}),
}
go a.loop()
return a, nil
}
func (a *Daemon) loop() {
backoff := 5 * time.Second
errc := make(chan error, 1)
for {
go func() {
errc <- a.connect()
}()
select {
case err := <-errc:
if err != nil {
a.logger.Log("err", err)
if err == ErrEndpointDeprecated {
// We have logged the deprecation error, now crashloop to garner attention
os.Exit(1)
}
time.Sleep(backoff)
continue
}
case <-a.quit:
return
}
}
}
func (a *Daemon) connect() error {
a.setConnectionDuration(0)
a.logger.Log("connecting", true)
ws, err := websocket.Dial(a.client, a.ua, a.token, a.url)
if err != nil {
if err, ok := err.(*websocket.DialErr); ok && err.HTTPResponse != nil && err.HTTPResponse.StatusCode == http.StatusGone {
return ErrEndpointDeprecated
}
return errors.Wrapf(err, "executing websocket %s", a.url)
}
a.ws = ws
defer func() {
a.ws = nil
// TODO: handle this error
a.logger.Log("connection closing", true, "err", ws.Close())
}()
a.logger.Log("connected", true)
// Instrument connection lifespan
connectedAt := time.Now()
disconnected := make(chan struct{})
defer close(disconnected)
go func() {
t := time.NewTicker(1 * time.Second)
for {
select {
case now := <-t.C:
a.setConnectionDuration(now.Sub(connectedAt).Seconds())
case <-disconnected:
t.Stop()
a.setConnectionDuration(0)
return
}
}
}()
// Hook up the rpc server. We are a websocket _client_, but an RPC
// _server_.
rpcserver, err := rpc.NewServer(a.platform)
if err != nil {
return errors.Wrap(err, "initializing rpc client")
}
rpcserver.ServeConn(ws)
a.logger.Log("disconnected", true)
return nil
}
func (a *Daemon) setConnectionDuration(duration float64) {
connectionDuration.With("target", a.endpoint).Set(duration)
}
// Close closes the connection to the service
func (a *Daemon) Close() error {
close(a.quit)
if a.ws == nil {
return nil
}
return a.ws.Close()
}