request

package
v0.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2024 License: MIT Imports: 14 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCircuitBreakerOpen = fmt.Errorf("circuit breaker is open")
)

Functions

func NewCacheMiddleware added in v0.5.0

func NewCacheMiddleware(requestCache func(r wasabi.Request) (cacheKey string, ttl time.Duration)) (middleware func(next wasabi.RequestHandler) wasabi.RequestHandler, cacheCloser func())

NewCacheMiddleware returns a middleware function that implements caching for requests. This middleware also implement debouncing pattern for requests to avoid duplicate requests hitting backend.

Parameters: - requestCache: A function that takes a `wasabi.Request` and returns a cache key and TTL duration.

Returns: - middleware: A function that takes a `wasabi.RequestHandler` and returns a new `wasabi.RequestHandler` with caching functionality. - cacheCloser: A function that stops the cache and performs cleanup.

func NewCircuitBreakerMiddleware

func NewCircuitBreakerMiddleware(threshold uint32, period time.Duration) func(next wasabi.RequestHandler) wasabi.RequestHandler

NewCircuitBreakerMiddleware creates a new circuit breaker middleware with the specified parameters. It returns a function that wraps the provided `wasabi.RequestHandler` and implements the circuit breaker logic. The circuit breaker monitors the number of errors and successful requests within a given time period. If the number of errors exceeds the threshold, the circuit breaker switches to the "Open" state and rejects subsequent requests. After a set amount of period, the circuit breaker switches to the "Semi-open" state. If request succeeds in "Semi-open" state, the state will be changed to "Closed", else back to "Open". The `threshold` parameter specifies the maximum number of errors allowed within the time period. The `period` parameter specifies the duration of the time period. The returned function can be used as middleware in a Wasabi server.

func NewErrorHandlingMiddleware

func NewErrorHandlingMiddleware(onError ErrorHandler) func(next wasabi.RequestHandler) wasabi.RequestHandler

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 NewMeasurer added in v0.3.0

func NewMeasurer(saveMetric func(req wasabi.Request, err error, duration time.Duration)) func(next wasabi.RequestHandler) wasabi.RequestHandler

NewMeasurer returns a middleware function that measures the duration of each request and saves the metric using the provided `saveMetric` function. The `saveMetric` function takes three parameters: the request, the error (if any), and the duration of the request. The middleware function wraps the provided `next` request handler and measures the duration of the request by calculating the time elapsed between the start and end of the request. After the request is handled, the metric is saved using the `saveMetric` function. The middleware function returns the wrapped request handler.

func NewRateLimiterMiddleware

func NewRateLimiterMiddleware(requestLimit func(wasabi.Request) (key string, period time.Duration, limit uint64)) func(next wasabi.RequestHandler) wasabi.RequestHandler

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 NewRetryMiddleware added in v0.5.0

func NewRetryMiddleware(getRetryInterval GetRetryInterval, maxRetries int, predicates ...func(e error) bool) func(next wasabi.RequestHandler) wasabi.RequestHandler

NewRetryMiddleware returns a new retry middleware that wraps the provided `next` request handler. The middleware accepts 3 params consisting of:

  • `getRetryInterval`: A higher order function that returns a function to calculate next retry interval
  • `maxRetries`: maximum number of retries allowed for this middleware
  • `predicates`: optional function(s) that determine whether the encountered errors needs retry. If multiple functions are provided, it will be executed in sequence and stop the moment a function returns false

If the request succeeds at any retry, the middleware returns `nil`. If all retries fail, it returns the last error encountered.

func NewSetTimeoutMiddleware

func NewSetTimeoutMiddleware(timeout time.Duration) func(next wasabi.RequestHandler) wasabi.RequestHandler

NewSetTimeoutMiddleware returns a middleware that sets a timeout for each request. The timeout duration is specified by the 'timeout' parameter. The returned middleware is a function that takes a 'next' request handler as input and returns a new request handler that applies the timeout to the incoming request.

func NewSpanMiddleware added in v0.5.1

func NewSpanMiddleware(spanName string, tracer trace.Tracer, attributes ...attribute.KeyValue) func(next wasabi.RequestHandler) wasabi.RequestHandler

NewSpanMiddleware returns a middleware function that creates a span for its requests. It takes the `spanName` and `attributes` as an argument. The attributes are essentially key value tuples used to set the span's attributes during creation. This middleware must be used in conjunction after the `WithTracer` server option

func NewTrottlerMiddleware

func NewTrottlerMiddleware(limit uint) func(next wasabi.RequestHandler) wasabi.RequestHandler

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.

Types

type ErrorHandler

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

type GetRetryInterval added in v0.5.1

type GetRetryInterval func(int) time.Duration

GetRetryInterval is a higher order function that returns a function to get retry interval. The inner function takes the retry interation (int) as an argument and returns the duration until next retry

func ExponentialGetRetryInterval added in v0.5.1

func ExponentialGetRetryInterval(seed time.Duration, delayFactor int) GetRetryInterval

ExponentialGetRetryInterval provides an exponential backoff implementation of GetRetryInterval. It enables the retry middleware to ensure exponentially higher wait times between retries. Parameters: - seed: The initial duration to wait until exponential backoff is triggered - delayFactor: The exponential index for backoff. Higher the delay factor, larger is the time between retries

Returns: - GetRetryInterval: returns the backoff function to get interval delay for current iteration

func LinearGetRetryInterval added in v0.5.1

func LinearGetRetryInterval(interval time.Duration) GetRetryInterval

LinearGetRetryInterval is an linear delay implementation of GetRetryInterval. It enables the retry middleware to ensure constant wait times between retries. Parameters: - interval: The duration to wait between retries

Returns: - GetRetryInterval: returns the backoff function to get interval delay for current iteration

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL