go-waku/waku/v2/rpc/runner.go

44 lines
804 B
Go
Raw Normal View History

2021-11-22 14:48:32 +00:00
package rpc
import (
v2 "github.com/waku-org/go-waku/waku/v2"
"github.com/waku-org/go-waku/waku/v2/protocol"
2021-11-22 14:48:32 +00:00
)
type Adder func(msg *protocol.Envelope)
type runnerService struct {
broadcaster v2.Broadcaster
ch chan *protocol.Envelope
quit chan bool
adder Adder
}
func newRunnerService(broadcaster v2.Broadcaster, adder Adder) *runnerService {
return &runnerService{
broadcaster: broadcaster,
quit: make(chan bool),
adder: adder,
}
}
func (r *runnerService) Start() {
r.ch = make(chan *protocol.Envelope, 1024)
r.broadcaster.Register(nil, r.ch)
2021-11-22 14:48:32 +00:00
for {
select {
case <-r.quit:
return
case envelope := <-r.ch:
r.adder(envelope)
}
}
}
func (r *runnerService) Stop() {
r.quit <- true
r.broadcaster.Unregister(nil, r.ch)
2021-11-22 14:48:32 +00:00
close(r.ch)
}