2019-12-10 15:04:22 +08:00
|
|
|
package singledo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-04-24 02:07:57 +08:00
|
|
|
type call[T any] struct {
|
2019-12-10 15:04:22 +08:00
|
|
|
wg sync.WaitGroup
|
2022-04-24 02:07:57 +08:00
|
|
|
val T
|
2019-12-10 15:04:22 +08:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2022-04-24 02:07:57 +08:00
|
|
|
type Single[T any] struct {
|
2019-12-10 15:04:22 +08:00
|
|
|
mux sync.Mutex
|
2019-12-10 16:26:15 +08:00
|
|
|
last time.Time
|
|
|
|
wait time.Duration
|
2022-04-24 02:07:57 +08:00
|
|
|
call *call[T]
|
|
|
|
result *Result[T]
|
2019-12-10 15:04:22 +08:00
|
|
|
}
|
|
|
|
|
2022-04-24 02:07:57 +08:00
|
|
|
type Result[T any] struct {
|
|
|
|
Val T
|
2019-12-10 15:04:22 +08:00
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2020-10-14 19:56:02 +08:00
|
|
|
// Do single.Do likes sync.singleFlight
|
2022-04-24 02:07:57 +08:00
|
|
|
func (s *Single[T]) Do(fn func() (T, error)) (v T, err error, shared bool) {
|
2019-12-10 15:04:22 +08:00
|
|
|
s.mux.Lock()
|
2019-12-10 16:26:15 +08:00
|
|
|
now := time.Now()
|
|
|
|
if now.Before(s.last.Add(s.wait)) {
|
2019-12-10 15:04:22 +08:00
|
|
|
s.mux.Unlock()
|
|
|
|
return s.result.Val, s.result.Err, true
|
|
|
|
}
|
|
|
|
|
2022-04-24 02:07:57 +08:00
|
|
|
if callM := s.call; callM != nil {
|
2019-12-10 15:04:22 +08:00
|
|
|
s.mux.Unlock()
|
2022-04-24 02:07:57 +08:00
|
|
|
callM.wg.Wait()
|
|
|
|
return callM.val, callM.err, true
|
2019-12-10 15:04:22 +08:00
|
|
|
}
|
|
|
|
|
2022-04-24 02:07:57 +08:00
|
|
|
callM := &call[T]{}
|
|
|
|
callM.wg.Add(1)
|
|
|
|
s.call = callM
|
2019-12-10 15:04:22 +08:00
|
|
|
s.mux.Unlock()
|
2022-04-24 02:07:57 +08:00
|
|
|
callM.val, callM.err = fn()
|
|
|
|
callM.wg.Done()
|
2020-07-18 20:56:13 +08:00
|
|
|
|
|
|
|
s.mux.Lock()
|
2019-12-10 15:04:22 +08:00
|
|
|
s.call = nil
|
2022-04-24 02:07:57 +08:00
|
|
|
s.result = &Result[T]{callM.val, callM.err}
|
2019-12-10 15:04:22 +08:00
|
|
|
s.last = now
|
2020-07-18 20:56:13 +08:00
|
|
|
s.mux.Unlock()
|
2022-04-24 02:07:57 +08:00
|
|
|
return callM.val, callM.err, false
|
2019-12-10 15:04:22 +08:00
|
|
|
}
|
|
|
|
|
2022-04-24 02:07:57 +08:00
|
|
|
func (s *Single[T]) Reset() {
|
2020-04-30 20:13:27 +08:00
|
|
|
s.last = time.Time{}
|
|
|
|
}
|
|
|
|
|
2022-04-24 02:07:57 +08:00
|
|
|
func NewSingle[T any](wait time.Duration) *Single[T] {
|
|
|
|
return &Single[T]{wait: wait}
|
2019-12-10 15:04:22 +08:00
|
|
|
}
|