1
2
mirror of https://github.com/yunginnanet/Rate5 synced 2024-06-28 18:11:01 +00:00

Merge pull request #1 from yunginnanet/racecondition

This commit is contained in:
kayos 2022-03-04 13:42:56 -08:00 committed by GitHub
commit a9888cad09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 115 additions and 31 deletions

70
.github/workflows/codeql-analysis.yml vendored Normal file

@ -0,0 +1,70 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"
on:
push:
branches: [ master ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
schedule:
- cron: '42 6 * * 0'
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://git.io/codeql-language-support
steps:
- name: Checkout repository
uses: actions/checkout@v2
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
# Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language
#- run: |
# make bootstrap
# make release
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1

@ -1,25 +1,18 @@
name: Go
name: Build Status
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./...
- uses: actions/checkout@v2
with:
fetch-depth: 2
- uses: actions/setup-go@v2
with:
go-version: '1.17'
- name: Run coverage
run: go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
- name: Upload coverage to Codecov
run: bash <(curl -s https://codecov.io/bash)

2
.gitignore vendored Normal file

@ -0,0 +1,2 @@
.idea
*.save

@ -1,5 +1,6 @@
# Rate5
[![GoDoc](https://godoc.org/github.com/yunginnanet/?status.svg)](https://godoc.org/github.com/yunginnanet/Rate5) [![Go Report Card](https://goreportcard.com/badge/github.com/yunginnanet/Rate5)](https://goreportcard.com/report/github.com/yunginnanet/Rate5) [![Go](https://github.com/yunginnanet/Rate5/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/yunginnanet/Rate5/actions/workflows/go.yml)
[![codecov](https://codecov.io/gh/yunginnanet/Rate5/branch/main/graph/badge.svg?token=R7WU58G5L7)](https://codecov.io/gh/yunginnanet/Rate5)
A generic ratelimitter for any golang project.
See [the docs](https://godoc.org/github.com/yunginnanet/Rate5) and the [examples](_examples/rated.go) below for more details.

@ -2,7 +2,7 @@ package rate5
import (
"sync/atomic"
"sync"
"github.com/patrickmn/go-cache"
)
@ -44,6 +44,8 @@ type Limiter struct {
locker uint32
count atomic.Value
known map[interface{}]rated
dmu *sync.RWMutex
}
// Policy defines the mechanics of our ratelimiter.

@ -2,6 +2,7 @@ package rate5
import (
"fmt"
"sync"
"sync/atomic"
"time"
@ -54,18 +55,27 @@ func newLimiter(policy Policy) *Limiter {
q.Ruleset = policy
q.Patrons = cache.New(time.Duration(q.Ruleset.Window)*time.Second, 5*time.Second)
q.known = make(map[interface{}]rated)
q.Debug = false
q.dmu = &sync.RWMutex{}
q.SetDebug(false)
return q
}
func (q *Limiter) SetDebug(on bool) {
q.dmu.Lock()
q.Debug = on
q.dmu.Unlock()
}
// DebugChannel enables Debug mode and returns a channel where debug messages are sent (NOTE: You must read from this channel if created via this function or it will block)
func (q *Limiter) DebugChannel() chan string {
q.dmu.Lock()
q.Patrons.OnEvicted(func(src string, count interface{}) {
q.debugPrint("ratelimit (expired): ", src, " ", count)
})
q.Debug = true
debugChannel = make(chan string, 20)
q.dmu.Unlock()
return debugChannel
}
@ -97,7 +107,7 @@ func (q *Limiter) strictLogic(src string, count int) {
q.known[src].inc()
extwindow := q.Ruleset.Window + q.known[src].seen.Load().(int)
if err := q.Patrons.Replace(src, count, time.Duration(extwindow)*time.Second); err != nil {
q.debugPrint("Rate5: " + err.Error())
q.debugPrint("ratelimit: " + err.Error())
}
q.debugPrint("ratelimit (strictly limited): ", count, " ", src)
q.increment()
@ -111,7 +121,7 @@ func (q *Limiter) Check(from Identity) bool {
if count, err = q.Patrons.IncrementInt(src, 1); err != nil {
q.debugPrint("ratelimit (new): ", src)
if err := q.Patrons.Add(src, 1, time.Duration(q.Ruleset.Window)*time.Second); err != nil {
q.debugPrint("Rate5: " + err.Error())
q.debugPrint("ratelimit: " + err.Error())
}
return false
}
@ -155,7 +165,11 @@ func (q *Limiter) GetGrandTotalRated() int {
}
func (q *Limiter) debugPrint(a ...interface{}) {
q.dmu.RLock()
if q.Debug {
q.dmu.RUnlock()
debugChannel <- fmt.Sprint(a...)
return
}
q.dmu.RUnlock()
}

@ -174,7 +174,9 @@ func Test_ConcurrentSafetyTest(t *testing.T) {
usedkeys := make(map[string]interface{})
for n := 0; n != 5000; n++ {
const jobs = 1000
for n := 0; n != jobs ; n++ {
randos[n] = new(randomPatron)
ok := true
for ok {
@ -188,17 +190,17 @@ func Test_ConcurrentSafetyTest(t *testing.T) {
t.Logf("generated %d Patrons with unique keys", len(randos))
doneChan := make(chan bool)
finChan := make(chan bool)
doneChan := make(chan bool, 10)
finChan := make(chan bool, 10)
var finished = 0
for _, rp := range randos {
for n := 0; n != 5; n++ {
go func() {
limiter.Check(rp)
limiter.Peek(rp)
go func(randomp *randomPatron) {
limiter.Check(randomp)
limiter.Peek(randomp)
finChan <- true
}()
}(rp)
}
}
@ -208,7 +210,7 @@ func Test_ConcurrentSafetyTest(t *testing.T) {
case <-finChan:
finished++
default:
if finished == 25000 {
if finished == (jobs * 5) {
done = true
break
}