Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a random, exponential increasing delay when there are no results for a service #117

Merged
merged 2 commits into from
Nov 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions pkg/rt/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math/rand"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -33,13 +34,15 @@ type MetadataProvider interface {
// Subscriber polls rt.fastly.com endpoints for a single service ID. It emits
// the received stats data to Prometheus metrics.
type Subscriber struct {
client HTTPClient
token string
serviceID string
provider MetadataProvider
metrics *prom.Metrics
postprocess func()
logger log.Logger
client HTTPClient
token string
serviceID string
provider MetadataProvider
metrics *prom.Metrics
postprocess func()
logger log.Logger
rtDelayCount int
oiDelayCount int
}

// SubscriberOption provides some additional behavior to a subscriber.
Expand Down Expand Up @@ -188,8 +191,10 @@ func (s *Subscriber) queryRealtime(ctx context.Context, ts uint64) (currentName
case http.StatusOK:
level.Debug(s.logger).Log("status_code", resp.StatusCode, "response_ts", response.Timestamp, "err", apiErr)
if strings.Contains(apiErr, "No data available") {
delay = s.rtDelay()
result = apiResultNoData
} else {
s.rtDelayCount = 0
result = apiResultSuccess
}
realtime.Process(&response, s.serviceID, name, version, s.metrics.Realtime)
Expand Down Expand Up @@ -250,8 +255,10 @@ func (s *Subscriber) queryOrigins(ctx context.Context, ts uint64) (currentName s
case http.StatusOK:
level.Debug(s.logger).Log("status_code", resp.StatusCode, "response_ts", response.Timestamp, "err", apiErr)
if strings.Contains(apiErr, "No data available") {
delay = s.oiDelay()
result = apiResultNoData
} else {
s.oiDelayCount = 0
result = apiResultSuccess
}
origin.Process(&response, s.serviceID, name, version, s.metrics.Origin)
Expand Down Expand Up @@ -313,3 +320,27 @@ func levelForError(base log.Logger, err error) log.Logger {
return nopLogger
}
}

const maxDelayCount = 5

func (s *Subscriber) rtDelay() time.Duration {
s.rtDelayCount++
if s.rtDelayCount > maxDelayCount {
s.rtDelayCount = maxDelayCount
}

return time.Duration(cube(s.rtDelayCount)+((rand.Intn(10)+1)*(s.rtDelayCount))) * time.Second
}

func (s *Subscriber) oiDelay() time.Duration {
s.oiDelayCount++
if s.oiDelayCount > maxDelayCount {
s.oiDelayCount = maxDelayCount
}

return time.Duration(cube(s.oiDelayCount)+((rand.Intn(10)+1)*(s.oiDelayCount))) * time.Second
}

func cube(i int) int {
return i * i * i
}