Clash.Meta/common/singledo/singledo.go

65 lines
1.1 KiB
Go
Raw Normal View History

package singledo
import (
"sync"
"time"
)
2022-04-24 02:07:57 +08:00
type call[T any] struct {
wg sync.WaitGroup
2022-04-24 02:07:57 +08:00
val T
err error
}
2022-04-24 02:07:57 +08:00
type Single[T any] struct {
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]
}
2022-04-24 02:07:57 +08:00
type Result[T any] struct {
Val T
Err error
}
2020-10-14 19:56:02 +08:00
// Do single.Do likes sync.singleFlight
//lint:ignore ST1008 it 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) {
s.mux.Lock()
2019-12-10 16:26:15 +08:00
now := time.Now()
if now.Before(s.last.Add(s.wait)) {
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 {
s.mux.Unlock()
2022-04-24 02:07:57 +08:00
callM.wg.Wait()
return callM.val, callM.err, true
}
2022-04-24 02:07:57 +08:00
callM := &call[T]{}
callM.wg.Add(1)
s.call = callM
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()
s.call = nil
2022-04-24 02:07:57 +08:00
s.result = &Result[T]{callM.val, callM.err}
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
}
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}
}