Skip to content

Instantly share code, notes, and snippets.

@navicore
Forked from ccutch/actor_persistance.go
Created February 16, 2018 02:27
Show Gist options
  • Save navicore/7655bf52cfe13ba1b88de162507b71e5 to your computer and use it in GitHub Desktop.
Save navicore/7655bf52cfe13ba1b88de162507b71e5 to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "bytes"
import "encoding/gob"
import "encoding/hex"
import "github.com/AsynkronIT/protoactor-go/actor"
type MyActor struct {
CallCount int
}
type Persistant struct {
actor.Actor
}
func Persist(a actor.Actor) actor.Actor {
return Persistant{a}
}
func (p Persistant) Receive(c actor.Context) {
p.Actor.Receive(c)
var buff bytes.Buffer
enc := gob.NewEncoder(&buff)
enc.Encode(&p.Actor)
// replace printing with writing to db/file
fmt.Println(hex.EncodeToString(buff.Bytes()))
}
func NewActor() actor.Actor {
a := &MyActor{
CallCount: 0,
}
return Persist(a)
}
func (a *MyActor) Receive(ctx actor.Context) {
a.CallCount++
switch msg := ctx.Message().(type) {
case string:
fmt.Printf("[call: %d] Hello %s\n", a.CallCount, msg)
}
}
func main() {
gob.Register(&MyActor{})
props := actor.FromProducer(NewActor)
pid := actor.Spawn(props)
fmt.Println("")
pid.Tell("Connor")
pid.Tell("Connor")
pid.Tell("Connor")
pid.Tell("Connor")
pid.Tell("Connor")
fmt.Scanln()
}
3310000d2a6d61696e2e4d794163746f72ff81030101074d794163746f7201ff82000101010943616c6c436f756e74010400000006ff8203010200
[call: 2] Hello Connor
3310000d2a6d61696e2e4d794163746f72ff81030101074d794163746f7201ff82000101010943616c6c436f756e74010400000006ff8203010400
[call: 3] Hello Connor
3310000d2a6d61696e2e4d794163746f72ff81030101074d794163746f7201ff82000101010943616c6c436f756e74010400000006ff8203010600
[call: 4] Hello Connor
3310000d2a6d61696e2e4d794163746f72ff81030101074d794163746f7201ff82000101010943616c6c436f756e74010400000006ff8203010800
[call: 5] Hello Connor
3310000d2a6d61696e2e4d794163746f72ff81030101074d794163746f7201ff82000101010943616c6c436f756e74010400000006ff8203010a00
[call: 6] Hello Connor
3310000d2a6d61696e2e4d794163746f72ff81030101074d794163746f7201ff82000101010943616c6c436f756e74010400000006ff8203010c00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment