Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: change boundedmailbox implementation #559

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: add Uptime methods to PID and ActorSystem
Tochemey committed Dec 13, 2024
commit b8125fb0e8a4b14790e23f9499558b5f9c78db4e
14 changes: 13 additions & 1 deletion actors/actor_system.go
Original file line number Diff line number Diff line change
@@ -147,6 +147,8 @@ type ActorSystem interface {
// Port returns the actor system node port.
// This is the bind port for remote communication
Port() int32
// Uptime returns the number of seconds since the actor system started
Uptime() int64
// handleRemoteAsk handles a synchronous message to another actor and expect a response.
// This block until a response is received or timed out.
handleRemoteAsk(ctx context.Context, to *PID, message proto.Message, timeout time.Duration) (response proto.Message, err error)
@@ -250,6 +252,7 @@ type actorSystem struct {
userGuardian *PID
systemGuardian *PID
janitor *PID
startedAt *atomic.Int64
}

// enforce compilation error when all methods of the ActorSystem interface are not implemented
@@ -287,6 +290,7 @@ func NewActorSystem(name string, opts ...Option) (ActorSystem, error) {
port: 0,
host: "127.0.0.1",
actors: newTree(),
startedAt: atomic.NewInt64(0),
}

system.started.Store(false)
@@ -320,6 +324,14 @@ func NewActorSystem(name string, opts ...Option) (ActorSystem, error) {
return system, nil
}

// Uptime returns the number of seconds since the actor system started
func (x *actorSystem) Uptime() int64 {
if x.started.Load() {
return time.Now().Unix() - x.startedAt.Load()
}
return 0
}

// Host returns the actor system node host address
// This is the bind address for remote communication
func (x *actorSystem) Host() string {
@@ -728,7 +740,7 @@ func (x *actorSystem) Start(ctx context.Context) error {
}

x.scheduler.Start(ctx)

x.startedAt.Store(time.Now().Unix())
x.logger.Infof("%s started..:)", x.name)
return nil
}
3 changes: 3 additions & 0 deletions actors/actor_system_test.go
Original file line number Diff line number Diff line change
@@ -79,6 +79,7 @@ func TestActorSystem(t *testing.T) {
assert.Error(t, err)
assert.EqualError(t, err, ErrActorSystemNotStarted.Error())
assert.Nil(t, actorRef)
assert.Zero(t, sys.Uptime())
})
t.Run("With Spawn an actor when started", func(t *testing.T) {
ctx := context.TODO()
@@ -95,6 +96,8 @@ func TestActorSystem(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, actorRef)

assert.NotZero(t, sys.Uptime())

// stop the actor after some time
lib.Pause(time.Second)
err = sys.Stop(ctx)
13 changes: 13 additions & 0 deletions actors/pid.go
Original file line number Diff line number Diff line change
@@ -143,6 +143,7 @@ type PID struct {
remoting *Remoting

goScheduler *goScheduler
startedAt *atomic.Int64
}

// newPID creates a new pid
@@ -177,6 +178,7 @@ func newPID(ctx context.Context, address *address.Address, actor Actor, opts ...
remoting: NewRemoting(),
goScheduler: newGoScheduler(300),
supervisorStrategies: newStrategiesMap(),
startedAt: atomic.NewInt64(0),
}

pid.initMaxRetries.Store(DefaultInitMaxRetries)
@@ -214,9 +216,18 @@ func newPID(ctx context.Context, address *address.Address, actor Actor, opts ...
receiveContext.build(ctx, NoSender, pid, new(goaktpb.PostStart), true)
pid.doReceive(receiveContext)

pid.startedAt.Store(time.Now().Unix())
return pid, nil
}

// Uptime returns the number of seconds since the actor started
func (pid *PID) Uptime() int64 {
if pid.IsRunning() {
return time.Now().Unix() - pid.startedAt.Load()
}
return 0
}

// ID is a convenient method that returns the actor unique identifier
// An actor unique identifier is its address in the actor system.
func (pid *PID) ID() string {
@@ -1268,6 +1279,8 @@ func (pid *PID) reset() {
pid.initTimeout.Store(DefaultInitTimeout)
pid.behaviorStack.Reset()
pid.processedCount.Store(0)
pid.restartCount.Store(0)
pid.startedAt.Store(0)
pid.stopping.Store(false)
pid.suspended.Store(false)
pid.supervisorStrategies.Reset()
6 changes: 6 additions & 0 deletions actors/pid_test.go
Original file line number Diff line number Diff line change
@@ -283,11 +283,16 @@ func TestRestart(t *testing.T) {
require.NoError(t, err)
assert.NotNil(t, pid)

lib.Pause(time.Second)

assert.NotZero(t, pid.Uptime())

// stop the actor
err = pid.Shutdown(ctx)
assert.NoError(t, err)

lib.Pause(time.Second)
assert.Zero(t, pid.Uptime())

// let us send a message to the actor
err = Tell(ctx, pid, new(testpb.TestSend))
@@ -298,6 +303,7 @@ func TestRestart(t *testing.T) {
err = pid.Restart(ctx)
assert.NoError(t, err)
assert.True(t, pid.IsRunning())
assert.NotZero(t, pid.Uptime())
// let us send 10 public to the actor
count := 10
for i := 0; i < count; i++ {