Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
Refine request time propagation.
Browse files Browse the repository at this point in the history
Request time is resolved at first call to `getLocalRateLimit()`, then is propagated across peer-to-peer for global behavior.
  • Loading branch information
Baliedge committed Feb 28, 2024
1 parent f02cb5b commit a665c3c
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $(GOLANGCI_LINT): ## Download Go linter

.PHONY: lint
lint: $(GOLANGCI_LINT) ## Run Go linter
$(GOLANGCI_LINT) run -v --fix -c .golangci.yml ./...
$(GOLANGCI_LINT) run -v -c .golangci.yml ./...

.PHONY: test
test: ## Run unit tests and measure code coverage
Expand Down
9 changes: 8 additions & 1 deletion gubernator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"strings"
"sync"
"time"

"github.com/mailgun/errors"
"github.com/mailgun/holster/v4/clock"
Expand Down Expand Up @@ -575,7 +576,13 @@ func (s *V1Instance) getLocalRateLimit(ctx context.Context, r *RateLimitReq) (_
defer func() { tracing.EndScope(ctx, err) }()
defer prometheus.NewTimer(metricFuncTimeDuration.WithLabelValues("V1Instance.getLocalRateLimit")).ObserveDuration()

requestTime := clock.Now()
var requestTime time.Time
if r.RequestTime != nil {
requestTime = time.UnixMilli(*r.RequestTime)
}
if requestTime.IsZero() {
requestTime = clock.Now()
}
resp, err := s.workerPool.GetRateLimit(ctx, r, requestTime)
if err != nil {
return nil, errors.Wrap(err, "during workerPool.GetRateLimit")
Expand Down
142 changes: 79 additions & 63 deletions gubernator.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions gubernator.proto
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ message RateLimitReq {
// this to pass trace context to other peers. Might be useful for future clients to pass along
// trace information to gubernator.
map<string, string> metadata = 9;

// The exact time of request in Epoch milliseconds.
// The is intended to be used for peer-to-peer requests to preserve
// timestamps.
optional int64 request_time = 10;
}

enum Status {
Expand Down
Loading

0 comments on commit a665c3c

Please sign in to comment.