-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
205 lines (179 loc) · 4.87 KB
/
server.go
File metadata and controls
205 lines (179 loc) · 4.87 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package server
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/flashbots/bmonitor/config"
"github.com/flashbots/bmonitor/httplogger"
"github.com/flashbots/bmonitor/logutils"
"github.com/flashbots/bmonitor/metrics"
"github.com/flashbots/bmonitor/utils"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/attribute"
otelapi "go.opentelemetry.io/otel/metric"
"go.uber.org/zap"
)
type Server struct {
cfg *config.Config
failure chan error
logger *zap.Logger
server *http.Server
builders map[string]*ethclient.Client
peers map[string]string
ticker *time.Ticker
}
func New(cfg *config.Config) (*Server, error) {
builders := make(map[string]*ethclient.Client, len(cfg.Monitor.Builders))
for _, b := range cfg.Monitor.Builders {
parts := strings.Split(b, "=")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid builder: %s", b)
}
name := strings.TrimSpace(parts[0])
rpc, err := ethclient.Dial(strings.TrimSpace(parts[1]))
if err != nil {
return nil, err
}
builders[name] = rpc
}
peers := make(map[string]string, 0)
for _, peer := range cfg.Monitor.Peers {
parts := strings.Split(peer, "=")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid peer config: %s", peer)
}
label := strings.TrimSpace(parts[0])
if len(label) == 0 {
return nil, fmt.Errorf("invalid peer label: %s", peer)
}
ip := net.ParseIP(strings.TrimSpace(parts[1]))
if ip == nil {
if len(label) == 0 {
return nil, fmt.Errorf("invalid peer ip: %s", peer)
}
}
if _, known := peers[ip.String()]; known {
if len(label) == 0 {
return nil, fmt.Errorf("duplicate ip: %s vs %s",
peer, fmt.Sprintf("%s=%s", label, peers[ip.String()]),
)
}
}
peers[ip.String()] = label
}
s := &Server{
builders: builders,
cfg: cfg,
failure: make(chan error, 1),
logger: zap.L(),
peers: peers,
ticker: time.NewTicker(cfg.Monitor.Interval),
}
mux := http.NewServeMux()
mux.HandleFunc("/", s.handleHealthcheck)
mux.Handle("/metrics", promhttp.Handler())
handler := httplogger.Middleware(s.logger, mux)
s.server = &http.Server{
Addr: cfg.Server.ListenAddress,
ErrorLog: logutils.NewHttpServerErrorLogger(s.logger),
Handler: handler,
MaxHeaderBytes: 1024,
ReadHeaderTimeout: 30 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
}
return s, nil
}
func (s *Server) Run() error {
l := s.logger
ctx := logutils.ContextWithLogger(context.Background(), l)
if err := metrics.Setup(ctx); err != nil {
return err
}
s.initializePeersMetrics(ctx)
go func() { // run the server
l.Info("Builder monitor server is going up...",
zap.String("server_listen_address", s.cfg.Server.ListenAddress),
)
if err := s.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
s.failure <- err
}
l.Info("Builder monitor server is down")
}()
go func() { // run the monitor loop
for {
s.monitor(ctx, <-s.ticker.C)
}
}()
errs := []error{}
{ // wait until termination or internal failure
terminator := make(chan os.Signal, 1)
signal.Notify(terminator, os.Interrupt, syscall.SIGTERM)
select {
case stop := <-terminator:
l.Info("Stop signal received; shutting down...",
zap.String("signal", stop.String()),
)
case err := <-s.failure:
l.Error("Internal failure; shutting down...",
zap.Error(err),
)
errs = append(errs, err)
exhaustErrors:
for { // exhaust the errors
select {
case err := <-s.failure:
l.Error("Extra internal failure",
zap.Error(err),
)
errs = append(errs, err)
default:
break exhaustErrors
}
}
}
}
{ // stop the monitor loop
s.ticker.Stop()
}
{ // stop the server
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
if err := s.server.Shutdown(ctx); err != nil {
l.Error("Builder monitor server shutdown failed",
zap.Error(err),
)
}
}
{ // close the clients
for _, rpc := range s.builders {
rpc.Close()
}
}
return utils.FlattenErrors(errs)
}
func (s *Server) initializePeersMetrics(ctx context.Context) {
for builder := range s.builders {
for _, typ := range []string{"loopback", "internal", "external"} {
metrics.PeersCount.Record(ctx, 0, otelapi.WithAttributes(
attribute.KeyValue{Key: "builder", Value: attribute.StringValue(builder)},
attribute.KeyValue{Key: "type", Value: attribute.StringValue(typ)},
))
}
for _, label := range s.peers {
metrics.PeersCount.Record(ctx, 0, otelapi.WithAttributes(
attribute.KeyValue{Key: "builder", Value: attribute.StringValue(builder)},
attribute.KeyValue{Key: "type", Value: attribute.StringValue("labelled")},
attribute.KeyValue{Key: "label", Value: attribute.StringValue(label)},
))
}
}
}