refactor: replace mutable global maps, log functions, and embedded request/response state #7

Closed
opened 2026-07-21 17:20:17 +02:00 by heiko · 0 comments
Owner

Problem

Several mutable package globals and embedded fields complicate reasoning and testing:

  • main.go: TSAs map[string]bool combines configuration, enablement, and iteration policy in a mutable/nondeterministic map.
  • main.go: verbosef and debugf are mutable global functions changed by command execution.
  • main.go: timestamp embeds both *tspclient.Request and *tspclient.Response, promoting methods and creating ambiguity (Validate is already called through an explicit embedded field to avoid it).
  • internal/rfc822/message.go: exported mutable FieldSet can be changed by any importer and is unsafe under concurrent access.
  • internal/rfc822/parser.go: mutable package-global debug state.
  • main.go:init performs process-global logging setup, making command construction less isolated.

Checklist: #3 (avoid unnecessary init), #10 (embedding ambiguity), and #58 (mutable shared state/races).

Proposed fix

  • Replace the TSA map with an ordered []tsaConfig{Endpoint, Enabled} and helper functions for enabled endpoints.
  • Make request/response explicit named fields in timestamp and use keyed literals.
  • Put loggers/diagnostic functions in a command/application dependency struct instead of globals.
  • Make field-set expansion private and return copies; expose behavior through functions rather than a mutable map.
  • Remove parser-global debug state or inject tracing into parser construction.
  • Configure logging explicitly in main/root construction rather than init.

Acceptance criteria

  • Repeated command construction/execution is deterministic and race-safe.
  • No mutable exported field-set map remains.
  • timestamp has explicit request and response fields.
  • TSA ordering and enabled-count behavior are deterministic.
  • Parallel tests pass under the race detector.

Reviewed against 948b7a9 on master (Go 1.26.2).

## Problem Several mutable package globals and embedded fields complicate reasoning and testing: - `main.go`: `TSAs map[string]bool` combines configuration, enablement, and iteration policy in a mutable/nondeterministic map. - `main.go`: `verbosef` and `debugf` are mutable global functions changed by command execution. - `main.go`: `timestamp` embeds both `*tspclient.Request` and `*tspclient.Response`, promoting methods and creating ambiguity (`Validate` is already called through an explicit embedded field to avoid it). - `internal/rfc822/message.go`: exported mutable `FieldSet` can be changed by any importer and is unsafe under concurrent access. - `internal/rfc822/parser.go`: mutable package-global debug state. - `main.go:init` performs process-global logging setup, making command construction less isolated. Checklist: #3 (avoid unnecessary `init`), #10 (embedding ambiguity), and #58 (mutable shared state/races). ## Proposed fix - Replace the TSA map with an ordered `[]tsaConfig{Endpoint, Enabled}` and helper functions for enabled endpoints. - Make request/response explicit named fields in `timestamp` and use keyed literals. - Put loggers/diagnostic functions in a command/application dependency struct instead of globals. - Make field-set expansion private and return copies; expose behavior through functions rather than a mutable map. - Remove parser-global debug state or inject tracing into parser construction. - Configure logging explicitly in `main`/root construction rather than `init`. ## Acceptance criteria - Repeated command construction/execution is deterministic and race-safe. - No mutable exported field-set map remains. - `timestamp` has explicit request and response fields. - TSA ordering and enabled-count behavior are deterministic. - Parallel tests pass under the race detector. Reviewed against `948b7a9` on `master` (Go 1.26.2).
heiko closed this issue 2026-07-22 11:18:25 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
heiko/mailseal#7
No description provided.