13 Star 51 Fork 0

Gitee 极速下载/etcd

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/coreos/etcd
克隆/下载
command.go 2.35 KB
一键复制 编辑 原始数据 按行查看 历史
package raft
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
)
//------------------------------------------------------------------------------
//
// Globals
//
//------------------------------------------------------------------------------
var commandTypes map[string]Command
func init() {
commandTypes = map[string]Command{}
}
//------------------------------------------------------------------------------
//
// Typedefs
//
//------------------------------------------------------------------------------
// A command represents an action to be taken on the replicated state machine.
type Command interface {
CommandName() string
Apply(server *Server) (interface{}, error)
}
type CommandEncoder interface {
Encode(w io.Writer) error
Decode(r io.Reader) error
}
//------------------------------------------------------------------------------
//
// Functions
//
//------------------------------------------------------------------------------
//--------------------------------------
// Instantiation
//--------------------------------------
// Creates a new instance of a command by name.
func newCommand(name string, data []byte) (Command, error) {
// Find the registered command.
command := commandTypes[name]
if command == nil {
return nil, fmt.Errorf("raft.Command: Unregistered command type: %s", name)
}
// Make a copy of the command.
v := reflect.New(reflect.Indirect(reflect.ValueOf(command)).Type()).Interface()
copy, ok := v.(Command)
if !ok {
panic(fmt.Sprintf("raft: Unable to copy command: %s (%v)", command.CommandName(), reflect.ValueOf(v).Kind().String()))
}
// If data for the command was passed in the decode it.
if data != nil {
if encoder, ok := copy.(CommandEncoder); ok {
if err := encoder.Decode(bytes.NewReader(data)); err != nil {
return nil, err
}
} else {
json.NewDecoder(bytes.NewReader(data)).Decode(copy)
}
}
return copy, nil
}
//--------------------------------------
// Registration
//--------------------------------------
// Registers a command by storing a reference to an instance of it.
func RegisterCommand(command Command) {
if command == nil {
panic(fmt.Sprintf("raft: Cannot register nil"))
} else if commandTypes[command.CommandName()] != nil {
panic(fmt.Sprintf("raft: Duplicate registration: %s", command.CommandName()))
return
}
commandTypes[command.CommandName()] = command
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mirrors/etcd.git
git@gitee.com:mirrors/etcd.git
mirrors
etcd
etcd
v0.1.0

搜索帮助

0d507c66 1850385 C8b1a773 1850385