Skip to content

Commit

Permalink
fix:concurrent save singleton resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
4strodev committed Jan 9, 2025
1 parent c3f3537 commit 8619288
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
6 changes: 2 additions & 4 deletions pkg/dependency_spec.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pkg

import (
"log"
"reflect"
"sync"

Expand Down Expand Up @@ -32,15 +31,14 @@ func (spec *dependencySpec) Type() reflect.Type {
func (spec *dependencySpec) Resolve() (any, error) {
switch spec.lifeCycle {
case SINGLETON:
spec.mutex.Lock()
defer spec.mutex.Unlock()
if spec.instance == nil {
spec.mutex.Lock()
defer spec.mutex.Unlock()
instance, err := spec.executeResolver()
if err != nil {
return nil, err
}
if instance == nil {
log.Println("instance is nil!")
return nil, errors.NewError("Resolver returned a nil instance")
}
spec.instance = instance
Expand Down
2 changes: 1 addition & 1 deletion pkg/dependency_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestSpecResolve(t *testing.T) {
}

waitGroup.Wait()
require.Equal(t, int32(5), counter.Load())
require.Equal(t, int32(1), counter.Load())
})
}

Expand Down

0 comments on commit 8619288

Please sign in to comment.