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

fix: Synchronize access to disabledUntil #158

Merged
merged 1 commit into from
Feb 3, 2020
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
25 changes: 19 additions & 6 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,15 @@ type HTTPTransport struct {
// current in-flight items and starts a new batch for subsequent events.
buffer chan batch

disabledUntil time.Time

start sync.Once

// Size of the transport buffer. Defaults to 30.
BufferSize int
// HTTP Client request timeout. Defaults to 30 seconds.
Timeout time.Duration

mu sync.RWMutex
disabledUntil time.Time
}

// NewHTTPTransport returns a new pre-configured instance of HTTPTransport
Expand Down Expand Up @@ -176,7 +177,13 @@ func (t *HTTPTransport) Configure(options ClientOptions) {

// SendEvent assembles a new packet out of `Event` and sends it to remote server.
func (t *HTTPTransport) SendEvent(event *Event) {
if t.dsn == nil || time.Now().Before(t.disabledUntil) {
if t.dsn == nil {
return
}
t.mu.RLock()
disabled := time.Now().Before(t.disabledUntil)
t.mu.RUnlock()
if disabled {
return
}

Expand Down Expand Up @@ -294,7 +301,10 @@ func (t *HTTPTransport) worker() {

// Process all batch items.
for request := range b.items {
if time.Now().Before(t.disabledUntil) {
t.mu.RLock()
disabled := time.Now().Before(t.disabledUntil)
t.mu.RUnlock()
if disabled {
continue
}

Expand All @@ -305,8 +315,11 @@ func (t *HTTPTransport) worker() {
}

if response != nil && response.StatusCode == http.StatusTooManyRequests {
t.disabledUntil = time.Now().Add(retryAfter(time.Now(), response))
Logger.Printf("Too many requests, backing off till: %s\n", t.disabledUntil)
deadline := time.Now().Add(retryAfter(time.Now(), response))
t.mu.Lock()
t.disabledUntil = deadline
t.mu.Unlock()
Logger.Printf("Too many requests, backing off till: %s\n", deadline)
}
}

Expand Down