Refactor: centralize port/protocol mapping and fix concurrency patterns#134
Merged
Conversation
- Extract duplicated port→protocol switch (4 copies × ~45 lines) into utils.ResolvePortProtocol() and utils.ProtoPorts — net 169 lines removed. - Fix stats collector busy loop: replace nested select/default with ticker-driven pattern (time.NewTicker + defer Stop). - Fix template retransmission: replace fragile takeAction flag arithmetic with dedicated time.Ticker firing every templateInterval seconds. - Add 5-second shutdown timeout to web server (was context.Background()). - Fix ExtendedProfile comment: 14→15 fields to match actual template. - Update all test files to use centralized utils.* constants.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Centralizes port/protocol mapping into
utils/flow.goand eliminates duplicated switch statements across NetFlow and IPFIX flow generators. Also fixes several concurrency and resource management issues.Net result: 15 files changed, +171 / -340 lines (net -169)
Changes
Port/Protocol Centralization
utils.ProtoPorts— single source of truth for well-known port listutils.ResolvePortProtocol()— replaces 4 identical switch statements (~170 lines removed)netflow/flow.go,netflow/profile_extended.go,netflow/profile_minimal.go, andipfix/ipfix.goutils.*constants directlyConcurrency & Resource Management Fixes
barrage/barrage.go: Replacedtime.Tick()(leaks ticker) withtime.NewTicker()+defer Stop(). Added proper template retransmission ticker with configurable interval.stats/collector.go: Sametime.Tick()→time.NewTicker()fix. Simplified select loop — removed nested select in default case.web/web.go:srv.Shutdown()now usescontext.WithTimeout(5s)instead ofcontext.Background()(which could block indefinitely).Bug Fixes
netflow/dataflowset.go: Fixed hardcodedprotoPorts[RandomNum(0, 12)]to uselen(protoPorts)— prevents out-of-bounds if ports list changes.Test Improvements
netflow/profile_roundtrip_test.go: Added version check in extended profile roundtrip test.utils.*constants.netflow/netflow_test.go.Verification
go build✅go vet ./...✅go test -v ./... -count 1✅ All passinggo test -race ./...✅ No races detectedgo mod tidy✅ Clean