github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/prepare/23_proto_actor/cluster-broadcast/shared/calculatorGrain.go (about) 1 package shared 2 3 import "github.com/AsynkronIT/protoactor-go/cluster" 4 5 type CalcGrain struct { 6 cluster.Grain 7 total int64 8 } 9 10 func (c *CalcGrain) Init(id string) { 11 c.Grain.Init(id) 12 c.total = 0 13 14 // register with the tracker 15 trackerGrain := GetTrackerGrain("singleTrackerGrain") 16 trackerGrain.RegisterGrain(&RegisterMessage{GrainId: c.ID()}) 17 } 18 19 func (c *CalcGrain) Terminate() { 20 21 // deregister with the tracker 22 trackerGrain := GetTrackerGrain("singleTrackerGrain") 23 trackerGrain.DeregisterGrain(&RegisterMessage{GrainId: c.ID()}) 24 } 25 26 func (c *CalcGrain) Add(n *NumberRequest, ctx cluster.GrainContext) (*CountResponse, error) { 27 c.total = c.total + n.Number 28 return &CountResponse{Number: c.total}, nil 29 } 30 31 func (c *CalcGrain) Subtract(n *NumberRequest, ctx cluster.GrainContext) (*CountResponse, error) { 32 c.total = c.total - n.Number 33 return &CountResponse{Number: c.total}, nil 34 } 35 36 func (c *CalcGrain) GetCurrent(n *Noop, ctx cluster.GrainContext) (*CountResponse, error) { 37 return &CountResponse{Number: c.total}, nil 38 }