Skip to content

Commit 38de8fc

Browse files
committed
fix: use i64 for shards/ranges
Using int is problematic because it's platform-dependent. On 32-bit platform this may cause issues if SubjectShards > 2,147,483,647. This will make the number go negative and ranges might not match.
1 parent ada7864 commit 38de8fc

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

eppoclient/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ func (ec *EppoClient) getAssignment(subjectKey string, flagKey string, subjectAt
8989

9090
// Get assigned variation
9191
assignmentKey := "assignment-" + subjectKey + "-" + flagKey
92-
shard := getShard(assignmentKey, int64(config.SubjectShards))
92+
shard := getShard(assignmentKey, config.SubjectShards)
9393
variations := allocation.Variations
9494
var variationShard Variation
9595

9696
for _, variation := range variations {
97-
if isShardInRange(int(shard), variation.ShardRange) {
97+
if isShardInRange(shard, variation.ShardRange) {
9898
variationShard = variation
9999
}
100100
}
@@ -137,9 +137,9 @@ func getSubjectVariationOverride(experimentConfig experimentConfiguration, subje
137137
return Null()
138138
}
139139

140-
func isInExperimentSample(subjectKey string, flagKey string, subjectShards int, percentExposure float32) bool {
140+
func isInExperimentSample(subjectKey string, flagKey string, subjectShards int64, percentExposure float32) bool {
141141
shardKey := "exposure-" + subjectKey + "-" + flagKey
142-
shard := getShard(shardKey, int64(subjectShards))
142+
shard := getShard(shardKey, subjectShards)
143143

144144
return float64(shard) <= float64(percentExposure)*float64(subjectShards)
145145
}

eppoclient/configurationstore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Allocation struct {
2222
type experimentConfiguration struct {
2323
Name string `json:"name"`
2424
Enabled bool `json:"enabled"`
25-
SubjectShards int `json:"subjectShards"`
25+
SubjectShards int64 `json:"subjectShards"`
2626
Rules []rule `json:"rules"`
2727
Overrides map[string]Value `json:"typedOverrides"`
2828
Allocations map[string]Allocation `json:"allocations"`

eppoclient/shard.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
)
77

88
type shardRange struct {
9-
Start int `json:"start"`
10-
End int `json:"end"`
9+
Start int64 `json:"start"`
10+
End int64 `json:"end"`
1111
}
1212

1313
func getShard(input string, subjectShards int64) int64 {
@@ -17,6 +17,6 @@ func getShard(input string, subjectShards int64) int64 {
1717
return intVal % subjectShards
1818
}
1919

20-
func isShardInRange(shard int, shardRange shardRange) bool {
20+
func isShardInRange(shard int64, shardRange shardRange) bool {
2121
return shard >= shardRange.Start && shard < shardRange.End
2222
}

eppoclient/shard_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Test_isShardInRange_Fail(t *testing.T) {
2727
input := 5
2828
inputRange := shardRange{Start: 1, End: 5}
2929
expected := false
30-
result := isShardInRange(input, inputRange)
30+
result := isShardInRange(int64(input), inputRange)
3131

3232
assert.Equal(t, expected, result)
3333
}
@@ -36,7 +36,7 @@ func Test_isShardInRange_Success(t *testing.T) {
3636
input := 3
3737
inputRange := shardRange{Start: 1, End: 7}
3838
expected := true
39-
result := isShardInRange(input, inputRange)
39+
result := isShardInRange(int64(input), inputRange)
4040

4141
assert.Equal(t, expected, result)
4242
}

0 commit comments

Comments
 (0)