From 4935f5a8ef6e535e1b5125f6eb016015dd8dff49 Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Sat, 22 Nov 2025 06:17:23 +0100 Subject: [PATCH] Refactor code: clear old OIDs in the Session before updating them --- src/ifmib/ifmib.go | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/ifmib/ifmib.go b/src/ifmib/ifmib.go index 25181ee..8a3f4a2 100644 --- a/src/ifmib/ifmib.go +++ b/src/ifmib/ifmib.go @@ -5,6 +5,7 @@ package ifmib import ( "fmt" "os" + "reflect" "sync" "time" @@ -161,14 +162,32 @@ func (m *InterfaceMIB) UpdateInterfaceDetails(details []vpp.InterfaceDetails) { logger.Debugf("Interface details updated for %d interfaces", len(details)) } +func (m *InterfaceMIB) clearHandler() { + // Use reflection to access and clear the private fields of ListHandler + // since it doesn't have a Clear() method + handlerValue := reflect.ValueOf(m.handler).Elem() + + oidsField := handlerValue.FieldByName("oids") + itemsField := handlerValue.FieldByName("items") + + if oidsField.IsValid() && oidsField.CanSet() { + oidsField.Set(reflect.Zero(oidsField.Type())) + } + + if itemsField.IsValid() && itemsField.CanSet() { + itemsField.Set(reflect.Zero(itemsField.Type())) + } +} + func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) { m.mutex.Lock() defer m.mutex.Unlock() logger.Debugf("Updating IF-MIB with %d interfaces", len(interfaceStats.Interfaces)) - // Clear existing entries - m.handler = &agentx.ListHandler{} + // Clear existing entries while preserving the handler reference + // Since go-agentx 0.3.0 binds handlers to sessions at creation time + m.clearHandler() m.stats = make(map[uint32]*api.InterfaceCounters) // Add new entries @@ -178,11 +197,7 @@ func (m *InterfaceMIB) UpdateStats(interfaceStats *api.InterfaceStats) { m.addInterfaceToMIB(&iface) } - // Note: With go-agentx 0.3.0, handlers are set during session creation and cannot be changed - if m.ifXTableSession != nil { - logger.Printf("Updated IF-MIB data for %d interfaces", len(m.stats)) - } - + logger.Printf("Updated IF-MIB data for %d interfaces", len(m.stats)) logger.Debugf("IF-MIB now contains %d interfaces", len(m.stats)) }