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

rate limiter example #106

Closed
kamilsk opened this issue Sep 8, 2017 · 0 comments
Closed

rate limiter example #106

kamilsk opened this issue Sep 8, 2017 · 0 comments
Assignees

Comments

@kamilsk
Copy link
Owner

kamilsk commented Sep 8, 2017

The concept:

sem := limiter.ForUser(request.Context())
release, err := sem.Acquire(semaphore.WithTimeout(config.SLA))
if err != nil {
	http.Error(rw, "operation timeout", http.StatusGatewayTimeout)
	return
}
deadline := semaphore.WithDeadline(time.Now().Add(config.LimitRange))
go func() {
	release()
	release, err = sem.Acquire(deadline)
	if err != nil { return }
	<-deadline
	release()
}()
// handle user request
@kamilsk kamilsk self-assigned this Sep 8, 2017
kamilsk added a commit that referenced this issue Oct 5, 2017
kamilsk added a commit that referenced this issue Oct 6, 2017
kamilsk added a commit that referenced this issue Oct 6, 2017
@kamilsk kamilsk closed this as completed Oct 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant