-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #520 from essentialkaos/develop
Version 13.11.0
- Loading branch information
Showing
6 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters