Documentation
¶
Index ¶
- Constants
- func ErrorCode(err error) int
- func ErrorCodeText(code int) string
- type HandlerFunc
- type InitMessageBody
- type KV
- func (kv *KV) CompareAndSwap(ctx context.Context, key string, from, to any, createIfNotExists bool) error
- func (kv *KV) Read(ctx context.Context, key string) (any, error)
- func (kv *KV) ReadInt(ctx context.Context, key string) (int, error)
- func (kv *KV) ReadInto(ctx context.Context, key string, v any) error
- func (kv *KV) Write(ctx context.Context, key string, value any) error
- type Message
- type MessageBody
- type Node
- func (n *Node) Handle(typ string, fn HandlerFunc)
- func (n *Node) ID() string
- func (n *Node) Init(id string, nodeIDs []string)
- func (n *Node) NodeIDs() []string
- func (n *Node) RPC(dest string, body any, handler HandlerFunc) error
- func (n *Node) Reply(req Message, body any) error
- func (n *Node) Run() error
- func (n *Node) Send(dest string, body any) error
- func (n *Node) SyncRPC(ctx context.Context, dest string, body any) (Message, error)
- type RPCError
Constants ¶
const ( LinKV = "lin-kv" SeqKV = "seq-kv" LWWKV = "lww-kv" )
Types of key/value stores.
const ( Timeout = 0 NotSupported = 10 MalformedRequest = 12 Crash = 13 Abort = 14 KeyDoesNotExist = 20 KeyAlreadyExists = 21 PreconditionFailed = 22 TxnConflict = 30 )
RPC error code constants.
Variables ¶
This section is empty.
Functions ¶
func ErrorCode ¶
ErrorCode returns the error code from err. Returns -1 if err does not have an *RPCError.
func ErrorCodeText ¶
ErrorCodeText returns the text representation of an error code.
Types ¶
type HandlerFunc ¶
HandlerFunc is the function signature for a message handler.
type InitMessageBody ¶
type InitMessageBody struct { MessageBody NodeID string `json:"node_id,omitempty"` NodeIDs []string `json:"node_ids,omitempty"` }
InitMessageBody represents the message body for the "init" message.
type KV ¶
type KV struct {
// contains filtered or unexported fields
}
KV represents a client to the key/value store service.
func (*KV) CompareAndSwap ¶
func (kv *KV) CompareAndSwap(ctx context.Context, key string, from, to any, createIfNotExists bool) error
CompareAndSwap updates the value for a key if its current value matches the previous value. Creates the key if createIfNotExists is true.
Returns an *RPCError with a code of PreconditionFailed if the previous value does not match. Return a code of KeyDoesNotExist if the key did not exist.
func (*KV) Read ¶
Read returns the value for a given key in the key/value store. Returns an *RPCError error with a KeyDoesNotExist code if the key does not exist.
type Message ¶
type Message struct { Src string `json:"src,omitempty"` Dest string `json:"dest,omitempty"` Body json.RawMessage `json:"body,omitempty"` }
Message represents a message sent from Src node to Dest node. The body is stored as unparsed JSON so the handler can parse it itself.
type MessageBody ¶
type MessageBody struct { // Message type. Type string `json:"type,omitempty"` // Optional. Message identifier that is unique to the source node. MsgID int `json:"msg_id,omitempty"` // Optional. For request/response, the msg_id of the request. InReplyTo int `json:"in_reply_to,omitempty"` // Error code, if an error occurred. Code int `json:"code,omitempty"` // Error message, if an error occurred. Text string `json:"text,omitempty"` }
MessageBody represents the reserved keys for a message body.
type Node ¶
type Node struct { // Stdin is for reading messages in from the Maelstrom network. Stdin io.Reader // Stdout is for writing messages out to the Maelstrom network. Stdout io.Writer // contains filtered or unexported fields }
Node represents a single node in the network.
func NewNode ¶
func NewNode() *Node
NewNode returns a new instance of Node connected to STDIN/STDOUT.
func (*Node) Handle ¶
func (n *Node) Handle(typ string, fn HandlerFunc)
Handle registers a message handler for a given message type. Will panic if registering multiple handlers for the same message type.
func (*Node) ID ¶
ID returns the identifier for this node. Only valid after "init" message has been received.
func (*Node) Init ¶
Init is used for initializing the node. This is normally called after receiving an "init" message but it can also be called manually when initializing unit tests.
func (*Node) NodeIDs ¶
NodeIDs returns a list of all node IDs in the cluster. This list include the local node ID and is the same order across all nodes. Only valid after "init" message has been received.
func (*Node) RPC ¶
func (n *Node) RPC(dest string, body any, handler HandlerFunc) error
RPC sends an async RPC request. Handler invoked when response message received.
func (*Node) Run ¶
Run executes the main event handling loop. It reads in messages from STDIN and delegates them to the appropriate registered handler. This should be the last function executed by main().
type RPCError ¶
RPCError represents a Maelstrom RPC error.
func NewRPCError ¶
NewRPCError returns a new instance of RPCError.
func (*RPCError) MarshalJSON ¶
MarshalJSON marshals the error into JSON format.