natsbot

NATS bot
git clone https://git.instinctive.eu/natsbot.git
Log | Files | Refs | README | LICENSE

commit 106be77ec3782d851d56c93b5b3de0e05ba54d2e
parent 5542a86c1a3bc1d99bb1dd8aa9893c95593ff71d
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Thu,  4 Sep 2025 17:36:04 +0000

Invalid subscriptions are cleaned
Diffstat:
Mnatsbot.go | 19+++++++++++++++++++
1 file changed, 19 insertions(+), 0 deletions(-)

diff --git a/natsbot.go b/natsbot.go @@ -34,6 +34,7 @@ type NatsBot interface { func Loop(cb NatsBot, mainScript string, capacity int) { msgChan := make(chan *nats.Msg, capacity) + toClean := make(map[*nats.Subscription]bool) L := lua.NewState() defer L.Close() @@ -65,11 +66,29 @@ func Loop(cb NatsBot, mainScript string, capacity int) { processMsg(L, msg) + if !msg.Sub.IsValid() { + toClean[msg.Sub] = true + } + case <-timer.C: } runTimers(L, timer) + if len(msgChan) == 0 { + for s := range toClean { + if !s.IsValid() { + log.Printf("Pruning subscription %q", s.Subject) + tbl, idx := stateSubsTable(L) + L.RawSetInt(tbl, idx[s], lua.LNil) + delete(idx, s) + } else { + log.Printf("Subscription %q is still valid", s.Subject) + } + } + toClean = make(map[*nats.Subscription]bool) + } + if tableWithIndexIsEmpty(stateConnTable(L)) && tableIsEmpty(stateTimerTable(L)) { break }