func New(primary bool, local, remote string) (bstar *Bstar, err error) { bstar = &Bstar{} // Initialize the Binary Star bstar.Reactor = zmq.NewReactor() if primary { bstar.state = state_PRIMARY } else { bstar.state = state_BACKUP } // Create publisher for state going to peer bstar.statepub, err = zmq.NewSocket(zmq.PUB) bstar.statepub.Bind(local) // Create subscriber for state coming from peer bstar.statesub, err = zmq.NewSocket(zmq.SUB) bstar.statesub.SetSubscribe("") bstar.statesub.Connect(remote) // Set-up basic reactor events bstar.Reactor.AddChannelTime(time.Tick(bstar_HEARTBEAT), 1, func(i interface{}) error { return bstar.send_state() }) bstar.Reactor.AddSocket(bstar.statesub, zmq.POLLIN, func(e zmq.State) error { return bstar.recv_state() }) return }
func main() { srv := &clonesrv_t{ port: 5556, kvmap: make(map[string]*kvmsg.Kvmsg), } // Set up our clone server sockets srv.snapshot, _ = zmq.NewSocket(zmq.ROUTER) srv.snapshot.Bind(fmt.Sprint("tcp://*:", srv.port)) srv.publisher, _ = zmq.NewSocket(zmq.PUB) srv.publisher.Bind(fmt.Sprint("tcp://*:", srv.port+1)) srv.collector, _ = zmq.NewSocket(zmq.PULL) srv.collector.Bind(fmt.Sprint("tcp://*:", srv.port+2)) // Register our handlers with reactor reactor := zmq.NewReactor() reactor.AddSocket(srv.snapshot, zmq.POLLIN, func(e zmq.State) error { return snapshots(srv) }) reactor.AddSocket(srv.collector, zmq.POLLIN, func(e zmq.State) error { return collector(srv) }) reactor.AddChannelTime(time.Tick(1000*time.Millisecond), 1, func(v interface{}) error { return flush_ttl(srv) }) log.Println(reactor.Run(100 * time.Millisecond)) // precision: .1 seconds }
func main() { lbbroker := &lbbroker_t{} lbbroker.frontend, _ = zmq.NewSocket(zmq.ROUTER) lbbroker.backend, _ = zmq.NewSocket(zmq.ROUTER) defer lbbroker.frontend.Close() defer lbbroker.backend.Close() lbbroker.frontend.Bind("ipc://frontend.ipc") lbbroker.backend.Bind("ipc://backend.ipc") for client_nbr := 0; client_nbr < NBR_CLIENTS; client_nbr++ { go client_task() } for worker_nbr := 0; worker_nbr < NBR_WORKERS; worker_nbr++ { go worker_task() } // Queue of available workers lbbroker.workers = make([]string, 0, 10) // Prepare reactor and fire it up lbbroker.reactor = zmq.NewReactor() lbbroker.reactor.AddSocket(lbbroker.backend, zmq.POLLIN, func(e zmq.State) error { return handle_backend(lbbroker) }) lbbroker.reactor.Run(-1) }
// Start processing incoming messages from ØMQ func (transporter *ZmqTransporter) Start(server raft.Server) { transporter.server = server transporter.running = true go func() { reactor := zmq.NewReactor() reactor.AddSocket(transporter.incoming, zmq.POLLIN, func(zmq.State) error { transporter.parseIncomingPackets(); return nil }) var chId uint64 chId = reactor.AddChannel(transporter.stop, 1, func(interface{}) error { transporter.running = false reactor.RemoveChannel(chId) reactor.RemoveSocket(transporter.incoming) return nil }) for transporter.running { glog.V(2).Info("Entering reactor") err := reactor.Run(100 * time.Millisecond) if err != nil && err.Error() != "No sockets to poll, no channels to read" { glog.Infof("Got error from ZMQ reactor: %v", err) } } transporter.stop <- true }() }