Skip to content

Commit

Permalink
Adds documentation and tests for middleware code
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Apr 11, 2024
1 parent 6dd47ef commit ff66e19
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
6 changes: 6 additions & 0 deletions middleware/http/clientip.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const (
CloudFront
)

// NewClientIPMiddleware returns a middleware function that extracts the client's IP address from the request
// and adds it to the request's context. The IP address is obtained using the provided IP address provider.
// The middleware function takes the next http.Handler as input and returns a new http.Handler that wraps
// the provided handler. When the new handler is called, it first extracts the client's IP address from the
// request using the provider, adds it to the request's context, and then calls the next handler in the chain.
// The IP address can be accessed from the request's context using the ClientIP key.
func NewClientIPMiddleware(provider Provider) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
4 changes: 4 additions & 0 deletions middleware/http/stash.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"sync"
)

// NewStashMiddleware returns a middleware function that adds a stash to the request context.
// The stash is a synchronized map that can be used to store and retrieve values during the connection lifecycle.
// The middleware function takes the next http.Handler as input and returns a new http.Handler that wraps the next handler.
// The returned handler adds the stash to the request context and then calls the next handler to process the request.
func NewStashMiddleware() func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
3 changes: 3 additions & 0 deletions middleware/request/error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (

type ErrorHandler func(conn wasabi.Connection, req wasabi.Request, err error) error

// NewErrorHandlingMiddleware returns a new error handling middleware that wraps the provided request handler.
// The middleware calls the provided `onError` function when an error occurs during request handling.
// It returns a new request handler that executes the provided `next` request handler and handles any errors that occur.
func NewErrorHandlingMiddleware(onError ErrorHandler) func(next wasabi.RequestHandler) wasabi.RequestHandler {
return func(next wasabi.RequestHandler) wasabi.RequestHandler {
return dispatch.RequestHandlerFunc(func(conn wasabi.Connection, req wasabi.Request) error {
Expand Down
6 changes: 6 additions & 0 deletions middleware/request/ratelimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
"github.com/ksysoev/wasabi/dispatch"
)

// NewRateLimiterMiddleware returns a middleware function that implements rate limiting for incoming requests.
// The `requestLimit` function is used to determine the rate limit for each request based on the provided request object.
// The `requestLimit` function takes a `wasabi.Request` object as input and returns the rate limit key, period, and limit.
// The `stor` variable is an instance of `ratestor.RateStor` used to store and manage rate limit information.
// The returned middleware function takes a `wasabi.RequestHandler` as input and returns a new `wasabi.RequestHandler`
// that performs rate limiting before passing the request to the next handler in the chain.
func NewRateLimiterMiddleware(requestLimit func(wasabi.Request) (key string, period time.Duration, limit uint64)) func(next wasabi.RequestHandler) wasabi.RequestHandler {
stor := ratestor.NewRateStor()

Expand Down
8 changes: 6 additions & 2 deletions middleware/request/throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (

type token struct{}

// NewTrottlerMiddleware creates a new throttler middleware that limits the number of concurrent requests.
// The `limit` parameter specifies the maximum number of concurrent requests allowed.
// It returns a function that takes a `wasabi.RequestHandler` as input and returns a new `wasabi.RequestHandler`
// that enforces the throttling limit.
func NewTrottlerMiddleware(limit uint) func(next wasabi.RequestHandler) wasabi.RequestHandler {
sem := make(chan token, limit)

Expand All @@ -16,8 +20,8 @@ func NewTrottlerMiddleware(limit uint) func(next wasabi.RequestHandler) wasabi.R
case sem <- token{}:
defer func() { <-sem }()
return next.Handle(conn, req)
case <-conn.Context().Done():
return conn.Context().Err()
case <-req.Context().Done():
return req.Context().Err()
}
})
}
Expand Down

0 comments on commit ff66e19

Please sign in to comment.