maelstrom

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 11 Imported by: 0

README

Maelstrom

Go Reference

Package maelstrom implements the Maelstrom protocol.

Fly.io's Distributed Systems Challenges

The commands under /cmd solve Fly.io's distributed systems challenges.

Documentation

Overview

Package maelstrom implements the Maelstrom protocol.

Index

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 Handler

type Handler interface {
	ServeMessage(*Node, Message)
}

A Handler responds to a Maelstrom message.

type HandlerFunc

type HandlerFunc func(*Node, Message)

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

func NewMessage(dest, src, typ string, payload any, msgID, inReplyTo *uint64) (Message, error)

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.

func (Message) String

func (msg Message) String() string

String returns the string representation of the message.

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node represents a cluster node.

func NewNode

func NewNode(r io.Reader, w io.Writer) *Node

NewNode initializes a new cluster node. r and w are used to receive and send Maelstrom messages.

func (*Node) Cluster

func (n *Node) Cluster() []string

Cluster returns the identifiers of all the nodes in the cluster. It returns nil if the node has not been initialized.

func (*Node) Do

func (n *Node) Do(msg Message) error

Do sends a message. Do is safe to call concurrently.

func (*Node) Handle

func (n *Node) Handle(typ string, handler Handler)

Handle registers the handler for messages of the specified type.

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

func (n *Node) ID() string

ID returns the identifier of the node. It returns an empty string if the node has not been initialized.

func (*Node) RPC

func (n *Node) RPC(dest, typ string, payload any, handler Handler) (Message, error)

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

func (n *Node) Reply(req Message, typ string, payload any) (Message, error)

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.

func (*Node) Send

func (n *Node) Send(dest, typ string, payload any) (Message, error)

Send sends a fire-and-forget message (without identifier) to dest with the specified type and payload. It returns the message that has been sent. Send is safe to call concurrently.

func (*Node) Serve

func (n *Node) Serve() error

Serve starts serving messages.

type Retrier

type Retrier struct {
	// contains filtered or unexported fields
}

A Retrier resends requests.

func NewRetrier

func NewRetrier(node *Node, wt, rt time.Duration) *Retrier

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.

func (*Retrier) Add

func (r *Retrier) Add(req Message) error

Add registers a new pending request. Add is safe to call concurrently.

func (*Retrier) Remove

func (r *Retrier) Remove(resp Message) error

Remove removes the request corresponding to the provided response from the list of pending requests. Remove is safe to call concurrently.

func (*Retrier) Retry

func (r *Retrier) Retry(ctx context.Context)

Retry starts handling pending requests.

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].

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL