mirror of
https://github.com/ChuckNorrison/LightningTipBot.git
synced 2026-08-13 12:33:14 +02:00
* satdress rebase * check config * satdress rebase * check config * check for invoices * return error * Update inline photo v5 (#332) * update url * round logo * resize Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com> * new logo (#333) Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com> * help (#334) Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com> * add file (#335) Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com> * markdown (#336) Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com> * satdress rebase * check config * satdress rebase * check for invoices * return error * satdress works * add lnbits * timeout * error checking * small lnd * errors * button fix Co-authored-by: LightningTipBot <88730856+LightningTipBot@users.noreply.github.com>
63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package runtime
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// var retryMap cmap.ConcurrentMap
|
|
|
|
// func init() {
|
|
// retryMap = cmap.New()
|
|
// }
|
|
|
|
// ResettableFunctionTicker will reset the user state as soon as tick is delivered.
|
|
type FunctionRetry struct {
|
|
Ticker *time.Ticker
|
|
duration time.Duration
|
|
ctx context.Context
|
|
name string
|
|
}
|
|
|
|
type FunctionRetryOption func(*FunctionRetry)
|
|
|
|
func WithRetryDuration(d time.Duration) FunctionRetryOption {
|
|
return func(a *FunctionRetry) {
|
|
a.duration = d
|
|
}
|
|
}
|
|
func NewRetryTicker(ctx context.Context, name string, option ...FunctionRetryOption) *FunctionRetry {
|
|
t := &FunctionRetry{
|
|
name: name,
|
|
ctx: ctx,
|
|
}
|
|
for _, opt := range option {
|
|
opt(t)
|
|
}
|
|
if t.duration == 0 {
|
|
t.duration = defaultTickerCoolDown
|
|
}
|
|
t.Ticker = time.NewTicker(t.duration)
|
|
return t
|
|
}
|
|
|
|
func (t *FunctionRetry) Do(f func(), cancel_f func(), deadline_f func()) {
|
|
tickerMap.Set(t.name, t)
|
|
go func() {
|
|
for {
|
|
select {
|
|
case <-t.Ticker.C:
|
|
// ticker delivered signal. do function f
|
|
f()
|
|
case <-t.ctx.Done():
|
|
if t.ctx.Err() == context.DeadlineExceeded {
|
|
deadline_f()
|
|
}
|
|
if t.ctx.Err() == context.Canceled {
|
|
cancel_f()
|
|
}
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|