Example #1
0
func (b *Broker) Run() {
	b.setup()

	_, err := b.sock.Bind(b.endpoint)
	if err != nil {
		b.sock.Destroy()
		b.sock = nil
		return
	}
	// Anything else to do?
	poller, err := czmq.NewPoller(b.sock)
	if err != nil {
		log.Fatalf("czmq.NewPoller failed: %s\n", err)
	}

	for {
		s := czmqPollerSafeWait(poller, durationInMilliseconds(b.HeartbeatInterval))
		if s != nil {
			b.once(s)
		}

		for _, entry := range b.workers {
			if time.Now().After(entry.expiration) {
				// We haven't seen this worker for too long, let's delete it.
				log.Printf("Worker hasn't been seen in a while, removing it.")
				b.removeWorker(entry.address)
			} else {
				// Healthy worker. Let's tell the worker we're still here if needed.
				if time.Now().After(entry.nextSendHeartbeatTime) {
					b.sendHeartbeat(entry)
				}
			}
		}
	}
}
Example #2
0
func (w *Worker) ensure_connected() error {
	if w.sock != nil {
		return nil
	}

	w.sock = newSock(czmq.Dealer)

	// Setup CURVE if a key is set.
	if len(w.CurveServerPublicKey) > 0 {
		w.sock.SetCurveServerkey(w.CurveServerPublicKey)
	}
	if w.CurveCertificate == nil {
		w.CurveCertificate = czmq.NewCert()
	}
	w.CurveCertificate.Apply(w.sock)

	log.WithFields(log.Fields{"endpoint": w.broker}).Info("Connecting")
	err := w.sock.Connect(w.broker)
	if err != nil {
		w.sock.Destroy()
		w.sock = nil
		return err
	}

	w.poller, err = czmq.NewPoller(w.sock)
	if err != nil {
		w.sock.Destroy()
		return err
	}

	err = w.sendReady()
	if err != nil {
		return err
	}
	return nil
}