项目作者: octu0

项目描述 :
connection pooling for nats.go
高级语言: Go
项目地址: git://github.com/octu0/nats-pool.git
创建时间: 2020-08-18T11:11:48Z
项目社区:https://github.com/octu0/nats-pool

开源协议:Apache License 2.0

下载


nats-pool

Apache License
GoDoc
Go Report Card
Releases

nats-pool connection pooling for nats.go

Installation

  1. go get github.com/octu0/nats-pool

Example

  1. import (
  2. "github.com/nats-io/nats.go"
  3. "github.com/octu0/nats-pool"
  4. )
  5. var (
  6. // 100 connections pool
  7. connPool = pool.New(100, "nats://localhost:4222",
  8. nats.NoEcho(),
  9. nats.Name("client/1.0"),
  10. nats.ErrorHandler(func(nc *nats.Conn, sub *nats.Subscription, err error) {
  11. ...
  12. }),
  13. )
  14. )
  15. func main() {
  16. nc, err := connPool.Get()
  17. if err != nil {
  18. panic(err)
  19. }
  20. // release *nats.Conn to pool
  21. defer connPool.Put(nc)
  22. :
  23. nc.Subscribe("subject.a.b.c", func(msg *nats.Msg) {
  24. ...
  25. })
  26. nc.Publish("foo.bar", []byte("hello world"))
  27. }

Benchmark

Here are the benchmark results for a simple case with multiple PubSub.

  1. $ go test -bench=. -benchmem
  2. goos: darwin
  3. goarch: amd64
  4. pkg: github.com/octu0/nats-pool
  5. BenchmarkSimpleConnPubSub/NoPool-8 5000 261422 ns/op 124696 B/op 177 allocs/op
  6. BenchmarkSimpleConnPubSub/UsePool-8 35050 29524 ns/op 4656 B/op 50 allocs/op
  7. PASS
  8. ok github.com/octu0/nats-pool 3.829s

License

Apache 2.0, see LICENSE file for details.