-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[exp/singleflight] refactor ticker code
Change-Id: I64bfadfdbad7179c8f3937a2b4a11bef7d923d37
- Loading branch information
Showing
4 changed files
with
152 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package singleflight | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/jxskiss/gopkg/v2/perf/mselect" | ||
) | ||
|
||
// For low frequency ticker, we use the mselect to reduce the number of goroutines. | ||
const lowFrequencyThreshold = 5 * time.Second | ||
|
||
var ( | ||
mselOnce sync.Once | ||
msel mselect.ManySelect | ||
) | ||
|
||
func initManySelect() { | ||
mselOnce.Do(func() { msel = mselect.New() }) | ||
} | ||
|
||
type callbackTicker interface { | ||
Stop() | ||
} | ||
|
||
func newCallbackTicker(d time.Duration, callback func()) callbackTicker { | ||
if d < lowFrequencyThreshold { | ||
return newStdTicker(d, callback) | ||
} | ||
return newManySelectTicker(d, callback) | ||
} | ||
|
||
type stdTickerImpl struct { | ||
ticker *time.Ticker | ||
close chan struct{} | ||
callback func() | ||
} | ||
|
||
func newStdTicker(d time.Duration, callback func()) *stdTickerImpl { | ||
impl := &stdTickerImpl{ | ||
ticker: time.NewTicker(d), | ||
close: make(chan struct{}), | ||
callback: callback, | ||
} | ||
go impl.run() | ||
return impl | ||
} | ||
|
||
func (t *stdTickerImpl) Stop() { | ||
t.ticker.Stop() | ||
close(t.close) | ||
} | ||
|
||
func (t *stdTickerImpl) run() { | ||
for { | ||
select { | ||
case <-t.ticker.C: | ||
t.callback() | ||
case <-t.close: | ||
return | ||
} | ||
} | ||
} | ||
|
||
type manySelectTickerImpl struct { | ||
ticker *time.Ticker | ||
task *mselect.Task | ||
} | ||
|
||
func newManySelectTicker(d time.Duration, asyncCallback func()) *manySelectTickerImpl { | ||
initManySelect() | ||
ticker := time.NewTicker(d) | ||
task := mselect.NewTask(ticker.C, nil, | ||
func(_ time.Time, ok bool) { | ||
if ok { | ||
asyncCallback() | ||
} | ||
}) | ||
msel.Add(task) | ||
impl := &manySelectTickerImpl{ | ||
ticker: ticker, | ||
task: task, | ||
} | ||
return impl | ||
} | ||
|
||
func (t *manySelectTickerImpl) Stop() { | ||
t.ticker.Stop() | ||
msel.Delete(t.task) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package singleflight | ||
|
||
import ( | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCallbackTicker(t *testing.T) { | ||
ticker1 := newCallbackTicker(time.Second, func() {}) | ||
assert.IsType(t, &stdTickerImpl{}, ticker1) | ||
|
||
ticker2 := newManySelectTicker(time.Second, func() {}) | ||
assert.IsType(t, &manySelectTickerImpl{}, ticker2) | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
ticker1.Stop() | ||
ticker2.Stop() | ||
} | ||
|
||
func TestStdTickerImpl(t *testing.T) { | ||
var count int32 | ||
ticker := newStdTicker(100*time.Millisecond, func() { | ||
atomic.AddInt32(&count, 1) | ||
}) | ||
|
||
time.Sleep(1100 * time.Millisecond) | ||
ticker.Stop() | ||
n1 := atomic.LoadInt32(&count) | ||
assert.True(t, n1 >= 9 && n1 <= 11) | ||
|
||
time.Sleep(300 * time.Millisecond) | ||
n2 := atomic.LoadInt32(&count) | ||
assert.True(t, n2 >= 9 && n2 <= 11) | ||
} | ||
|
||
func TestManySelectTickerImpl(t *testing.T) { | ||
var count int32 | ||
ticker := newManySelectTicker(100*time.Millisecond, func() { | ||
atomic.AddInt32(&count, 1) | ||
}) | ||
|
||
time.Sleep(1100 * time.Millisecond) | ||
ticker.Stop() | ||
n1 := atomic.LoadInt32(&count) | ||
assert.True(t, n1 >= 9 && n1 <= 11) | ||
|
||
time.Sleep(300 * time.Millisecond) | ||
n2 := atomic.LoadInt32(&count) | ||
assert.True(t, n2 >= 9 && n2 <= 11) | ||
} |