Skip to content

Commit

Permalink
refactor: remove defer and explictly unlock (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tochemey authored Aug 30, 2024
1 parent 4e6a479 commit f4f11a0
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions actors/actor_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,17 +539,18 @@ func (x *actorSystem) PeerAddress() string {
// An actor not found error is return when the actor is not found.
func (x *actorSystem) ActorOf(ctx context.Context, actorName string) (addr *goaktpb.Address, pid *PID, err error) {
x.locker.Lock()
defer x.locker.Unlock()

if !x.started.Load() {
x.locker.Unlock()
return nil, nil, ErrActorSystemNotStarted
}

// first check whether the actor exist locally
pids := x.actors.pids()
for _, actorRef := range pids {
if actorRef.ActorPath().Name() == actorName {
return actorRef.ActorPath().RemoteAddress(), actorRef, nil
for _, lpid := range pids {
if lpid.ActorPath().Name() == actorName {
x.locker.Unlock()
return lpid.ActorPath().RemoteAddress(), lpid, nil
}
}

Expand All @@ -560,41 +561,48 @@ func (x *actorSystem) ActorOf(ctx context.Context, actorName string) (addr *goak
if errors.Is(err, cluster.ErrActorNotFound) {
x.logger.Infof("actor=%s not found", actorName)
e := ErrActorNotFound(actorName)
x.locker.Unlock()
return nil, nil, e
}

x.locker.Unlock()
return nil, nil, fmt.Errorf("failed to fetch remote actor=%s: %w", actorName, err)
}

x.locker.Unlock()
return actor.GetActorAddress(), nil, nil
}

if x.remotingEnabled.Load() {
x.locker.Unlock()
return nil, nil, ErrMethodCallNotAllowed
}

x.logger.Infof("actor=%s not found", actorName)
x.locker.Unlock()
return nil, nil, ErrActorNotFound(actorName)
}

// LocalActor returns the reference of a local actor.
// A local actor is an actor that reside on the same node where the given actor system is running
func (x *actorSystem) LocalActor(actorName string) (*PID, error) {
x.locker.Lock()
defer x.locker.Unlock()

if !x.started.Load() {
x.locker.Unlock()
return nil, ErrActorSystemNotStarted
}

pids := x.actors.pids()
for _, pid := range pids {
if pid.ActorPath().Name() == actorName {
x.locker.Unlock()
return pid, nil
}
}

x.logger.Infof("actor=%s not found", actorName)
x.locker.Unlock()
return nil, ErrActorNotFound(actorName)
}

Expand All @@ -603,27 +611,31 @@ func (x *actorSystem) LocalActor(actorName string) (*PID, error) {
// One can always check whether cluster is enabled before calling this method or just use the ActorOf method.
func (x *actorSystem) RemoteActor(ctx context.Context, actorName string) (addr *goaktpb.Address, err error) {
x.locker.Lock()
defer x.locker.Unlock()

if !x.started.Load() {
e := ErrActorSystemNotStarted
x.locker.Unlock()
return nil, e
}

if x.cluster == nil {
x.locker.Unlock()
return nil, ErrClusterDisabled
}

actor, err := x.cluster.GetActor(ctx, actorName)
if err != nil {
if errors.Is(err, cluster.ErrActorNotFound) {
x.logger.Infof("actor=%s not found", actorName)
x.locker.Unlock()
return nil, ErrActorNotFound(actorName)
}

x.locker.Unlock()
return nil, fmt.Errorf("failed to fetch remote actor=%s: %w", actorName, err)
}

x.locker.Unlock()
return actor.GetActorAddress(), nil
}

Expand Down

0 comments on commit f4f11a0

Please sign in to comment.