Exemple #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.IntToByteArray(int64(len(marshaledCmd)))

		if _, err := cn.Write(append(msgSize, marshaledCmd...)); 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")
}
Exemple #2
0
func (s *Server) handleCommand(c *client) {
	for {
		if err := s.read(c.Conn, c.MsgHeader); err != nil {
			log.Print(err)
			c.Conn.Close()
			return
		}

		// Read command to client buffer
		if err := s.read(c.Conn, c.MsgBuffer); err != nil {
			log.Print(err)
			c.Conn.Close()
			return
		}

		res, err := c.Exec()

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

		replySize := ghost.IntToByteArray(int64(len(res)))

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