diff --git a/telemetry/client.go b/telemetry/client.go index 41c018c5a..544083ae7 100644 --- a/telemetry/client.go +++ b/telemetry/client.go @@ -140,7 +140,7 @@ func (c *Client) Start(ctx context.Context) { if len(telemetryRequests) > 0 { err := c.pushTelemetryRequest(telemetryRequests) if err != nil { - if sendPeriod < 60 { //Stop the growing if the timer is > 60s to at least retry every minute + if sendPeriod < 60*time.Second { //Stop the growing if the timer is > 60s to at least retry every minute sendPeriod = sendPeriod * 2 } } else { @@ -196,8 +196,8 @@ func (c *Client) processAndPushTelemetry(data interface{}) { // This is assuming to not run concurrently as we are not locking the `telemetryRetryCache` func (c *Client) pushTelemetryRequest(request []TelemetryRequest) error { - if len(c.telemetryRetryCache)+len(request) > MaxRetryCache { //Limit the size of the cache to not grow the slice indefinitely in case the Telemetry server is gone for longer time - removeNum := len(c.telemetryRetryCache) + len(request) - MaxRetryCache + if len(c.telemetryRetryCache) > MaxRetryCache { //Limit the size of the cache to not grow the slice indefinitely in case the Telemetry server is gone for longer time + removeNum := len(c.telemetryRetryCache) - MaxRetryCache c.telemetryRetryCache = c.telemetryRetryCache[removeNum:] } c.telemetryRetryCache = append(c.telemetryRetryCache, request...) diff --git a/telemetry/client_test.go b/telemetry/client_test.go index ef5a64e90..e476b0144 100644 --- a/telemetry/client_test.go +++ b/telemetry/client_test.go @@ -266,3 +266,32 @@ func TestRetryCache(t *testing.T) { require.Equal(t, 0, len(client.telemetryRetryCache)) } + +func TestRetryCacheCleanup(t *testing.T) { + client := createClient(t, "") + client.Start(context.Background()) + + for i := 0; i < 6000; i++ { + client.PushReceivedEnvelope(v2protocol.NewEnvelope(&pb.WakuMessage{ + Payload: []byte{1, 2, 3, 4, 5}, + ContentTopic: testContentTopic, + Version: proto.Uint32(0), + Timestamp: proto.Int64(time.Now().Unix()), + }, 0, "")) + } + + time.Sleep(110 * time.Millisecond) + + require.Equal(t, 6000, len(client.telemetryRetryCache)) + + client.PushReceivedEnvelope(v2protocol.NewEnvelope(&pb.WakuMessage{ + Payload: []byte{1, 2, 3, 4, 5}, + ContentTopic: testContentTopic, + Version: proto.Uint32(0), + Timestamp: proto.Int64(time.Now().Unix()), + }, 0, "")) + + time.Sleep(210 * time.Millisecond) + + require.Equal(t, 5001, len(client.telemetryRetryCache)) +}