forked from ehazlett/stellar
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.go
49 lines (40 loc) · 1015 Bytes
/
backend.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 proxy
import (
"context"
"time"
api "github.com/ehazlett/stellar/api/services/proxy/v1"
ptypes "github.com/gogo/protobuf/types"
)
func (s *service) Backends(ctx context.Context, req *api.BackendRequest) (*api.BackendResponse, error) {
var backends []*api.Backend
servers, err := s.bclient.Servers()
if err != nil {
return nil, err
}
for _, server := range servers {
backend := &api.Backend{
Host: server.Host,
}
for _, b := range server.Upstreams {
up := s.loadUpstream(b)
backend.Upstreams = append(backend.Upstreams, up)
}
backends = append(backends, backend)
}
return &api.BackendResponse{
Backends: backends,
}, nil
}
func (s *service) loadUpstream(upstream string) *api.Upstream {
status := "up"
latency, err := checkConnection(upstream, s.cfg.ProxyHealthcheckInterval)
if err != nil {
latency = time.Millisecond * 0
status = err.Error()
}
return &api.Upstream{
Address: upstream,
Latency: ptypes.DurationProto(latency),
Status: status,
}
}