Skip to content

Commit

Permalink
minor changes and make EventLogger public
Browse files Browse the repository at this point in the history
  • Loading branch information
anthdm committed Dec 6, 2023
1 parent ba84b41 commit 18ca292
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 68 deletions.
42 changes: 0 additions & 42 deletions actor/deadletter.go

This file was deleted.

3 changes: 2 additions & 1 deletion actor/deadletter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package actor

import (
"bytes"
"github.com/stretchr/testify/assert"
"sync"
"testing"

"github.com/stretchr/testify/assert"
)

// TestDeadLetterCustom tests the custom deadletter handling.
Expand Down
14 changes: 12 additions & 2 deletions actor/event_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ import (
"log/slog"
)

// EventSub is the message that will be send to subscribe to the event stream.
type EventSub struct {
pid *PID
}

// EventUnSub is the message that will be send to unsubscribe from the event stream.
type EventUnsub struct {
pid *PID
}

type EventStream struct {
subs map[*PID]bool
}
Expand All @@ -28,9 +38,9 @@ func (e *EventStream) Receive(c *Context) {
delete(e.subs, msg.pid)
default:
// check if we should log the event, if so, log it with the relevant level, message and attributes
logMsg, ok := c.Message().(eventLog)
logMsg, ok := c.Message().(EventLogger)
if ok {
level, msg, attr := logMsg.log()
level, msg, attr := logMsg.Log()
slog.Log(context.Background(), level, msg, attr...)
}
for sub := range e.subs {
Expand Down
39 changes: 16 additions & 23 deletions actor/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,10 @@ import (

// Here the events are defined.

// eventLog is an interface that the various Events can choose to implement. If they do, the event stream
// EventLogger is an interface that the various Events can choose to implement. If they do, the event stream
// will log these events to slog.
type eventLog interface {
log() (slog.Level, string, []any)
}

// EventSub is the message that will be send to subscribe to the event stream.
type EventSub struct {
pid *PID
}

// EventUnSub is the message that will be send to unsubscribe from the event stream.
type EventUnsub struct {
pid *PID
type EventLogger interface {
Log() (slog.Level, string, []any)
}

// ActorStartedEvent is broadcasted over the EventStream each time
Expand All @@ -32,7 +22,7 @@ type ActorStartedEvent struct {
Timestamp time.Time
}

func (e ActorStartedEvent) log() (slog.Level, string, []any) {
func (e ActorStartedEvent) Log() (slog.Level, string, []any) {
return slog.LevelInfo, "Actor started", []any{"pid", e.PID.GetID()}
}

Expand All @@ -43,7 +33,7 @@ type ActorStoppedEvent struct {
Timestamp time.Time
}

func (e ActorStoppedEvent) log() (slog.Level, string, []any) {
func (e ActorStoppedEvent) Log() (slog.Level, string, []any) {
return slog.LevelInfo, "Actor stopped", []any{"pid", e.PID.GetID()}
}

Expand All @@ -56,7 +46,7 @@ type ActorRestartedEvent struct {
Restarts int32
}

func (e ActorRestartedEvent) log() (slog.Level, string, []any) {
func (e ActorRestartedEvent) Log() (slog.Level, string, []any) {
return slog.LevelError, "Actor crashed and restarted",
[]any{"pid", e.PID.GetID(), "stack", string(e.Stacktrace),
"reason", e.Reason, "restarts", e.Restarts}
Expand All @@ -68,7 +58,7 @@ type ActorMaxRestartsExceededEvent struct {
Timestamp time.Time
}

func (e ActorMaxRestartsExceededEvent) log() (slog.Level, string, []any) {
func (e ActorMaxRestartsExceededEvent) Log() (slog.Level, string, []any) {
return slog.LevelError, "Actor crashed too many times", []any{"pid", e.PID.GetID()}
}

Expand All @@ -78,22 +68,25 @@ type ActorDuplicateIdEvent struct {
PID *PID
}

func (e ActorDuplicateIdEvent) log() (slog.Level, string, []any) {
func (e ActorDuplicateIdEvent) Log() (slog.Level, string, []any) {
return slog.LevelError, "Actor name already claimed", []any{"pid", e.PID.GetID()}
}

// TODO: Not sure if this event is super usefull. Cause most DeadLetter actors
// we be subscribed to late to the event stream.
type EngineRemoteMissingEvent struct {
Target *PID
Sender *PID
Message any
}

func (e EngineRemoteMissingEvent) log() (slog.Level, string, []any) {
func (e EngineRemoteMissingEvent) Log() (slog.Level, string, []any) {
return slog.LevelError, "Engine has no remote", []any{"sender", e.Target.GetID()}
}

type DeadLetterLoopEvent struct{}

func (e DeadLetterLoopEvent) log() (slog.Level, string, []any) {
return slog.LevelError, "Deadletter loop detected", []any{}
// DeadLetterEvent is delivered to the deadletter actor when a message can't be delivered to it's recipient
type DeadLetterEvent struct {
Target *PID
Message any
Sender *PID
}

0 comments on commit 18ca292

Please sign in to comment.