Skip to content

Commit

Permalink
fix hash function to work on different workers the same
Browse files Browse the repository at this point in the history
  • Loading branch information
dzakharchuk committed Mar 20, 2024
1 parent acca6a3 commit b6c4266
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion featureflags_client/http/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Any, Callable, Dict, List, Optional, Set

from featureflags_client.http.types import Check, Flag, Operator
from featureflags_client.http.utils import hash_flag_value

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -79,7 +80,9 @@ def percent(name: str, value: Any) -> Callable:
@except_false
def proc(ctx: Dict[str, Any]) -> bool:
ctx_val = ctx.get(name, _UNDEFINED)
return ctx_val is not _UNDEFINED and hash(ctx_val) % 100 < int(value)
hash_ctx_val = hash_flag_value(name, ctx_val)

return ctx_val is not _UNDEFINED and hash_ctx_val % 100 < int(value)

return proc

Expand Down
8 changes: 8 additions & 0 deletions featureflags_client/http/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import hashlib
import inspect
import struct
from enum import Enum, EnumMeta
from typing import Any, Dict, Generator, Mapping, Type, Union

Expand Down Expand Up @@ -54,3 +56,9 @@ def intervals_gen(
else:
success = yield retry_interval
retry_interval = min(retry_interval * 2, retry_interval_max)


def hash_flag_value(name: str, value: Any) -> int:
hash_digest = hashlib.md5(f"{name}{value}".encode()).digest() # noqa: S324
(hash_int,) = struct.unpack("<L", hash_digest[-4:])
return hash_int

0 comments on commit b6c4266

Please sign in to comment.