Skip to content

Commit

Permalink
rips out the custom logging. replace with vanilla slog. (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
perbu authored Dec 5, 2023
1 parent c089c0b commit c0a4bb5
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 201 deletions.
6 changes: 2 additions & 4 deletions actor/context.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package actor

import (
"log/slog"
"strings"
"time"

"github.com/anthdm/hollywood/log"
"github.com/anthdm/hollywood/safemap"
)

Expand All @@ -19,15 +19,13 @@ type Context struct {
// when the child dies.
parentCtx *Context
children *safemap.SafeMap[string, *PID]
logger log.Logger
}

func newContext(e *Engine, pid *PID) *Context {
return &Context{
engine: e,
pid: pid,
children: safemap.New[string, *PID](),
logger: e.logger.SubLogger("[context]"),
}
}

Expand All @@ -45,7 +43,7 @@ func (c *Context) Request(pid *PID, msg any, timeout time.Duration) *Response {
// Respond will sent the given message to the sender of the current received message.
func (c *Context) Respond(msg any) {
if c.sender == nil {
c.logger.Warnw("context got no sender", "func", "Respond", "pid", c.PID())
slog.Warn("context got no sender", "func", "Respond", "pid", c.PID())
return
}
c.engine.Send(c.sender, msg)
Expand Down
19 changes: 7 additions & 12 deletions actor/deadletter.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package actor

import (
fmt "fmt"
"log/slog"
"reflect"

"github.com/anthdm/hollywood/log"
)

//

type deadLetter struct {
logger log.Logger
pid *PID
pid *PID
}

func newDeadLetter() Receiver {
Expand All @@ -29,17 +26,15 @@ func (d *deadLetter) Receive(ctx *Context) {
switch msg := ctx.Message().(type) {
case Started:
// intialize logger on deadletter startup. is this a sane approach? I'm not sure how the get to the logger otherwise.
d.logger = ctx.Engine().logger.SubLogger("[deadletter]")
d.logger.Debugw("default deadletter actor started")
slog.Debug("default deadletter actor started")
case Stopped:
d.logger.Debugw("default deadletter actor stopped")
slog.Debug("default deadletter actor stopped")
case Initialized:
d.logger.Debugw("default deadletter actor initialized")
slog.Debug("default deadletter actor initialized")
case *DeadLetterEvent:
fmt.Println("received deadletter", msg)
d.logger.Warnw("deadletter arrived", "msg-type", reflect.TypeOf(msg),
slog.Warn("deadletter arrived", "msg-type", reflect.TypeOf(msg),
"sender", msg.Sender, "target", msg.Target, "msg", msg.Message)
default:
d.logger.Errorw("unknown message arrived", "msg", msg)
slog.Error("unknown message arrived at deadletter", "msg", msg)
}
}
10 changes: 4 additions & 6 deletions actor/deadletter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"bytes"
"fmt"
"log/slog"
"os"
"sync"
"testing"
"time"

"github.com/anthdm/hollywood/log"
"github.com/stretchr/testify/assert"
)

Expand All @@ -18,8 +16,10 @@ import (
// received the message.
func TestDeadLetterDefault(t *testing.T) {
logBuffer := SafeBuffer{}
lh := log.NewHandler(&logBuffer, log.TextFormat, slog.LevelDebug)
e, err := NewEngine(EngineOptLogger(log.NewLogger("[engine]", lh)))
lh := slog.NewTextHandler(&logBuffer, nil)
logger := slog.New(lh)
slog.SetDefault(logger)
e, err := NewEngine()
assert.NoError(t, err)
a1 := e.Spawn(newTestActor, "a1")
assert.NotNil(t, a1)
Expand All @@ -39,9 +39,7 @@ func TestDeadLetterDefault(t *testing.T) {
// received the message.
// It is using the custom deadletter receiver below.
func TestDeadLetterCustom(t *testing.T) {
lh := log.NewHandler(os.Stdout, log.TextFormat, slog.LevelDebug)
e, err := NewEngine(
EngineOptLogger(log.NewLogger("[engine]", lh)),
EngineOptDeadletter(newCustomDeadLetter))
assert.NoError(t, err)
a1 := e.Spawn(newTestActor, "a1")
Expand Down
17 changes: 3 additions & 14 deletions actor/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import (
reflect "reflect"
"sync"
"time"

"github.com/anthdm/hollywood/log"
)

type Remoter interface {
Address() string
Send(*PID, any, *PID)
Start(*Engine, log.Logger) error
Start(*Engine) error
}

// Producer is any function that can return a Receiver
Expand All @@ -31,7 +29,6 @@ type Engine struct {
remote Remoter
deadLetter *PID
eventStream *PID
logger log.Logger
initErrors []error
}

Expand All @@ -56,26 +53,18 @@ func NewEngine(opts ...func(*Engine)) (*Engine, error) {
e.eventStream = e.Spawn(NewEventStream(), "eventstream")
// if no deadletter is registered, we will register the default deadletter from deadletter.go
if e.deadLetter == nil {
e.logger.Debugw("no deadletter receiver set, registering default")
e.deadLetter = e.Spawn(newDeadLetter, "deadletter")
}
return e, nil
}

// EngineOptLogger configured the engine with a logger from the internal log package
func EngineOptLogger(logger log.Logger) func(*Engine) {
return func(e *Engine) {
e.logger = logger
}
}

// TODO: Doc
func EngineOptRemote(r Remoter) func(*Engine) {
return func(e *Engine) {
e.remote = r
e.address = r.Address()
// TODO: potential error not handled here
r.Start(e, e.logger)
r.Start(e)
}
}

Expand All @@ -101,7 +90,7 @@ func EngineOptDeadletter(d Producer) func(*Engine) {
func (e *Engine) WithRemote(r Remoter) {
e.remote = r
e.address = r.Address()
r.Start(e, e.logger)
r.Start(e)
}

// Spawn spawns a process that will producer by the given Producer and
Expand Down
15 changes: 6 additions & 9 deletions actor/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package actor

import (
"fmt"
"log/slog"
"runtime/debug"
"sync"
"time"

"github.com/anthdm/hollywood/log"
)

type Envelope struct {
Expand All @@ -32,7 +31,6 @@ type process struct {
restarts int32

mbuffer []Envelope
logger log.Logger
}

func newProcess(e *Engine, opts Opts) *process {
Expand All @@ -44,7 +42,6 @@ func newProcess(e *Engine, opts Opts) *process {
Opts: opts,
context: ctx,
mbuffer: nil,
logger: e.logger.SubLogger(opts.Name),
}
p.inbox.Start(p)
return p
Expand Down Expand Up @@ -129,7 +126,7 @@ func (p *process) Start() {
p.context.message = Started{}
applyMiddleware(recv.Receive, p.Opts.Middleware...)(p.context)
p.context.engine.BroadcastEvent(ActorStartedEvent{PID: p.pid})
p.logger.Debugw("actor started", "pid", p.pid)
slog.Debug("actor started", "pid", p.pid)
// If we have messages in our buffer, invoke them.
if len(p.mbuffer) > 0 {
p.Invoke(p.mbuffer)
Expand All @@ -144,7 +141,7 @@ func (p *process) tryRestart(v any) {
// back up. NOTE: not sure if that is the best option. What if that
// node never comes back up again?
if msg, ok := v.(*InternalError); ok {
p.logger.Errorw(msg.From, "err", msg.Err)
slog.Error(msg.From, "err", msg.Err)
time.Sleep(p.Opts.RestartDelay)
p.Start()
return
Expand All @@ -154,15 +151,15 @@ func (p *process) tryRestart(v any) {
// If we reach the max restarts, we shutdown the inbox and clean
// everything up.
if p.restarts == p.MaxRestarts {
p.logger.Errorw("max restarts exceeded, shutting down...",
slog.Error("max restarts exceeded, shutting down...",
"pid", p.pid, "restarts", p.restarts)
p.cleanup(nil)
return
}

p.restarts++
// Restart the process after its restartDelay
p.logger.Errorw("actor restarting",
slog.Error("actor restarting",
"n", p.restarts,
"maxRestarts", p.MaxRestarts,
"pid", p.pid,
Expand Down Expand Up @@ -196,7 +193,7 @@ func (p *process) cleanup(wg *sync.WaitGroup) {
proc.Shutdown(wg)
}
}
p.logger.Debugw("shutdown", "pid", p.pid)
slog.Debug("shutdown", "pid", p.pid)
p.context.engine.BroadcastEvent(ActorStoppedEvent{PID: p.pid})
if wg != nil {
wg.Done()
Expand Down
7 changes: 2 additions & 5 deletions actor/registry.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package actor

import (
"log/slog"
"sync"

"github.com/anthdm/hollywood/log"
)

const LocalLookupAddr = "local"
Expand All @@ -12,14 +11,12 @@ type Registry struct {
mu sync.RWMutex
lookup map[string]Processer
engine *Engine
logger log.Logger
}

func newRegistry(e *Engine) *Registry {
return &Registry{
lookup: make(map[string]Processer, 1024),
engine: e,
logger: e.logger.SubLogger("[registry]"),
}
}

Expand Down Expand Up @@ -56,7 +53,7 @@ func (r *Registry) add(proc Processer) {
defer r.mu.Unlock()
id := proc.PID().ID
if _, ok := r.lookup[id]; ok {
r.logger.Warnw("process already registered",
slog.Warn("process already registered",
"pid", proc.PID(),
)
return
Expand Down
3 changes: 1 addition & 2 deletions examples/chat/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"github.com/anthdm/hollywood/actor"
"github.com/anthdm/hollywood/examples/chat/types"
"github.com/anthdm/hollywood/log"
"github.com/anthdm/hollywood/remote"
"log/slog"
"math/rand"
Expand Down Expand Up @@ -55,7 +54,7 @@ func main() {
rem := remote.New(remote.Config{
ListenAddr: *listenAt,
})
e, err := actor.NewEngine(actor.EngineOptLogger(log.Default()), actor.EngineOptRemote(rem))
e, err := actor.NewEngine(actor.EngineOptRemote(rem))
if err != nil {
slog.Error("failed to create engine", "err", err)
os.Exit(1)
Expand Down
8 changes: 2 additions & 6 deletions examples/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package main

import (
"fmt"
"github.com/anthdm/hollywood/log"
"log/slog"
"os"

"github.com/anthdm/hollywood/actor"
)

Expand All @@ -31,8 +27,8 @@ func (f *foo) Receive(ctx *actor.Context) {
}

func main() {
lh := log.NewHandler(os.Stdout, log.TextFormat, slog.LevelDebug)
engine, err := actor.NewEngine(actor.EngineOptLogger(log.NewLogger("[engine]", lh)))

engine, err := actor.NewEngine()
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit c0a4bb5

Please sign in to comment.