From 36361ee3a2cdc8c46e391202906ad05b219a3e40 Mon Sep 17 00:00:00 2001 From: Janosch van Rosmalen Date: Wed, 12 Jun 2024 14:18:15 +0200 Subject: [PATCH] Fixes #236 Class C timeout --- cmd/chirpstack-gateway-bridge/cmd/configfile.go | 3 +++ cmd/chirpstack-gateway-bridge/cmd/root.go | 1 + internal/backend/semtechudp/backend.go | 3 ++- internal/backend/semtechudp/registry.go | 8 +++++--- internal/config/config.go | 7 ++++--- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cmd/chirpstack-gateway-bridge/cmd/configfile.go b/cmd/chirpstack-gateway-bridge/cmd/configfile.go index 94d85a98..981e2e84 100644 --- a/cmd/chirpstack-gateway-bridge/cmd/configfile.go +++ b/cmd/chirpstack-gateway-bridge/cmd/configfile.go @@ -88,6 +88,9 @@ type="{{ .Backend.Type }}" # the time would otherwise be unset. fake_rx_time={{ .Backend.SemtechUDP.FakeRxTime }} + # Cleanup duration + # Cleaup duration for class C devices + cleanup_duration={{ .Backend.SemtechUDP.CleanupDuration }} # ChirpStack Concentratord backend. [backend.concentratord] diff --git a/cmd/chirpstack-gateway-bridge/cmd/root.go b/cmd/chirpstack-gateway-bridge/cmd/root.go index 5ad16bde..99184d60 100644 --- a/cmd/chirpstack-gateway-bridge/cmd/root.go +++ b/cmd/chirpstack-gateway-bridge/cmd/root.go @@ -39,6 +39,7 @@ func init() { viper.SetDefault("general.log_level", 4) viper.SetDefault("backend.type", "semtech_udp") viper.SetDefault("backend.semtech_udp.udp_bind", "0.0.0.0:1700") + viper.SetDefault("backend.semtech_udp.cleanup_duration", -1) viper.SetDefault("backend.concentratord.crc_check", true) viper.SetDefault("backend.concentratord.event_url", "ipc:///tmp/concentratord_event") diff --git a/internal/backend/semtechudp/backend.go b/internal/backend/semtechudp/backend.go index 14128a68..41bb3b05 100644 --- a/internal/backend/semtechudp/backend.go +++ b/internal/backend/semtechudp/backend.go @@ -67,7 +67,8 @@ func NewBackend(conf config.Config) (*Backend, error) { conn: conn, udpSendChan: make(chan udpPacket), gateways: gateways{ - gateways: make(map[lorawan.EUI64]gateway), + gateways: make(map[lorawan.EUI64]gateway), + cleanupDuration: time.Duration(conf.Backend.SemtechUDP.CleanupDuration), }, fakeRxTime: conf.Backend.SemtechUDP.FakeRxTime, skipCRCCheck: conf.Backend.SemtechUDP.SkipCRCCheck, diff --git a/internal/backend/semtechudp/registry.go b/internal/backend/semtechudp/registry.go index d1e91b37..c3c82d4e 100644 --- a/internal/backend/semtechudp/registry.go +++ b/internal/backend/semtechudp/registry.go @@ -18,7 +18,8 @@ var ( // gatewayCleanupDuration contains the duration after which the gateway is // cleaned up from the registry after no activity -var gatewayCleanupDuration = -1 * time.Minute +// This value can be set in config [backend.SemtechUDP.cleanup_duration] +// var gatewayCleanupDuration = time.Duration(config.C.Backend.SemtechUDP.CleanupDuration) + (-1 * time.Minute) // gateway contains a connection and meta-data for a gateway connection. type gateway struct { @@ -31,7 +32,8 @@ type gateway struct { // gateways contains the gateways registry. type gateways struct { sync.RWMutex - gateways map[lorawan.EUI64]gateway + gateways map[lorawan.EUI64]gateway + cleanupDuration time.Duration subscribeEventFunc func(events.Subscribe) } @@ -82,7 +84,7 @@ func (c *gateways) cleanup() error { defer c.Unlock() for gatewayID := range c.gateways { - if c.gateways[gatewayID].lastSeen.Before(time.Now().Add(gatewayCleanupDuration)) { + if c.gateways[gatewayID].lastSeen.Before(time.Now().Add(c.cleanupDuration)) { disconnectCounter().Inc() if c.subscribeEventFunc != nil { diff --git a/internal/config/config.go b/internal/config/config.go index 70d42f2a..d34aa777 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -21,9 +21,10 @@ type Config struct { Type string `mapstructure:"type"` SemtechUDP struct { - UDPBind string `mapstructure:"udp_bind"` - SkipCRCCheck bool `mapstructure:"skip_crc_check"` - FakeRxTime bool `mapstructure:"fake_rx_time"` + UDPBind string `mapstructure:"udp_bind"` + SkipCRCCheck bool `mapstructure:"skip_crc_check"` + FakeRxTime bool `mapstructure:"fake_rx_time"` + CleanupDuration int `mapstructure:"cleanup_duration"` } `mapstructure:"semtech_udp"` BasicStation struct {