Skip to content
floatdropPublic

About

Erlang-style processes for Go over gRPC

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

275 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

grpcproc

CI Go Reference Coverage License Awesome Go

Erlang-style processes for Go over gRPC.

A process is a goroutine with a typed mailbox and an address any node in the cluster can use. Send, Call, Monitor and Exit work the same whether the process runs in this binary or on another node, and supervisors restart what fails.

go get github.com/floatdrop/grpcproc

Documentation · Quick start · Concepts · API reference

A quick look

Two nodes, each on its own gRPC server. A process on one, a call and a monitor from the other:

// inventory is a process: a goroutine with a mailbox. The mailbox holds
// *shoppb.Reserve and nothing else, and every message is a call, answered
// with a *shoppb.Reserved or an error.
func inventory(p *grpcproc.Process[*shoppb.Reserve]) error {
	left := map[string]int64{"apple": 3}
	for {
		m, err := p.Receive()
		if err != nil {
			return err // asked to exit, or the node is stopping
		}
		if left[m.Body.Sku] < m.Body.Qty {
			_ = m.Reply(nil, fmt.Errorf("only %d %s left", left[m.Body.Sku], m.Body.Sku))
			continue
		}
		left[m.Body.Sku] -= m.Body.Qty
		_ = m.Reply(&shoppb.Reserved{Sku: m.Body.Sku, Left: left[m.Body.Sku]}, nil)
	}
}

func main() {
	ctx := context.Background()

	// Two nodes, each on its own gRPC server; see node() below.
	warehouse, shop := node(ctx, "warehouse"), node(ctx, "shop")

	// The process runs on warehouse, under a name.
	_, err := warehouse.Spawn(inventory, grpcproc.WithName("stock"))
	check(err)

	// From shop it is a node name and a process name. The address carries the
	// mailbox type, so the compiler checks what is sent to it.
	stock := grpcproc.Named[*shoppb.Reserve]("warehouse", "stock")
	for range 2 {
		r, err := stock.Call[*shoppb.Reserved](ctx, shop, &shoppb.Reserve{Sku: "apple", Qty: 2})
		if err != nil {
			fmt.Println("reserve failed:", err) // the handler's error, as a *grpcproc.RemoteError
			continue
		}
		fmt.Println("reserved, left:", r.Left)
	}

	// A monitor across nodes works as one within a node does. A process on
	// shop monitors stock, then asks it to exit; the Down arrives with the
	// reason, as it would for a crash or a lost node.
	exited := make(chan string)
	_, err = shop.Spawn(func(p *grpcproc.Process[proto.Message]) error {
		p.Monitor(stock)
		check(p.Exit(stock, "closing"))
		m, err := p.Receive() // the Down: nothing else is sent to this process
		if err != nil {
			return err
		}
		exited <- m.Down.Reason
		return nil
	})
	check(err)
	fmt.Println("stock exited:", <-exited)

	_, err = stock.Call[*shoppb.Reserved](ctx, shop, &shoppb.Reserve{Sku: "apple", Qty: 1})
	fmt.Println("no such process:", errors.Is(err, grpcproc.ErrNoProc))

	check(shop.Stop(ctx))
	check(warehouse.Stop(ctx))
}
reserved, left: 1
reserve failed: only 1 apple left
stock exited: closing
no such process: true

examples/quickstart is the whole program, node() included: a grpcproc.Node is a value you construct, register on your *grpc.Server next to your other services, start and stop.

What is in the box

  • Processes with typed mailboxes, Send and Call, monitors and links, exit reasons: the core, which depends on gRPC and protobuf, and on fsm, which has no dependencies of its own. Concepts
  • Actors and supervision trees in grpcproc/actor: a struct with a method per kind of message, restarted by a supervisor when it fails, and started from any node, monitored from before it runs. Supervision
  • Pub/sub in grpcproc/pubsub: topics a process publishes to and any node subscribes to, with the last few events kept for whoever comes late, and each event sent once to each node. Guide
  • Process groups in grpcproc/groups: named sets of processes on any node, after Erlang's pg, which every node lists from its own copy and any process can watch. Guide
  • Contracts as proto services: a service marked grpcproc.v1.process is what a process answers and takes, and protoc-gen-grpcproc, run by buf generate, writes its address with a method per rpc and the handler that dispatches to it, as gRPC's stubs are written. Guide
  • An Inspector and grpcprocctl: every process, its mailbox and what it says about itself, from a terminal or an AI agent over MCP. Guide
  • Cron and leader election in grpcproc/cron and grpcproc/leader: jobs on crontab schedules, every run a process of its own; and a singleton that runs on the elected leader and carries its state to the next, so a job can run once in a cluster.
  • Sagas in grpcproc/saga: work that spans services and outlasts a process, as an fsm machine and its data per run, kept in a store, with retries, durable timers and signals; Sequence for steps with what undoes each. Guide
  • OpenTelemetry in grpcproc/otel, etcd membership in grpcproc/etcd, sagas kept in PostgreSQL in grpcproc/saga/postgres, and grpcproctest, which runs a cluster inside one go test, partitions and crashes included. Testing
  • A tutorial: an AI agent runtime, with a process per conversation, the model on GPU nodes, tools on a sandbox node and tokens streamed to the browser, run as one program or as a program per node from the same code. An agent runtime

docs/DESIGN.md has the wire protocol and the reasons behind the choices; benchmarks measures it against GoAkt, Hollywood, Proto.Actor and Ergo.

License

MIT, see LICENSE.

About

Erlang-style processes for Go over gRPC

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages