|
6 | 6 | "log" |
7 | 7 | "os" |
8 | 8 | "os/signal" |
| 9 | + "sync" |
9 | 10 | "time" |
10 | 11 | ) |
11 | 12 |
|
@@ -42,3 +43,104 @@ func Notify(sig ...os.Signal) chan os.Signal { |
42 | 43 | signal.Notify(terminate, sig...) |
43 | 44 | return terminate |
44 | 45 | } |
| 46 | + |
| 47 | +func NewDelayOnce(delay time.Duration, timeout time.Duration) *DelayOnce { |
| 48 | + if timeout <= delay { |
| 49 | + panic(`timeout must be greater than delay`) |
| 50 | + } |
| 51 | + return &DelayOnce{ |
| 52 | + mp: sync.Map{}, |
| 53 | + delay: delay, |
| 54 | + timeout: timeout, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// DelayOnce 触发之后延迟一定的时间后再执行。如果在延迟处理的时间段内再次触发,则延迟时间基于此处触发时间顺延 |
| 59 | +// d := NewDelayOnce(time.Second*5, time.Hour) |
| 60 | +// ctx := context.TODO() |
| 61 | +// for i:=0; i<10; i++ { |
| 62 | +// d.Do(ctx, `key`,func() error { return nil }) |
| 63 | +// } |
| 64 | +type DelayOnce struct { |
| 65 | + mp sync.Map |
| 66 | + delay time.Duration |
| 67 | + timeout time.Duration |
| 68 | +} |
| 69 | + |
| 70 | +type eventSession struct { |
| 71 | + cancel context.CancelFunc |
| 72 | + time time.Time |
| 73 | + mutex sync.RWMutex |
| 74 | +} |
| 75 | + |
| 76 | +func (e *eventSession) Renew(t time.Time) { |
| 77 | + e.mutex.Lock() |
| 78 | + e.time = t |
| 79 | + e.mutex.Unlock() |
| 80 | +} |
| 81 | + |
| 82 | +func (e *eventSession) Time() time.Time { |
| 83 | + e.mutex.RLock() |
| 84 | + t := e.time |
| 85 | + e.mutex.RUnlock() |
| 86 | + return t |
| 87 | +} |
| 88 | + |
| 89 | +func (d *DelayOnce) checkAndStore(parentCtx context.Context, key string) (exit bool, ctx context.Context) { |
| 90 | + v, loaded := d.mp.Load(key) |
| 91 | + if loaded { |
| 92 | + session := v.(*eventSession) |
| 93 | + if time.Since(session.Time()) < d.timeout { // 超过 d.timeout 后重新处理,d.timeout 内记录当前时间 |
| 94 | + session.Renew(time.Now()) |
| 95 | + d.mp.Store(key, session) |
| 96 | + return true, nil |
| 97 | + } |
| 98 | + session.cancel() |
| 99 | + } |
| 100 | + var cancel context.CancelFunc |
| 101 | + ctx, cancel = context.WithCancel(parentCtx) |
| 102 | + d.mp.Store(key, &eventSession{ |
| 103 | + cancel: cancel, |
| 104 | + time: time.Now(), |
| 105 | + }) |
| 106 | + return false, ctx |
| 107 | +} |
| 108 | + |
| 109 | +func (d *DelayOnce) Do(parentCtx context.Context, key string, f func() error) (isNew bool) { |
| 110 | + exit, ctx := d.checkAndStore(parentCtx, key) |
| 111 | + if exit { |
| 112 | + return false |
| 113 | + } |
| 114 | + go func(key string) { |
| 115 | + for { |
| 116 | + t := time.NewTicker(time.Second) |
| 117 | + defer t.Stop() |
| 118 | + select { |
| 119 | + case <-ctx.Done(): |
| 120 | + return |
| 121 | + case <-t.C: |
| 122 | + if err := d.exec(key, f); err != nil { |
| 123 | + log.Println(key+`:`, err) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + }(key) |
| 128 | + return true |
| 129 | +} |
| 130 | + |
| 131 | +func (d *DelayOnce) exec(key string, f func() error) (err error) { |
| 132 | + v, ok := d.mp.Load(key) |
| 133 | + if !ok { |
| 134 | + return |
| 135 | + } |
| 136 | + session := v.(*eventSession) |
| 137 | + if time.Since(session.Time()) > d.delay { // 时间超过d.delay才触发 |
| 138 | + err = f() |
| 139 | + if err != nil { |
| 140 | + return |
| 141 | + } |
| 142 | + d.mp.Delete(key) |
| 143 | + session.cancel() |
| 144 | + } |
| 145 | + return |
| 146 | +} |
0 commit comments