Skip to content

Commit

Permalink
Merge pull request #160 from wnxd/development
Browse files Browse the repository at this point in the history
Fix `getRouteHash` concurrency safety
  • Loading branch information
topi314 authored Jun 3, 2022
2 parents 758a43d + afe9393 commit b385c99
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion rest/rest_rate_limiter_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ type (
global int64

// route.APIRoute -> Hash
hashes map[*route.APIRoute]routeHash
hashes map[*route.APIRoute]routeHash
hashesMu sync.Mutex
// Hash + Major Parameter -> bucket
buckets map[hashMajor]*bucket
bucketsMu sync.Mutex
Expand Down Expand Up @@ -96,15 +97,18 @@ func (l *rateLimiterImpl) Reset() {
l.bucketsMu = sync.Mutex{}
l.global = 0
l.hashes = map[*route.APIRoute]routeHash{}
l.hashesMu = sync.Mutex{}
}

func (l *rateLimiterImpl) getRouteHash(route *route.CompiledAPIRoute) hashMajor {
l.hashesMu.Lock()
hash, ok := l.hashes[route.APIRoute]
if !ok {
// generate routeHash
hash = routeHash(route.APIRoute.Method().String() + "+" + route.APIRoute.Path())
l.hashes[route.APIRoute] = hash
}
l.hashesMu.Unlock()
if route.MajorParams() != "" {
hash += routeHash("+" + route.MajorParams())
}
Expand Down

0 comments on commit b385c99

Please sign in to comment.