Clear http handler's resource

I am very new to golang. I have some handlers that make use of go’s http package. Go version is 1.6.x. Before a handler gets executed, there are some filter operations which may perform operations like rate limit. And the rate limit do some leaky bucket checking, so there is a queue like list and each handler will check and register its timestamp to redis.

Now I know that each handler is handled by go routine as mentioned by [1]. So I am thinking when the handler is accomplished its execution, I can cleanup its related info from redis. However I check online, I do not find doc mentioning how to perform post-operation.

The only info I found so far is [2] where seemingly I can use channel to receive notification by spawning a go routine for cleanup. Is this the only way to do that. If so, it seems that I need to insert such cleanup function to all endpoints/ handlers I have. If there are 100 endpoints/ handlers, I need to insert 100 lines to the end of all those handlers/ endpoints. Is there better way to do this? Thanks


There is a way that I use on my websites. Dynamic way, sort of. This has served a static site for a couple of years. The endpoint variable is created from r.URL.Path and if there is no html page, it serves a 404 page. This code serves about 50 endpoints.

There is another approach to store this instead redis, like algorithm Token Bucket, you can use this package for this:

or an implementation a little bit more simple, but manually, see this:

How are you performing these operations before the handler is executed? If it’s with some sort of wrapper around the goroutines, can you just add a call to cleanup the related data in redis to after the handler gets called?

I don’t know how these handler resources are created but normally this is the classic case for the middleware pattern:

func middleware(handler http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.