Skip to content

Commit 481dc1c

Browse files
avoid to send empty stats to the GUI (server)
By default, every second we send to the GUI the collected statistics by the daemon: metrics + events (connections). This call has a hard-coded timeout of one second, and if it reaches the deadline, a warning message is printed to the log. After several timeouts, the daemon closes the connection and tries to reconnect again. The daemon invokes this call even if there're no statistics to send. With one daemon and one GUI, it's not much of a problem. But if there're n nodes, the GUI will receive a minimum of n connections every second. In many scenarios, these empty calls are unnecessary. So, in order to optimize this behaviour, if the daemon has not collected new statistics, we won't invoke this call. This will slightly reduce the load on the GUI (server) when managing multiple nodes, and will save some CPU cycles on the daemon side. The connection status between the daemon and the GUI is managed with the following gRPC options: - The server and the daemon configures a 5-seconds keepalive to keep the channel open: 'grpc.keepalive_time_ms', 5000 - If after 5 seconds the keepalive msg is not received, then after 20 seconds the connection with the daemon will be closed if the server doesn't receive a response: 'grpc.keepalive_timeout_ms', 20000 So after about 30 seconds, if the daemon can't communicate with the GUI (server), it'll close the connection and retry it every second.
1 parent f2e2e52 commit 481dc1c

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

daemon/statistics/stats.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ type Statistics struct {
5757
maxWorkers int
5858
Dropped int
5959

60+
// flag to indicate if there're new events available
61+
newEvents bool
62+
6063
sync.RWMutex
6164
}
6265

@@ -226,6 +229,8 @@ func (s *Statistics) onConnection(con *conman.Connection, match *rule.Rule, wasM
226229
return
227230
}
228231
s.Events = append(s.Events, NewEvent(con, match))
232+
233+
s.newEvents = true
229234
}
230235

231236
func (s *Statistics) serializeEvents() []*protocol.Event {
@@ -246,6 +251,7 @@ func (s *Statistics) emptyStats() {
246251
if len(s.Events) > 0 {
247252
s.Events = make([]*Event, 0)
248253
}
254+
s.newEvents = false
249255
s.Unlock()
250256
}
251257

@@ -257,6 +263,10 @@ func (s *Statistics) Serialize() *protocol.Statistics {
257263
defer s.emptyStats()
258264
defer s.Unlock()
259265

266+
if !s.newEvents {
267+
return nil
268+
}
269+
260270
return &protocol.Statistics{
261271
DaemonVersion: core.Version,
262272
Rules: uint64(s.rules.NumRules()),

daemon/ui/client.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,16 @@ func (c *Client) ping(ts time.Time) (err error) {
326326
c.Lock()
327327
defer c.Unlock()
328328

329-
reqID := uint64(ts.UnixNano())
329+
serializedStats := c.stats.Serialize()
330+
if serializedStats == nil {
331+
log.Trace("client, no stats")
332+
return nil
333+
}
330334

335+
reqID := uint64(ts.UnixNano())
331336
pReq := &protocol.PingRequest{
332337
Id: reqID,
333-
Stats: c.stats.Serialize(),
338+
Stats: serializedStats,
334339
}
335340

336341
ctx, cancel := context.WithTimeout(context.Background(), time.Second)

0 commit comments

Comments
 (0)