mennanov / limiters

Golang rate limiters for distributed applications
https://godoc.org/github.com/mennanov/limiters
MIT License
459 stars 49 forks source link

Allow partition key to be passed as a parameter #89

Open adrianlungu opened 3 weeks ago

adrianlungu commented 3 weeks ago

Hello,

Looking at the FixedWindowDynamoDB (and probably the others as well), it would be great if the partitionKey could be passed as a parameter in Increment instead of as part of the struct.

In highly concurrent scenarios, where multiple keys would need to be handled potentially at the same time, being able to re-use the same FixedWindow per table, and not per key, to increase multiple keys via the function call would be ideal.

I didn't get a chance to look at the extent of how such a change would impact the library as a whole, for FixedWindowDynamoDB it does not look like a big change, although it would be a breaking change.

mennanov commented 3 days ago

We can't add a backend specific argument (partitionKey) to the Increment() method as it won't make sense for other backends and it's a breaking change.

Instead, the context.WithValue(...) can be used to pass the values to the underlying state backend from the Limit() method.

@leeym what do you think?

leeym commented 2 days ago

I feel that using context.WithValue(...) will be a good alternative to make it work while keep it backward compatible.