-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rate limits on changes per config
- Loading branch information
1 parent
121f062
commit a7358dd
Showing
5 changed files
with
156 additions
and
1 deletion.
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
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,48 @@ | ||
package ratelimit | ||
|
||
import ( | ||
"time" | ||
|
||
sw "github.com/RussellLuo/slidingwindow" | ||
) | ||
|
||
// LocalWindow represents a window that ignores sync behavior entirely | ||
// and only stores counters in memory. | ||
// | ||
// NOTE: It's an exact copy of the LocalWindow provided by RussellLuo/slidingwindow | ||
// with an added capability of setting a custom start time. | ||
type LocalWindow struct { | ||
// The start boundary (timestamp in nanoseconds) of the window. | ||
// [start, start + size) | ||
start int64 | ||
|
||
// The total count of events happened in the window. | ||
count int64 | ||
} | ||
|
||
func NewLocalWindow() (*LocalWindow, sw.StopFunc) { | ||
return &LocalWindow{}, func() {} | ||
} | ||
|
||
func (w *LocalWindow) SetStart(s time.Time) { | ||
w.start = s.UnixNano() | ||
} | ||
|
||
func (w *LocalWindow) Start() time.Time { | ||
return time.Unix(0, w.start) | ||
} | ||
|
||
func (w *LocalWindow) Count() int64 { | ||
return w.count | ||
} | ||
|
||
func (w *LocalWindow) AddCount(n int64) { | ||
w.count += n | ||
} | ||
|
||
func (w *LocalWindow) Reset(s time.Time, c int64) { | ||
w.start = s.UnixNano() | ||
w.count = c | ||
} | ||
|
||
func (w *LocalWindow) Sync(now time.Time) {} |