Skip to content

Commit b2c4719

Browse files
Implement early_exit probability (#139)
1 parent bde8017 commit b2c4719

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

handler/handler.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
log "github.com/sirupsen/logrus"
2121
"gopkg.in/square/go-jose.v2/jwt"
2222

23+
"github.com/m-lab/go/mathx"
2324
"github.com/m-lab/go/rtx"
2425
v2 "github.com/m-lab/locate/api/v2"
2526
"github.com/m-lab/locate/clientgeo"
@@ -30,7 +31,11 @@ import (
3031
"github.com/prometheus/common/model"
3132
)
3233

33-
var errFailedToLookupClient = errors.New("Failed to look up client location")
34+
var (
35+
errFailedToLookupClient = errors.New("Failed to look up client location")
36+
rand = mathx.NewRandom(time.Now().UnixNano())
37+
earlyExitProbability = 0.01
38+
)
3439

3540
// Signer defines how access tokens are signed.
3641
type Signer interface {
@@ -117,6 +122,9 @@ func extraParams(hostname string, p paramOpts) url.Values {
117122
// note: we only use the first value.
118123
v.Set(key, p.raw.Get(key))
119124
}
125+
if key == "early_exit" && rand.Src.Float64() < earlyExitProbability {
126+
v.Set(key, p.raw.Get(key))
127+
}
120128
}
121129

122130
// Add Locate Service version.

handler/handler_test.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,11 @@ func TestClient_Ready(t *testing.T) {
476476

477477
func TestExtraParams(t *testing.T) {
478478
tests := []struct {
479-
name string
480-
hostname string
481-
p paramOpts
482-
want url.Values
479+
name string
480+
hostname string
481+
p paramOpts
482+
earlyExitProbability float64
483+
want url.Values
483484
}{
484485
{
485486
name: "all-params",
@@ -518,9 +519,33 @@ func TestExtraParams(t *testing.T) {
518519
"locate_version": []string{"v2"},
519520
},
520521
},
522+
{
523+
name: "early-exit-true",
524+
p: paramOpts{
525+
raw: map[string][]string{"early_exit": {"250"}},
526+
version: "v2",
527+
},
528+
earlyExitProbability: 1,
529+
want: url.Values{
530+
"early_exit": []string{"250"},
531+
"locate_version": []string{"v2"},
532+
},
533+
},
534+
{
535+
name: "early-exit-false",
536+
p: paramOpts{
537+
raw: map[string][]string{"early_exit": {"250"}},
538+
version: "v2",
539+
},
540+
earlyExitProbability: 0,
541+
want: url.Values{
542+
"locate_version": []string{"v2"},
543+
},
544+
},
521545
}
522546
for _, tt := range tests {
523547
t.Run(tt.name, func(t *testing.T) {
548+
earlyExitProbability = tt.earlyExitProbability
524549
got := extraParams(tt.hostname, tt.p)
525550
if !reflect.DeepEqual(got, tt.want) {
526551
t.Errorf("extraParams() = %v, want %v", got, tt.want)

0 commit comments

Comments
 (0)