https://github.com/octu0/nats-pool
connection pooling for nats.go
https://github.com/octu0/nats-pool
connection-pool nats nats-client natsio
Last synced: about 1 year ago
JSON representation
connection pooling for nats.go
- Host: GitHub
- URL: https://github.com/octu0/nats-pool
- Owner: octu0
- License: apache-2.0
- Created: 2020-08-18T11:11:48.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-09-04T14:17:17.000Z (almost 4 years ago)
- Last Synced: 2024-06-19T05:59:52.839Z (almost 2 years ago)
- Topics: connection-pool, nats, nats-client, natsio
- Language: Go
- Homepage:
- Size: 40 KB
- Stars: 9
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `nats-pool`
[](https://github.com/octu0/nats-pool/blob/master/LICENSE)
[](https://godoc.org/github.com/octu0/nats-pool)
[](https://goreportcard.com/report/github.com/octu0/nats-pool)
[](https://github.com/octu0/nats-pool)
`nats-pool` connection pooling for [nats.go](https://github.com/nats-io/nats.go)
## Installation
```bash
go get github.com/octu0/nats-pool
```
## Example
```go
import (
"github.com/nats-io/nats.go"
"github.com/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, err error) {
...
}),
)
)
func main() {
nc, err := connPool.Get()
if err != nil {
panic(err)
}
// release *nats.Conn to pool
defer connPool.Put(nc)
:
nc.Subscribe("subject.a.b.c", func(msg *nats.Msg) {
...
})
nc.Publish("foo.bar", []byte("hello world"))
}
```
## Benchmark
Here are the benchmark results for a simple case with multiple PubSub.
```
$ go test -bench=. -benchmem
goos: darwin
goarch: amd64
pkg: github.com/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.com/octu0/nats-pool 3.829s
```
## License
Apache 2.0, see LICENSE file for details.