Exemplo n.º 1
0
// Main broker working loop
func (self *loadBalancer) Run() {
	for {
		items := zmq.PollItems{
			zmq.PollItem{Socket: self.frontend, Events: zmq.POLLIN},
			zmq.PollItem{Socket: self.backend, Events: zmq.POLLIN},
		}

		_, err := zmq.Poll(items, HEARTBEAT_INTERVAL)
		if err != nil {
			log.Fatal("Non items for polling")
		}

		if items[0].REvents&zmq.POLLIN != 0 {
			msg, _ := self.frontend.RecvMultipart(0)
			// TODO: check msg parts
			requestId := msg[0]
			msg = msg[2:]
			self.processClient(requestId, msg)
		}
		if items[1].REvents&zmq.POLLIN != 0 {
			msg, _ := self.backend.RecvMultipart(0)
			sender := msg[0]
			msg = msg[2:]
			self.processWorker(sender, msg)
		}

		if self.heartbeatAt.Before(time.Now()) {
			self.purgeWorkers()
			for elem := self.waiting.Front(); elem != nil; elem = elem.Next() {
				worker, _ := elem.Value.(*lbWorker)
				self.sendToWorker(worker, SIGNAL_HEARTBEAT, nil, nil)
			}
			self.heartbeatAt = time.Now().Add(HEARTBEAT_INTERVAL)
		}
	}
}
Exemplo n.º 2
0
func (self *lbWorker) recv(reply [][]byte) (msg [][]byte) {

	Dump(reply)
	//  Format and send the reply if we were provided one
	if len(reply) == 0 && self.expectReply {
		panic("Error reply")
	}
	// log.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ******************************** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")

	if len(reply) > 0 {
		if len(self.replyTo) == 0 {
			panic("Error replyTo")
		}
		reply = append([][]byte{self.replyTo, nil}, reply...)
		// log.Println("SENDING TO BROKER >>>>>>>>>>>>")
		Dump(reply)
		self.sendToBroker(SIGNAL_REPLY, nil, reply)
	}

	self.expectReply = true

	for {
		items := zmq.PollItems{
			zmq.PollItem{Socket: self.worker, Events: zmq.POLLIN},
		}

		_, err := zmq.Poll(items, self.heartbeat)
		if err != nil {
			panic(err) //  Interrupted
		}

		// log.Printf("RECV %d", len(items))
		// log.Printf("RECV items[0] %d", items[0])
		// log.Printf("RECV POLLIN %d", items[0].REvents&zmq.POLLIN)

		if item := items[0]; item.REvents&zmq.POLLIN != 0 {

			// log.Printf("RECV2 %d", len(items))

			msg, _ = self.worker.RecvMultipart(0)

			// log.Printf("RECV3 %d", len(msg))

			if self.verbose {
				log.Println("Received message from broker: ")
				//Dump(msg)
			}
			self.liveness = HEARTBEAT_LIVENESS
			Dump(msg)
			if len(msg) < 2 {
				panic("Invalid msg") //  Interrupted
			}

			switch command := string(msg[1]); command {
			case SIGNAL_REQUEST:
				// log.Printf("Signal REQUEST received")
				//  We should pop and save as many addresses as there are
				//  up to a null part, but for now, just save one...
				log.Println("PRE REQUEST FROM WORKER")
				Dump(msg)
				self.replyTo = msg[2]
				// log.Println("REPLY TO: " + string(self.replyTo))
				msg = msg[4:6]
				// log.Println("DUMP 2")
				Dump(msg)
				// log.Println("END DUMP")
				return
			case SIGNAL_HEARTBEAT:
				// log.Printf("Signal HEARBEAT received")
				// do nothin
			case SIGNAL_DISCONNECT:
				// log.Printf("Signal DISCONNECT received")
				self.reconnectToBroker()
			default:
				log.Println("Invalid input message:")
				//Dump(msg)
			}
		} else if self.liveness--; self.liveness <= 0 {
			if self.verbose {
				log.Println("Disconnected from broker - retrying...")
			}
			time.Sleep(self.reconnect)
			self.reconnectToBroker()
		}

		//  Send HEARTBEAT if it's time
		if self.heartbeatAt.Before(time.Now()) {
			self.sendToBroker(SIGNAL_HEARTBEAT, nil, nil)
			self.heartbeatAt = time.Now().Add(self.heartbeat)
		}
	}

	return
}