Obvs icon indicating copy to clipboard operation
Obvs copied to clipboard

`t:Hammer.key/0` is `term()`, but all backend expect `String.t()`

Open Gladear opened this issue 8 months ago • 5 comments

Describe the bug

Hammer.key/0 is set to value term(), so we should be able to add any key. However, in all backends (e.g. ETS.FixWindows, Atomic.LeakyBucket, Redis.TokenBucket), @spec of implementations require the key to be a string. More importantly, while non-binary values do work in most backends, the Redis one actually require the key to be a string, and crashes otherwise.

** Provide the following details

  • Version of the library: 7.0.1
  • Backend used: *
  • Algorithm used: *
  • Elixir version (elixir -v): N/A
  • Erlang version (erl -v): N/A
  • Operating system: N/A

Expected behavior

Should the backend work with any term? Or should the key be updated to be a string? Having any term as key can be pretty useful, but does not seem to fit all backends.

I'm open to make the required changes in a PR, but I'd rather wait a decision on the matter beforehand 😄

Gladear avatar Apr 30 '25 07:04 Gladear

thanks for reporting this! I am not sure if there is a need of non string key and I am open to it. I would prefer to make them term in most and just limit redis to be a string would be my direction. Which means it s still a term just restricted to string in redis only

epinault avatar May 01 '25 19:05 epinault

Hello, sorry for the delay. For what it worth, I'd rather have all backends use the same types of keys, so it's easier to change if necessary. But if you decide to make the change for some backends only, well, I can apply this change 😄

Gladear avatar May 12 '25 13:05 Gladear

are you sayin you would prefer to limit to string only? or have the same time, and for redis figure out a way to serialize to string when it s not? that another option too

epinault avatar May 12 '25 14:05 epinault

Both options sound good to me! The "string-only" option might be a little less magic, but the "term-everywhere" option does not break compatibility.

Gladear avatar May 12 '25 15:05 Gladear

yea, I ll fix it with term everywhere, and handle in redis to convert the key . And if not a string, I ll add something to the doc to warn about it

epinault avatar May 12 '25 15:05 epinault

I have an MR almost ready to go

epinault avatar Jul 18 '25 23:07 epinault