nats-pool
connection pooling fornats.go
go get github /octu0/nats-pool
import(
"github /nats-io/nats.go"
"github /octu0/nats-pool"
)
var(
// 100 connections pool
connPool=pool.New(100,"nats://localhost:4222",
nats.NoEcho(),
nats.Name("client/1.0"),
nats.ErrorHandler(func(nc*nats.Conn,sub*nats.Subscription,errerror) {
...
}),
)
)
funcmain() {
nc,err:=connPool.Get()
iferr!=nil{
panic(err)
}
// release *nats.Conn to pool
deferconnPool.Put(nc)
:
nc.Subscribe("subject.a.b.c",func(msg*nats.Msg) {
...
})
nc.Publish("foo.bar",[]byte("hello world"))
}
Here are the benchmark results for a simple case with multiple PubSub.
$ go test -bench=. -benchmem
goos: darwin
goarch: amd64
pkg: github /octu0/nats-pool
BenchmarkSimpleConnPubSub/NoPool-8 5000 261422 ns/op 124696 B/op 177 allocs/op
BenchmarkSimpleConnPubSub/UsePool-8 35050 29524 ns/op 4656 B/op 50 allocs/op
PASS
ok github /octu0/nats-pool 3.829s
Apache 2.0, see LICENSE file for details.