-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(thumbnails): Implement ratelimit for the grpc service
This moves the ratelimit ('THUMBNAILS_MAX_CONCURRENT_REQUESTS') from the HTTP endpoint to the GRPC endpoint. The HTTP endpoint is just used for downloading already created thumbnails. But the resource consuming part of thumbnail generation happens in the GRPC service.
- Loading branch information
Showing
13 changed files
with
112 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Bugfix: Thumbnail request limit | ||
|
||
The `THUMBNAILS_MAX_CONCURRENT_REQUESTS` setting was not working correctly. | ||
Previously it was just limiting the number of concurrent thumbnail downloads. | ||
Now the limit is applied to the number thumbnail generations requests. | ||
|
||
https://github.com/owncloud/ocis/pull/10225 |
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,39 @@ | ||
package ratelimiter | ||
|
||
import ( | ||
"context" | ||
|
||
"go-micro.dev/v4/errors" | ||
"go-micro.dev/v4/server" | ||
) | ||
|
||
// NewHandlerWrapper creates a blocking server side rate limiter. | ||
func NewHandlerWrapper(limit int) server.HandlerWrapper { | ||
if limit <= 0 { | ||
return func(h server.HandlerFunc) server.HandlerFunc { | ||
return h | ||
} | ||
} | ||
|
||
token := make(chan struct{}, limit) | ||
for i := 0; i < limit; i++ { | ||
token <- struct{}{} | ||
} | ||
|
||
return func(h server.HandlerFunc) server.HandlerFunc { | ||
return func(ctx context.Context, req server.Request, rsp interface{}) error { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case t := <-token: | ||
defer func() { | ||
token <- t | ||
}() | ||
return h(ctx, req, rsp) | ||
default: | ||
return errors.New("go.micro.server", "Rate limit exceeded", 429) | ||
} | ||
|
||
} | ||
} | ||
} |
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
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
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