Documentation ¶
Overview ¶
Package maelstrom implements the Maelstrom protocol.
Index ¶
- type CommonBody
- type ErrorBody
- type Handler
- type HandlerFunc
- type Message
- type Node
- func (n *Node) Cluster() []string
- func (n *Node) Do(msg Message) error
- func (n *Node) Handle(typ string, handler Handler)
- func (n *Node) HandleFunc(typ string, handler HandlerFunc)
- func (n *Node) ID() string
- func (n *Node) RPC(dest, typ string, payload any, handler Handler) (Message, error)
- func (n *Node) Reply(req Message, typ string, payload any) (Message, error)
- func (n *Node) Send(dest, typ string, payload any) (Message, error)
- func (n *Node) Serve() error
- type Retrier
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CommonBody ¶
type CommonBody struct { // Type identifies the message type. Type string `json:"type"` // MsgID is a unique integer identifier. MsgID *uint64 `json:"msg_id,omitempty"` // InReplyTo is the identifier of the request in a // request/response context. InReplyTo *uint64 `json:"in_reply_to,omitempty"` }
CommonBody contains the fields present in all messages.
type ErrorBody ¶
type ErrorBody struct { CommonBody // Code indicates the type of error which occurred. Code uint64 `json:"code"` // Text is optional and may contain an explanatory message. Text *string `json:"text,omitempty"` }
ErrorBody represents the body of an "error" message.
type HandlerFunc ¶
The HandlerFunc type is an adapter to allow the use of ordinary functions as Maelstrom handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.
func (HandlerFunc) ServeMessage ¶
func (f HandlerFunc) ServeMessage(n *Node, msg Message)
ServeMessage calls f(msg).
type Message ¶
type Message struct { // Src identifies the node this message came from. Src string `json:"src"` // Dest identifies the node this message is to. Dest string `json:"dest"` // Body is the payload of the message. Body json.RawMessage `json:"body"` }
Message represents a message exchanged between clients and nodes in the Maelstrom cluster.
func NewMessage ¶
NewMessage creates a new message. msgID and inReplyTo are optional fields.
func (Message) CommonBody ¶
func (msg Message) CommonBody() (CommonBody, error)
CommonBody returns the fields present in all message types.
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node represents a cluster node.
func NewNode ¶
NewNode initializes a new cluster node. r and w are used to receive and send Maelstrom messages.
func (*Node) Cluster ¶
Cluster returns the identifiers of all the nodes in the cluster. It returns nil if the node has not been initialized.
func (*Node) HandleFunc ¶
func (n *Node) HandleFunc(typ string, handler HandlerFunc)
HandleFunc registers the handler function for messages of the specified type.
func (*Node) ID ¶
ID returns the identifier of the node. It returns an empty string if the node has not been initialized.
func (*Node) RPC ¶
RPC sends an RPC request to dest with the specified type and payload. The provided handler is executed when the RPC response is received. It returns the RPC request that has been sent. RPC is safe to call concurrently.
func (*Node) Reply ¶
Reply replies to the specified request with a message with the provided type and payload. It returns the reply that has been sent. Reply is safe to call concurrently.
type Retrier ¶
type Retrier struct {
// contains filtered or unexported fields
}
A Retrier resends requests.
func NewRetrier ¶
NewRetrier returns a new Retrier. It resends the requests using the provided node. It waits at least wt before sending the first retry. Then, it waits rt between retries.
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
broadcast
Broadcast solves Fly.io [challenge 3: Broadcast].
|
Broadcast solves Fly.io [challenge 3: Broadcast]. |
echo
Echo solves Fly.io [challenge 1: Echo].
|
Echo solves Fly.io [challenge 1: Echo]. |
uid
Uid solves Fly.io [challenge 2: Unique ID Generation].
|
Uid solves Fly.io [challenge 2: Unique ID Generation]. |