當前位置: 首頁>>代碼示例>>Golang>>正文


Golang raft.Command類代碼示例

本文整理匯總了Golang中github.com/goraft/raft.Command的典型用法代碼示例。如果您正苦於以下問題:Golang Command類的具體用法?Golang Command怎麽用?Golang Command使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Command類的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: doOrProxyCommandOnce

func (s *RaftServer) doOrProxyCommandOnce(command raft.Command) (interface{}, error) {

	if s.raftServer.State() == raft.Leader {
		value, err := s.raftServer.Do(command)
		if err != nil {
			log.Error("Cannot run command %#v. %s", command, err)
		}
		return value, err
	} else {
		if leader, ok := s.leaderConnectString(); !ok {
			return nil, errors.New("Couldn't connect to the cluster leader...")
		} else {
			var b bytes.Buffer
			if err := json.NewEncoder(&b).Encode(command); err != nil {
				return nil, err
			}
			resp, err := http.Post(leader+"/process_command/"+command.CommandName(), "application/json", &b)
			if err != nil {
				return nil, err
			}
			defer resp.Body.Close()
			body, err2 := ioutil.ReadAll(resp.Body)

			if resp.StatusCode != 200 {
				return nil, errors.New(strings.TrimSpace(string(body)))
			}

			var js interface{}
			json.Unmarshal(body, &js)
			return js, err2
		}
	}
	return nil, nil
}
開發者ID:qz267,項目名稱:influxdb,代碼行數:34,代碼來源:raft_server.go

示例2: SendCommand

// SendCommand sends command to other peer and receives reply
func (transporter *ZmqTransporter) SendCommand(peer string, command raft.Command) error {
	socket, err := transporter.getSocketFor(peer)
	if err != nil {
		return fmt.Errorf("unable to create socket for peer %s: %v", peer, err)
	}

	buf := new(bytes.Buffer)
	buf.WriteByte(requestCommand)

	encoder := json.NewEncoder(buf)
	encoder.Encode(command.CommandName())
	encoder.Encode(command)

	_, err = socket.SendBytes(buf.Bytes(), 0)
	if err != nil {
		return fmt.Errorf("unable to send command %s to peer %s: %v", command.CommandName(), peer, err)
	}

	response, err := socket.RecvBytes(0)
	if err != nil {
		return fmt.Errorf("unable to receive %s response from peer %s: %v", command.CommandName(), peer, err)
	}

	var result interface{}
	err = json.Unmarshal(response, &result)
	if err != nil {
		return fmt.Errorf("unable to decode %s response from peer %s: %v", command.CommandName(), peer, err)
	}

	if result != nil {
		return fmt.Errorf("error response to command %s from peer %s: %v", command.CommandName(), peer, result)
	}

	return nil
}
開發者ID:pombredanne,項目名稱:czar,代碼行數:36,代碼來源:zeromq.go

示例3: SendCommandToServer

func SendCommandToServer(url string, command raft.Command) (interface{}, error) {
	var b bytes.Buffer
	if err := json.NewEncoder(&b).Encode(command); err != nil {
		return nil, err
	}
	resp, err := http.Post(url+"/process_command/"+command.CommandName(), "application/json", &b)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	body, err2 := ioutil.ReadAll(resp.Body)

	if resp.StatusCode != 200 {
		return nil, errors.New(strings.TrimSpace(string(body)))
	}

	return body, err2

}
開發者ID:jhermann,項目名稱:influxdb,代碼行數:19,代碼來源:raft_server.go


注:本文中的github.com/goraft/raft.Command類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。