Skip to content

refactor: adjust error handling, improve locking & logging#19

Merged
danpasecinic merged 7 commits intodevelopmentfrom
feat/improve-di-framework
Mar 7, 2026
Merged

refactor: adjust error handling, improve locking & logging#19
danpasecinic merged 7 commits intodevelopmentfrom
feat/improve-di-framework

Conversation

@danpasecinic
Copy link
Owner

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.

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.
@danpasecinic danpasecinic self-assigned this Mar 7, 2026
@danpasecinic danpasecinic added the enhancement New feature or request label Mar 7, 2026
@danpasecinic danpasecinic merged commit f365af4 into development Mar 7, 2026
5 checks passed
@danpasecinic danpasecinic deleted the feat/improve-di-framework branch March 7, 2026 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant