cluster
Two ygo servers that share their documents through a backplane, as
instances behind a load balancer do.
go run ./examples/cluster
alice on server one, bob on server two
both see: [alice: draft the plan bob: review it]
bob sees alice's presence: {"name":"alice"}
alice left: her presence is gone on server two
Alice's client connects to server one, Bob's to server two. Their edits and presence cross between the servers only through the backplane, the in-process hub here (backplane.NewMemory).
Things to copy right:
- One connection per server. Each server gets its own
hub.Conn() (or its own Redis or NATS backplane value): a server never gets back what it published, so two servers sharing one connection would not hear each other.
- A shared Store in production. These servers have none, so the example proves the backplane carries the updates. In production every instance needs the same
persist.Store (a database all instances reach): a document loaded on an instance after others edited it comes from the Store, and the backplane only relays live updates.
- Shut down in order: clients, then each server's HTTP listener, then the server (
Close also closes its backplane connection), each with its own bounded context.
Across processes
The same with Redis, in your own module (go get github.com/Deln0r/ygo/server/backplane/redis):
rdb := redis.NewClient(&redis.Options{Addr: "redis.internal:6379"})
bp, err := redisbp.NewStreams(rdb) // or redisbp.New(rdb) for Pub/Sub
srv := server.New(server.Options{Store: sharedStore, Backplane: bp})
or with NATS (github.com/Deln0r/ygo/server/backplane/nats). Each instance builds its own backplane value against the same Redis or NATS. See server/backplane/redis and server/backplane/nats for what each guarantees.