Skip to content

Commit

Permalink
Fix cached RouterConfig response.
Browse files Browse the repository at this point in the history
Because the RouterConfig struct was cached, it would return the same
MuxTime on every response.
  • Loading branch information
brocaar committed Apr 17, 2023
1 parent b4fe733 commit 6b202d1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
38 changes: 22 additions & 16 deletions internal/backend/basicstation/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ type Backend struct {
gatewayStatsFunc func(*gw.GatewayStats)
rawPacketForwarderEventFunc func(*gw.RawPacketForwarderEvent)

band band.Band
region band.Name
netIDs []lorawan.NetID
joinEUIs [][2]lorawan.EUI64
frequencyMin uint32
frequencyMax uint32
routerConfig structs.RouterConfig
band band.Band
region band.Name
netIDs []lorawan.NetID
joinEUIs [][2]lorawan.EUI64
frequencyMin uint32
frequencyMax uint32
concentrators []config.BasicStationConcentrator

// Cache to store diid to UUIDs.
diidCache *cache.Cache
Expand All @@ -94,9 +94,10 @@ func NewBackend(conf config.Config) (*Backend, error) {
readTimeout: conf.Backend.BasicStation.ReadTimeout,
writeTimeout: conf.Backend.BasicStation.WriteTimeout,

region: band.Name(conf.Backend.BasicStation.Region),
frequencyMin: conf.Backend.BasicStation.FrequencyMin,
frequencyMax: conf.Backend.BasicStation.FrequencyMax,
region: band.Name(conf.Backend.BasicStation.Region),
frequencyMin: conf.Backend.BasicStation.FrequencyMin,
frequencyMax: conf.Backend.BasicStation.FrequencyMax,
concentrators: config.C.Backend.BasicStation.Concentrators,

diidCache: cache.New(time.Minute, time.Minute),
}
Expand Down Expand Up @@ -127,11 +128,6 @@ func NewBackend(conf config.Config) (*Backend, error) {
return nil, errors.Wrap(err, "get band config error")
}

b.routerConfig, err = structs.GetRouterConfig(b.region, b.netIDs, b.joinEUIs, b.frequencyMin, b.frequencyMax, conf.Backend.BasicStation.Concentrators)
if err != nil {
return nil, errors.Wrap(err, "get router config error")
}

mux := http.NewServeMux()
mux.HandleFunc("/router-info", func(w http.ResponseWriter, r *http.Request) {
b.websocketWrap(b.handleRouterInfo, w, r)
Expand Down Expand Up @@ -295,6 +291,10 @@ func (b *Backend) Stop() error {
return b.ln.Close()
}

func (b *Backend) getRouterConfig() (structs.RouterConfig, error) {
return structs.GetRouterConfig(b.region, b.netIDs, b.joinEUIs, b.frequencyMin, b.frequencyMax, b.concentrators)
}

func (b *Backend) handleRouterInfo(r *http.Request, conn *connection) {
websocketReceiveCounter("router_info").Inc()
var req structs.RouterInfoRequest
Expand Down Expand Up @@ -553,8 +553,14 @@ func (b *Backend) handleVersion(gatewayID lorawan.EUI64, pl structs.Version) {
// "features": pl.Features,
}).Info("backend/basicstation: gateway version received")

routerConfig, err := b.getRouterConfig()
if err != nil {
log.WithError(err).Error("backend/basicstation: get router config error")
return
}

websocketSendCounter("router_config").Inc()
if err := b.sendToGateway(gatewayID, b.routerConfig); err != nil {
if err := b.sendToGateway(gatewayID, routerConfig); err != nil {
log.WithError(err).Error("backend/basicstation: send to gateway error")
return
}
Expand Down
8 changes: 4 additions & 4 deletions internal/backend/basicstation/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ func (ts *BackendTestSuite) TestRouterInfo() {

func (ts *BackendTestSuite) TestVersion() {
assert := require.New(ts.T())
ts.backend.routerConfig = structs.RouterConfig{
MessageType: structs.RouterConfigMessage,
}

ver := structs.Version{
MessageType: structs.VersionMessage,
Expand All @@ -125,7 +122,10 @@ func (ts *BackendTestSuite) TestVersion() {
var routerConfig structs.RouterConfig
assert.NoError(ts.wsClient.ReadJSON(&routerConfig))

assert.Equal(ts.backend.routerConfig, routerConfig)
routerConfig, err := ts.backend.getRouterConfig()
assert.NoError(err)

assert.Equal(routerConfig, routerConfig)
}

func (ts *BackendTestSuite) TestUplinkDataFrame() {
Expand Down

0 comments on commit 6b202d1

Please sign in to comment.