Skip to content

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

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.