Exemplo n.º 1
0
func HandleConnection(conn net.Conn) {
	for {
		var cmd *command.Command = command.DecodeRedisProtocol(conn)
		if cmd == nil {
			break
		}
		//fmt.Println("recv: "+ command.EncodeText(*cmd))
		//通过key的第一部分来确定分区
		if cmd.Argc > 1 {
			index := strings.Index(cmd.Args[1], ".")
			if index == -1 {
				cmd.PartitionKey = cmd.Args[1]
			} else {
				cmd.PartitionKey = cmd.Args[1][:index]
			}
			server, _ := c.Get(cmd.PartitionKey)
			//fmt.Println("lookup node for key:"+cmd.PartitionKey+"->"+server)
			if server != hostname {
				//fmt.Println("forward to another node!")
				Send(server, command.EncodeRedisProtocol(*cmd))
			} else {
				DoCommand(conn, *cmd)
			}
		} else {
			DoCommand(conn, *cmd)
		}
	}

}