Skip to content

Commit

Permalink
solver rate limitter exemptions
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindi committed Jan 31, 2025
1 parent 9afda87 commit a6d63c6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
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

0 comments on commit a6d63c6

Please sign in to comment.