Skip to content

Commit

Permalink
[req] Add request limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Nov 12, 2024
1 parent 227ac4b commit 88d78a1
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions req/limiter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package req

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import "time"

// ////////////////////////////////////////////////////////////////////////////////// //

// limiter is request limiter
type limiter struct {
lastCall time.Time
delay time.Duration
}

// ////////////////////////////////////////////////////////////////////////////////// //

// createLimiter creates new limiter
func createLimiter(rps float64) *limiter {
if rps <= 0 {
return nil
}

return &limiter{
delay: time.Duration(float64(time.Second) / rps),
}
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Wait blocks current goroutine execution until next time slot become available
func (l *limiter) Wait() {
if l == nil {
return
}

if l.lastCall.IsZero() {
l.lastCall = time.Now()
return
}

w := time.Since(l.lastCall)

if w < l.delay {
time.Sleep(l.delay - w)
}

l.lastCall = time.Now()
}

0 comments on commit 88d78a1

Please sign in to comment.