Esempio n. 1
0
// TODO(alexyer): Implement proper error handling and result return.
func (c *GhostClient) process(cmd *protocol.Command) (*protocol.Reply, error) {
	for i := 0; i <= c.opt.GetMaxRetries(); i++ {
		cn, _, err := c.conn()
		if err != nil {
			fmt.Println(err)
			return nil, err
		}

		marshaledCmd, err := proto.Marshal(cmd)
		if err != nil {
			fmt.Println(err)
			c.putConn(cn, err)
			return nil, err
		}

		msgSize := ghost.UintToByteArray(uint64(len(marshaledCmd)))
		msg := append(msgSize, marshaledCmd...)

		if _, err := cn.Write(msg); err != nil {
			fmt.Println(err)
			c.putConn(cn, err)
			return nil, err
		}

		reply, err := c.getReply(cn)

		c.putConn(cn, err)
		return reply, err
	}

	return nil, errors.New("ghost: exceeded maximum number of retries")
}
Esempio n. 2
0
func (c *client) handleCommand() {
	for {
		res, err := c.Exec()

		if err != nil {
			if err != io.EOF {
				log.Print(err)
				c.Server.logger.Print(err)
			}
			c.Conn.Close()
			return
		}

		replySize := ghost.UintToByteArray(uint64(len(res)))

		if _, err := c.Conn.Write(append(replySize, res...)); err != nil {
			log.Print(err)
			c.Server.logger.Print(err)
			c.Conn.Close()
			return
		}
	}
}