-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Description
NATS is used across the framework for various interface implementations. Each creates a separate connection. This is probably wasteful. We could potentially reuse the connection via some sort of util/nats
package or by allowing the profile to create/define the connection and pass it into each interface implementation.
So one possible scenario is the connection pool
import "util/nats"
// returns an existing connection or creates one
conn, err := nats.Conn()
Nats says A Conn represents a bare connection to a nats-server. It can send and receive []byte payloads. The connection is safe to use in multiple Go routines concurrently.
So that would be totally fine to reuse.
The alternative is nats profile function creates and passes it in
func NatsProfile() {
conn, err := nats.Connect(url)
reg := natsReg.NewRegistry(
regOpt.NatsConn(conn),
)
bkr := natsBkr.NewBroker(
bkrOpt.NatsConn(conn),
)
}
Something like this would maybe then require all interfaces to have some common use of an options
package to ease this or generics implementation of an option func, just something to make it easier to pass in one thing.
To be honest, I think util/nats
is the fastest and easiest way to go and mimics a lot of tcp connection pooling.