Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: solver rate limiter exemption list #507

Merged
merged 4 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .local.dev
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ WEB3_TOKEN_ADDRESS_=0xa513E6E4b8f2a923D98304ec87F64353C4D5C853
WEB3_USERS_ADDRESS=0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82
BACALHAU_API_HOST=localhost
BACALHAU_API_PORT=1234
SERVER_RATE_EXEMPTED_IPS=127.0.0.1,::1
1 change: 1 addition & 0 deletions pkg/http/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ValidationToken struct {
type RateLimiterOptions struct {
RequestLimit int
WindowLength int
ExemptedIPs []string
}

type ClientOptions struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/options/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func GetDefaultRateLimiterOptions() http.RateLimiterOptions {
return http.RateLimiterOptions{
RequestLimit: GetDefaultServeOptionInt("SERVER_RATE_REQUEST_LIMIT", 5),
WindowLength: GetDefaultServeOptionInt("SERVER_RATE_WINDOW_LENGTH", 10),
ExemptedIPs: GetDefaultServeOptionStringArray("SERVER_RATE_EXEMPTED_IPS", []string{}),
}
}

Expand Down
30 changes: 29 additions & 1 deletion pkg/solver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
corehttp "net/http"
"net"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -62,17 +63,44 @@ func NewSolverServer(
*
*/

func exemptIPKeyFunc(exemptIPs []string) func(r *corehttp.Request) (string, error) {
return func(r *corehttp.Request) (string, error) {
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
ip = r.RemoteAddr
}


for _, exemptIP := range exemptIPs {
if ip == exemptIP {
return "", nil
}
}

return ip, nil
}
}

func (solverServer *solverServer) ListenAndServe(ctx context.Context, cm *system.CleanupManager, tracerProvider *trace.TracerProvider) error {
router := mux.NewRouter()

subrouter := router.PathPrefix(http.API_SUB_PATH).Subrouter()

subrouter.Use(http.CorsMiddleware)
subrouter.Use(otelmux.Middleware("solver", otelmux.WithTracerProvider(tracerProvider)))


exemptIPs := solverServer.options.RateLimiter.ExemptedIPs

log.Debug().Strs("exemptIPs", exemptIPs).Msg("Loaded rate limit exemptions")

subrouter.Use(httprate.Limit(
solverServer.options.RateLimiter.RequestLimit,
time.Duration(solverServer.options.RateLimiter.WindowLength)*time.Second,
httprate.WithKeyFuncs(httprate.KeyByRealIP, httprate.KeyByEndpoint),
httprate.WithKeyFuncs(
exemptIPKeyFunc(exemptIPs),
httprate.KeyByEndpoint,
),
))

subrouter.HandleFunc("/job_offers", http.GetHandler(solverServer.getJobOffers)).Methods("GET")
Expand Down
Loading