refactor: adjust error handling, improve locking & logging#19
Merged
danpasecinic merged 7 commits intodevelopmentfrom Mar 7, 2026
Merged
refactor: adjust error handling, improve locking & logging#19danpasecinic merged 7 commits intodevelopmentfrom
danpasecinic merged 7 commits intodevelopmentfrom
Conversation
Remove errCircularDependency, errDuplicateService, and errProviderFailed which were defined but never called. Remove //nolint:unused directives from errServiceNotFound, errStartupFailed, errShutdownFailed which are actually used.
Extract registerLocked/registerValueLocked from Register/RegisterValue to use defer-based unlock instead of manual multi-exit-point unlocks. Extract markResolving/unmarkResolving from resolveSlow for the same reason. Add comment to GetInstanceFast documenting why defer is intentionally skipped on that hot path.
Log a warning via slog when a pooled instance is dropped because the pool channel is full. Logging lives in Container.Release rather than Registry to keep the data layer free of observability concerns.
…resolution failure Change InvokeOptional/InvokeOptionalNamed return type from Optional[T] to (Optional[T], error). Not-registered returns (None, nil); a broken provider returns (None, error). Extract shared resolveOptional helper to deduplicate the Ctx and NamedCtx variants. Remove TOCTOU Has()+Resolve() double-lock pattern in favor of a single Resolve() call with error inspection.
Add 6 tests covering parallel singleton resolve, named provide+invoke, pool acquire/release cycles, transient with different keys, request scope isolation, and concurrent replace with race detection.
Add a Choosing a Scope decision table and a Replacing Services section with usage examples to the README.
Replace and ReplaceValue were calling Unsafe registry methods (no registry lock) while holding only the container mutex. Concurrent Resolve calls via GetInstanceFast hold only the registry mutex, creating a race on the services map. Switch to locked registry methods so both paths coordinate through the same lock. Also remove leftover rebase conflict marker in resolve.go.
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.
This pull request introduces improved concurrency support, error handling, and documentation updates for the Needle dependency injection framework. The most significant changes include new concurrent tests for service resolution and registration, enhanced error handling for optional dependencies, documentation and example updates to reflect the new error-returning API, and internal refactoring to improve thread safety and pool management.