Skip to content

Commit

Permalink
Poison (#46)
Browse files Browse the repository at this point in the history
* poison returns a wait group api

* clean API

* updated README
  • Loading branch information
anthdm authored Aug 30, 2023
1 parent bd6a765 commit 72c7722
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 36 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ By using the Actor Model in your application, you can build highly scalable and

## Features

- lock free LMAX based message queue for ultra low latency messaging
- guaranteed message delivery on receiver failure (buffer mechanism)
- fire&forget or request&response messaging, or both.
- dRPC as the transport layer
Expand Down Expand Up @@ -71,7 +70,9 @@ func main() {
engine := actor.NewEngine()
pid := engine.Spawn(newFoo, "foo")
engine.Send(pid, &message{data: "hello world!"})
time.Sleep(time.Second * 1)

// Stop the actor, but let it process its messages first.
engine.Poison(pid).Wait()
}
```

Expand Down
7 changes: 5 additions & 2 deletions actor/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,19 @@ func (e *Engine) SendRepeat(pid *PID, msg any, interval time.Duration) SendRepea
// Poison will send a poisonPill to the process that is associated with the given PID.
// The process will shut down once it processed all its messages before the poisonPill
// was received. If given a WaitGroup, you can wait till the process is completely shutdown.
func (e *Engine) Poison(pid *PID, wg ...*sync.WaitGroup) {
func (e *Engine) Poison(pid *PID, wg ...*sync.WaitGroup) *sync.WaitGroup {
var _wg *sync.WaitGroup
if len(wg) > 0 {
_wg = wg[0]
_wg.Add(1)
} else {
_wg = &sync.WaitGroup{}
}
_wg.Add(1)
proc := e.Registry.get(pid)
if proc != nil {
e.SendLocal(pid, poisonPill{_wg}, nil)
}
return _wg
}

func (e *Engine) SendLocal(pid *PID, msg any, sender *PID) {
Expand Down
5 changes: 1 addition & 4 deletions examples/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"sync"

"github.com/anthdm/hollywood/actor"
)
Expand Down Expand Up @@ -34,7 +33,5 @@ func main() {
for i := 0; i < 100; i++ {
engine.Send(pid, &message{data: "hello world!"})
}
wg := sync.WaitGroup{}
engine.Poison(pid, &wg)
wg.Wait()
engine.Poison(pid).Wait()
}
28 changes: 0 additions & 28 deletions examples/ttt/main.go

This file was deleted.

0 comments on commit 72c7722

Please sign in to comment.