refactor: extract magic numbers to named constants#29
Open
aicontentcreate2023-star wants to merge 1 commit intoNP-compete:mainfrom
Open
refactor: extract magic numbers to named constants#29aicontentcreate2023-star wants to merge 1 commit intoNP-compete:mainfrom
aicontentcreate2023-star wants to merge 1 commit intoNP-compete:mainfrom
Conversation
Fixes NP-compete#21 Creates internal/constants/constants.go with descriptive names for all hard-coded timeout and limit values used throughout the codebase. Changes: - Created internal/constants/constants.go with: * HTTP server timeouts (Request/Read/Write/Idle/Shutdown) * Pagination limits (DefaultPageSize, MaxPageSize) * Long operation tool bounds (Min/MaxOperationSeconds) * Rate limiting constants (for future use) - Updated files to use constants: * internal/api/router.go - DefaultRequestTimeout * cmd/server/main.go - Read/Write/Idle/ShutdownTimeout * internal/tools/long_operation_sdk.go - Min/MaxOperationSeconds * test/integration_test.go - MaxPageSize Benefits: - Single source of truth for configuration values - Self-documenting code with descriptive constant names - Easier to modify timeout/limit values in one place - All constants include godoc comments All magic numbers have been replaced and the code compiles successfully.
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.
Fixes #21
Summary
Replaces hard-coded timeout and limit values scattered throughout the codebase with named constants defined in a centralized location.
Changes
Created
internal/constants/constants.goDefines all magic numbers with descriptive names and godoc comments:
Updated Files
internal/api/router.go
60 * time.Second → constants.DefaultRequestTimeoutcmd/server/main.go
ReadTimeout: 30 * time.Second → constants.DefaultReadTimeoutWriteTimeout: 30 * time.Second → constants.DefaultWriteTimeoutIdleTimeout: 120 * time.Second → constants.DefaultIdleTimeoutcontext.WithTimeout(..., 30*time.Second) → constants.DefaultShutdownTimeoutinternal/tools/long_operation_sdk.go
if input.Seconds < 1 → if input.Seconds < constants.MinOperationSecondsif input.Seconds > 60 → if input.Seconds > constants.MaxOperationSecondstest/integration_test.go
float64(100) → float64(constants.MaxPageSize)Benefits
✅ Single source of truth for all configuration values
✅ Self-documenting code with descriptive constant names
✅ Easier maintenance — modify timeout/limit values in one place
✅ All constants include godoc comments explaining their purpose
✅ Code compiles and builds successfully
Testing
✅ Verified
go build ./cmd/serversucceeds✅ All magic numbers replaced consistently
✅ No behavioral changes — only refactoring