You can either use one of the existing other routers that have meddleware
support or you could wrap your handlers in another handler much like this:

handle("/foo",wrapHandler(rateLimiter, realHandler))

func wrapHandler(limit *ARateLimiter, handler func(http.ResponseWriter,
*http.Request)) func(http.ResponseWriter, *http.Request){
      return func(w http.ResponseWriter, r *http.Request) {
                   limit.Aquire() //Or however you use the limiter
                   handle(w,r)
     }
}

Very rudimentary and simplified but could perhaps work. You can wrap any
handler in the same way with either a shared limiter or one per call or any
combination.


fre 2 feb. 2018 kl 13:06 skrev Patrik Iselind <patrik....@gmail.com>:

>
> Den fredag 2 februari 2018 kl. 11:50:43 UTC+1 skrev Jesper Louis Andersen:
>>
>> A simple solution is to have a channel of type struct{} of some bound,
>> say 10. A process only gives service as long as it can pull a message from
>> the channel.
>>
>
> How would i hook in such a channel in the http server? Wouldn't a channel
> of type interface{} be better?
>
>
>> More advanced solutions include handling the channel as a token bucket
>> and regulating it to have a drip-rate.
>>
>
> I would prefer simple over complex in this instance.
>
>
>>
>> Even more advanced solutions sample your system and slows the drip feed
>> when it is overloaded with resources.
>>
>> You might also consider pushback and queuing. If you are overloaded, you
>> have to tell the caller to stop sending requests for a while.
>>
>> Consider using a model such as CoDel (Controlled Delay) on your queues.
>>
>
> pushback, queuing and CoDel might be good ideas further down the line for
> my project. I'll keep those in mind, thanks!
>
>
> // Patrik
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to