Skip to content

Commit

Permalink
Fix: make selector react immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamacro committed Apr 30, 2020
1 parent 7d51ab5 commit 94e0e4b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions adapters/outboundgroup/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (s *Selector) Set(name string) error {
for _, proxy := range getProvidersProxies(s.providers) {
if proxy.Name() == name {
s.selected = name
s.single.Reset()
return nil
}
}
Expand Down
4 changes: 4 additions & 0 deletions common/singledo/singledo.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (s *Single) Do(fn func() (interface{}, error)) (v interface{}, err error, s
return call.val, call.err, false
}

func (s *Single) Reset() {
s.last = time.Time{}
}

func NewSingle(wait time.Duration) *Single {
return &Single{wait: wait}
}
19 changes: 17 additions & 2 deletions common/singledo/singledo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestBasic(t *testing.T) {
}

var wg sync.WaitGroup
const n = 10
const n = 5
wg.Add(n)
for i := 0; i < n; i++ {
go func() {
Expand All @@ -33,7 +33,7 @@ func TestBasic(t *testing.T) {

wg.Wait()
assert.Equal(t, 1, foo)
assert.Equal(t, 9, shardCount)
assert.Equal(t, 4, shardCount)
}

func TestTimer(t *testing.T) {
Expand All @@ -51,3 +51,18 @@ func TestTimer(t *testing.T) {
assert.Equal(t, 1, foo)
assert.True(t, shard)
}

func TestReset(t *testing.T) {
single := NewSingle(time.Millisecond * 30)
foo := 0
call := func() (interface{}, error) {
foo++
return nil, nil
}

single.Do(call)
single.Reset()
single.Do(call)

assert.Equal(t, 2, foo)
}

0 comments on commit 94e0e4b

Please sign in to comment.