Skip to content

Refactor: centralize port/protocol mapping and fix concurrency patterns#134

Merged
dmabry merged 1 commit into
mainfrom
feature/code-quality-improvements
May 15, 2026
Merged

Refactor: centralize port/protocol mapping and fix concurrency patterns#134
dmabry merged 1 commit into
mainfrom
feature/code-quality-improvements

Conversation

@dmabry
Copy link
Copy Markdown
Owner

@dmabry dmabry commented May 15, 2026

Summary

Centralizes port/protocol mapping into utils/flow.go and 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

  • Added utils.ProtoPorts — single source of truth for well-known port list
  • Added utils.ResolvePortProtocol() — replaces 4 identical switch statements (~170 lines removed)
  • Removed duplicated port/protocol constant aliases from netflow/flow.go, netflow/profile_extended.go, netflow/profile_minimal.go, and ipfix/ipfix.go
  • Updated all callers and tests to use utils.* constants directly

Concurrency & Resource Management Fixes

  • barrage/barrage.go: Replaced time.Tick() (leaks ticker) with time.NewTicker() + defer Stop(). Added proper template retransmission ticker with configurable interval.
  • stats/collector.go: Same time.Tick()time.NewTicker() fix. Simplified select loop — removed nested select in default case.
  • web/web.go: srv.Shutdown() now uses context.WithTimeout(5s) instead of context.Background() (which could block indefinitely).

Bug Fixes

  • netflow/dataflowset.go: Fixed hardcoded protoPorts[RandomNum(0, 12)] to use len(protoPorts) — prevents out-of-bounds if ports list changes.

Test Improvements

  • netflow/profile_roundtrip_test.go: Added version check in extended profile roundtrip test.
  • Updated all test references from local aliases to utils.* constants.
  • Fixed import ordering (gofmt) in netflow/netflow_test.go.

Verification

  • go build
  • go vet ./...
  • go test -v ./... -count 1 ✅ All passing
  • go test -race ./... ✅ No races detected
  • go mod tidy ✅ Clean

- 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.
@dmabry dmabry merged commit 732fef0 into main May 15, 2026
3 checks passed
@dmabry dmabry deleted the feature/code-quality-improvements branch May 15, 2026 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant