Skip to content

Commit

Permalink
Make shards a generic interface (#198)
Browse files Browse the repository at this point in the history
This allows return types other than strings,
e.g. a database connection.
  • Loading branch information
marein authored Nov 14, 2023
1 parent fcb9f62 commit 50bc22e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/Common/Sharding/Integration/Crc32ModShards.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
* It's therefore not suitable for every use case and should be used with caution.
* A better alternative would be to use a consistent hashing algorithm,
* which would reduce the likelihood of values being reassigned to another shard.
*
* @template T
* @implements Shards<T>
*/
final class Crc32ModShards implements Shards
{
/**
* @param string[] $shards
* @param T[] $shards
*
* @throws ShardingException
*/
Expand All @@ -30,7 +33,7 @@ public function __construct(
}
}

public function lookup(string $value): string
public function lookup(string $value): mixed
{
return $this->shards[crc32($value) % count($this->shards)];
}
Expand Down
6 changes: 5 additions & 1 deletion src/Common/Sharding/Shards.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

use Gaming\Common\Sharding\Exception\ShardingException;

/**
* @template T
*/
interface Shards
{
/**
* @return T
* @throws ShardingException
*/
public function lookup(string $value): string;
public function lookup(string $value): mixed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

final class DoctrineJsonGameRepository implements Games, GameFinder
{
/**
* @param Shards<string> $shards
*/
public function __construct(
private readonly Connection $connection,
private readonly string $tableName,
Expand Down

0 comments on commit 50bc22e

Please sign in to comment.