From 88096fbd847d1b6b370f802744f6d552daee7c38 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 28 Jul 2025 22:06:12 -0300 Subject: [PATCH] khatru: allow disabling expiration manager. --- khatru/expiration.go | 12 ++++++++++++ khatru/relay.go | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/khatru/expiration.go b/khatru/expiration.go index e9ca724..89f6b80 100644 --- a/khatru/expiration.go +++ b/khatru/expiration.go @@ -39,6 +39,8 @@ type expirationManager struct { relay *Relay interval time.Duration initialScanDone bool + kill chan struct{} // used for manually killing this + killonce *sync.Once } func newExpirationManager(relay *Relay) *expirationManager { @@ -46,9 +48,17 @@ func newExpirationManager(relay *Relay) *expirationManager { events: make(expiringEventHeap, 0), relay: relay, interval: time.Hour, + kill: make(chan struct{}), + killonce: &sync.Once{}, } } +func (em *expirationManager) stop() { + em.killonce.Do(func() { + close(em.kill) + }) +} + func (em *expirationManager) start(ctx context.Context) { ticker := time.NewTicker(em.interval) defer ticker.Stop() @@ -57,6 +67,8 @@ func (em *expirationManager) start(ctx context.Context) { select { case <-ctx.Done(): return + case <-em.kill: + return case <-ticker.C: if !em.initialScanDone { em.initialScan(ctx) diff --git a/khatru/relay.go b/khatru/relay.go index 74b53a6..ab6af4e 100644 --- a/khatru/relay.go +++ b/khatru/relay.go @@ -6,6 +6,7 @@ import ( "log" "net/http" "os" + "slices" "strconv" "strings" "sync" @@ -168,3 +169,12 @@ func (rl *Relay) getBaseURL(r *http.Request) string { } return proto + "://" + host } + +func (rl *Relay) DisableExpiration() { + rl.expirationManager.stop() + idx := slices.Index(rl.Info.SupportedNIPs, 40) + if idx != -1 { + rl.Info.SupportedNIPs[idx] = rl.Info.SupportedNIPs[len(rl.Info.SupportedNIPs)-1] + rl.Info.SupportedNIPs = rl.Info.SupportedNIPs[0 : len(rl.Info.SupportedNIPs)-1] + } +}