Пример #1
0
func (c *cluster) refresh(server string) (err error) {
	var (
		connPool    connection.Pool = c.GetNodePool(server)
		conn        connection.Conn
		slotsMsgAck protocol.Message
	)
	if conn, err = connPool.Get(); err != nil {
		return
	}
	// 发送cluster slots
	if slotsMsgAck, err = conn.HandelMessage(protocol.ClusterSlots); err != nil {
		connPool.Remove(conn)
		return
	}
	connPool.Put(conn)
	// 处理返回值
	if slotsMsgAck.GetProtocolType() != protocol.ArraysType || slotsMsgAck.GetIntegersValue() <= 0 {
		return fmt.Errorf("slots数据无效")
	}
	var slotsMsgAckValue protocol.Message
	for _, slotsMsgAckValue = range slotsMsgAck.GetArraysValue() {
		if slotsMsgAckValue.GetProtocolType() != protocol.ArraysType || slotsMsgAckValue.GetIntegersValue() < 3 {
			return fmt.Errorf("slots节点数据无效")
		}
		var (
			slotsMsgAckValueArrays []protocol.Message = slotsMsgAckValue.GetArraysValue()
			nodeMsgValue           protocol.Message
			nodeMsgValueArrays     []protocol.Message
			connPoolList           []connection.Pool
		)
		if slotsMsgAckValueArrays[0].GetProtocolType() != protocol.IntegersType || slotsMsgAckValueArrays[1].GetProtocolType() != protocol.IntegersType {
			return fmt.Errorf("slots节点起始或者结束数据无效")
		}
		for _, nodeMsgValue = range slotsMsgAckValueArrays[2:] {
			if nodeMsgValue.GetProtocolType() != protocol.ArraysType || nodeMsgValue.GetIntegersValue() < 2 {
				return fmt.Errorf("slot节点数据无效")
			}
			nodeMsgValueArrays = nodeMsgValue.GetArraysValue()
			if nodeMsgValueArrays[0].GetProtocolType() != protocol.BulkStringsType {
				return fmt.Errorf("slot节点IP无效")
			}
			if nodeMsgValueArrays[1].GetProtocolType() != protocol.IntegersType {
				return fmt.Errorf("slot节点PORT无效")
			}
			connPoolList = append(
				connPoolList,
				c.GetNodePool(fmt.Sprintf("%s:%d", nodeMsgValueArrays[0].GetBytesValue(), nodeMsgValueArrays[1].GetIntegersValue())),
			)
		}
		c.clusterSlots.AddSlots(
			slotsMsgAckValueArrays[0].GetIntegersValue(),
			slotsMsgAckValueArrays[1].GetIntegersValue(),
			nodes.NewNodes().Set(connPoolList), // TODO 同一个List用同一个Nodes
		)
	}
	return
}
Пример #2
0
func forwardMsgToPool(msg protocol.Message, clusterConnPool connection.Pool, isMaster bool, isAsking bool) (msgAck protocol.Message) {
	var (
		clusterConn connection.Conn
		err         error
	)
	if clusterConn, err = clusterConnPool.Get(); err != nil {
		logger.Warningf("从连接池中获取句柄出错: %v", err)
		return protocol.ERR_ClusterSlotsConn
	}
	if isMaster {
		if err = clusterConn.Readwrite(); err != nil {
			goto failure
		}
	} else {
		if err = clusterConn.Readonly(); err != nil {
			goto failure
		}
	}
	if isAsking {
		if err = clusterConn.Asking(); err != nil {
			goto failure
		}
	}
	if msgAck, err = clusterConn.HandelMessage(msg); err != nil {
		goto failure
	}
	clusterConnPool.Put(clusterConn)
	return
failure:
	clusterConnPool.Remove(clusterConn)
	logger.Warningf("转发消息出错: %v", err)
	return protocol.ERR_ClusterSlotsForward
}