nats¶
A service that needs messaging early needs it between two goroutines. A service that needs it later needs it between two clusters. Those are usually two pieces of code written a year apart, and the second one is a rewrite.
NATS makes them the same code. This module makes that an estate convention rather than something each project rediscovers.
v0.1.0 runs in-process only
An embedded server with a network listener is refused, cluster routes included. There is no TLS or authentication configuration yet, so a listening server would accept anything that could reach it, with no credential and nothing encrypted. Refusing means the unsafe surface does not exist rather than existing with a warning beside it.
Connecting a client to a cluster somebody else operates and secures is unaffected — and is what most services should do.
Start here¶
-
A server and a client in one process, sending a message, in about twenty lines.
-
Every setting, what it defaults to, and why the default is what it is.
-
Why embedding is not a compromise
The embedded server is the same server. What that means for how far it scales.
-
Read before assuming. The refusals are deliberate and several are load-bearing.
-
A real server and client for a test, in the right order. No broker, no mock, under a second.
The shape¶
import (
gonats "gitlab.com/phpboyscout/go/nats"
"gitlab.com/phpboyscout/go/nats/client"
"gitlab.com/phpboyscout/go/nats/server"
)
srv, err := server.Register(ctx, "nats", controller, log, gonats.ServerSettings{
InProcessOnly: true,
})
cli, err := client.Register(ctx, "nats-client", controller,
gonats.ClientSettings{Name: "my-service"}, nil,
client.InProcess(srv))
Register the server before the client. go/controls starts its services concurrently, and an
in-process dial waits for the server to finish starting, up to ServerSettings.ReadyTimeout. It stops
them in reverse registration order, so the client drains before the server goes away underneath it.
When the broker moves out of the process, the first call goes away and ClientSettings.URL is set.
Everything that publishes or subscribes is untouched.