124 lines
2.6 KiB
Go
124 lines
2.6 KiB
Go
package webhook
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var retrySchedule = []time.Duration{
|
|
30 * time.Second,
|
|
2 * time.Minute,
|
|
10 * time.Minute,
|
|
1 * time.Hour,
|
|
6 * time.Hour,
|
|
}
|
|
|
|
type Worker struct {
|
|
repo *Repo
|
|
url string
|
|
secret string
|
|
timeout time.Duration
|
|
maxRetries int
|
|
hc *http.Client
|
|
}
|
|
|
|
func NewWorker(repo *Repo, url, secret string, timeoutSecs, maxRetries int) *Worker {
|
|
if maxRetries <= 0 {
|
|
maxRetries = len(retrySchedule)
|
|
}
|
|
return &Worker{
|
|
repo: repo,
|
|
url: url,
|
|
secret: secret,
|
|
timeout: time.Duration(timeoutSecs) * time.Second,
|
|
maxRetries: maxRetries,
|
|
hc: &http.Client{Timeout: time.Duration(timeoutSecs) * time.Second},
|
|
}
|
|
}
|
|
|
|
func (w *Worker) Run(ctx context.Context) {
|
|
if w.url == "" {
|
|
<-ctx.Done()
|
|
return
|
|
}
|
|
t := time.NewTicker(1 * time.Second)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-t.C:
|
|
w.tick(ctx)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (w *Worker) tick(ctx context.Context) {
|
|
items, err := w.repo.Claim(ctx, 5)
|
|
if err != nil {
|
|
slog.Error("webhook claim", "err", err)
|
|
return
|
|
}
|
|
if len(items) == 0 {
|
|
return
|
|
}
|
|
var wg sync.WaitGroup
|
|
for _, it := range items {
|
|
wg.Add(1)
|
|
go func(it *OutboxItem) {
|
|
defer wg.Done()
|
|
w.deliver(ctx, it)
|
|
}(it)
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
func (w *Worker) deliver(ctx context.Context, it *OutboxItem) {
|
|
body := []byte(it.Payload)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, w.url, bytes.NewReader(body))
|
|
if err != nil {
|
|
w.handleErr(ctx, it, err)
|
|
return
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("User-Agent", "nip05api/1.0")
|
|
req.Header.Set("X-Webhook-Event", string(it.EventType))
|
|
if sig := Sign(w.secret, body); sig != "" {
|
|
req.Header.Set("X-Webhook-Signature", sig)
|
|
}
|
|
|
|
resp, err := w.hc.Do(req)
|
|
if err != nil {
|
|
w.handleErr(ctx, it, err)
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode/100 == 2 {
|
|
_ = w.repo.MarkDelivered(ctx, it.ID)
|
|
slog.Info("webhook delivered", "event", it.EventType, "id", it.ID)
|
|
return
|
|
}
|
|
w.handleErr(ctx, it, fmt.Errorf("status %d", resp.StatusCode))
|
|
}
|
|
|
|
func (w *Worker) handleErr(ctx context.Context, it *OutboxItem, err error) {
|
|
attempts := it.Attempts + 1
|
|
if attempts >= w.maxRetries {
|
|
_ = w.repo.MarkDead(ctx, it.ID, err.Error())
|
|
slog.Error("webhook dead", "id", it.ID, "err", err)
|
|
return
|
|
}
|
|
idx := attempts - 1
|
|
if idx >= len(retrySchedule) {
|
|
idx = len(retrySchedule) - 1
|
|
}
|
|
next := time.Now().UTC().Add(retrySchedule[idx])
|
|
_ = w.repo.MarkRetry(ctx, it.ID, attempts, next, err.Error())
|
|
slog.Warn("webhook retry", "id", it.ID, "attempts", attempts, "next", next, "err", err)
|
|
}
|