-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ARCO-183: put problematic callback receivers to quarantine
- Loading branch information
1 parent
21cc7a5
commit 3f6a3c1
Showing
25 changed files
with
1,957 additions
and
360 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
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,122 @@ | ||
package callbacker | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"sync" | ||
"time" | ||
|
||
"github.com/bitcoin-sv/arc/internal/callbacker/store" | ||
) | ||
|
||
type BackgroundWorkers struct { | ||
s store.CallbackerStore | ||
l *slog.Logger | ||
d *CallbackDispatcher | ||
|
||
workersWg sync.WaitGroup | ||
ctx context.Context | ||
cancelAll func() | ||
} | ||
|
||
func NewBackgroundWorkers(store store.CallbackerStore, dispatcher *CallbackDispatcher, logger *slog.Logger) *BackgroundWorkers { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
return &BackgroundWorkers{ | ||
s: store, | ||
d: dispatcher, | ||
l: logger.With(slog.String("module", "background workers")), | ||
|
||
ctx: ctx, | ||
cancelAll: cancel, | ||
} | ||
} | ||
|
||
func (w *BackgroundWorkers) StartCallbackStoreCleanup(interval, olderThanDuration time.Duration) { | ||
w.workersWg.Add(1) | ||
go w.pruneCallbacks(interval, olderThanDuration) | ||
} | ||
|
||
func (w *BackgroundWorkers) StartQuarantineCallbacksDispatch(interval time.Duration) { | ||
w.workersWg.Add(1) | ||
go w.dispatchQuarantineCallbacks(interval) | ||
} | ||
|
||
func (w *BackgroundWorkers) GracefulStop() { | ||
w.cancelAll() | ||
w.workersWg.Wait() | ||
} | ||
|
||
func (w *BackgroundWorkers) pruneCallbacks(interval, olderThanDuration time.Duration) { | ||
ctx := context.Background() | ||
t := time.NewTicker(interval) | ||
|
||
for { | ||
select { | ||
case <-t.C: | ||
n := time.Now() | ||
midnight := time.Date(n.Year(), n.Month(), n.Day(), 0, 0, 0, 0, time.UTC) | ||
olderThan := midnight.Add(-1 * olderThanDuration) | ||
|
||
err := w.s.DeleteFailedOlderThan(ctx, olderThan) | ||
if err != nil { | ||
w.l.Error("failed to delete old callbacks in quarantine", slog.String("err", err.Error())) | ||
} | ||
|
||
case <-w.ctx.Done(): | ||
w.workersWg.Done() | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (w *BackgroundWorkers) dispatchQuarantineCallbacks(interval time.Duration) { | ||
const batchSize = 100 | ||
|
||
ctx := context.Background() | ||
t := time.NewTicker(interval) | ||
|
||
for { | ||
select { | ||
case <-t.C: | ||
callbacks, err := w.s.PopFailedMany(ctx, time.Now(), batchSize) | ||
if err != nil { | ||
w.l.Error("reading callbacks from store failed", slog.String("err", err.Error())) | ||
continue | ||
} | ||
|
||
if len(callbacks) == 0 { | ||
continue | ||
} | ||
|
||
for _, c := range callbacks { | ||
w.d.Dispatch(c.Url, toCallbackEntry(c)) | ||
} | ||
|
||
case <-w.ctx.Done(): | ||
w.workersWg.Done() | ||
return | ||
} | ||
} | ||
} | ||
|
||
func toCallbackEntry(dto *store.CallbackData) *CallbackEntry { | ||
d := &Callback{ | ||
Timestamp: dto.Timestamp, | ||
|
||
CompetingTxs: dto.CompetingTxs, | ||
TxID: dto.TxID, | ||
TxStatus: dto.TxStatus, | ||
ExtraInfo: dto.ExtraInfo, | ||
MerklePath: dto.MerklePath, | ||
|
||
BlockHash: dto.BlockHash, | ||
BlockHeight: dto.BlockHeight, | ||
} | ||
|
||
return &CallbackEntry{ | ||
Token: dto.Token, | ||
Data: d, | ||
quarantineUntil: dto.QuarantineUntil, | ||
} | ||
} |
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
Oops, something went wrong.