Git Product home page Git Product logo

Comments (2)

sethvargo avatar sethvargo commented on August 11, 2024

Hi @dmytrohridin

I assume you're talking about the httplimit package specifically? For custom behavior, my general recommendation is to build your own middleware. The existing logic isn't particularly complex:

func (m *Middleware) Handle(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Call the key function - if this fails, it's an internal server error.
key, err := m.keyFunc(r)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
// Take from the store.
limit, remaining, reset, ok, err := m.store.Take(ctx, key)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
resetTime := time.Unix(0, int64(reset)).UTC().Format(time.RFC1123)
// Set headers (we do this regardless of whether the request is permitted).
w.Header().Set(HeaderRateLimitLimit, strconv.FormatUint(limit, 10))
w.Header().Set(HeaderRateLimitRemaining, strconv.FormatUint(remaining, 10))
w.Header().Set(HeaderRateLimitReset, resetTime)
// Fail if there were no tokens remaining.
if !ok {
w.Header().Set(HeaderRetryAfter, resetTime)
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
return
}
// If we got this far, we're allowed to continue, so call the next middleware
// in the stack to continue processing.
next.ServeHTTP(w, r)
})
}

Then you can set your own headers, responses, or even custom logic.

from go-limiter.

github-actions avatar github-actions commented on August 11, 2024

This issue has been automatically locked since there has not been any
recent activity after it was closed. Please open a new issue for
related bugs.

from go-limiter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.