nats_pool

package module
v0.0.0-...-c7bed8c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2017 License: MIT Imports: 2 Imported by: 4

README

nats-pool

A simple, automatically expanding/cleaning thread-safe connection pool for nats.io.

GoDoc

Installation

go get github.com/akaumov/nats-pool

Documentation

Documentation

Testing

go test github.com/akaumov/nats-pool

The test action assumes you have the following running:

  • A nats server listening on port 4222

This code is rewriting of "pool" packet from radix for nats.io.

Unless otherwise noted, the source files are distributed under the MIT License found in the LICENSE.txt file.

Documentation

Overview

Package implements a connection pool for nats.io which is thread-safe.

Basic usage

The basic use-case is to create a pool and then pass that pool amongst multiple go-routines, each of which can use it safely. To retrieve a connection you use Get, and to return the connection to the pool when you're done with it you use Put.

p, err := pool.New("nats://localhost:4222", 10)
if err != nil {
	// handle error
}

// In another go-routine

conn, err := p.Get()
if err != nil {
	// handle error
}

conn.Publish(....)

p.Put(conn)

Shortcuts

If you're doing multiple operations you may find it useful to defer the Put right after retrieving a connection, so that you don't have to always remember to do so

conn, err := p.Get()
if err != nil {
	// handle error
}
defer p.Put(conn)

conn.Publish(....)

Custom connections

Sometimes it's necessary to run some code on each connection in a pool upon its creation, This can be done with NewCustom, like so

df := func(addr string) (*nats.Conn, error) {
	client, err := nats.Connect(addr)
	if err != nil {
		return nil, err
	}
	client.Publish(....)

	return client, nil
}
p, err := pool.NewCustom("nats://localhost:4222", 10, df)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DialFunc

type DialFunc func(url string, options ...nats.Option) (*nats.Conn, error)

DialFunc is a function which can be passed into NewCustom

type Pool

type Pool struct {

	// The network/address that the pool is connecting to. These are going to be
	// whatever was passed into the New function. These should not be
	// changed after the pool is initialized
	Network, Addr string
	// contains filtered or unexported fields
}

Pool is a simple connection pool for nats.io connections. It will create a small pool of initial connections, and if more connections are needed they will be created on demand. If a connection is Put back and the pool is full it will be closed.

func New

func New(addr string, size int) (*Pool, error)

New creates a new NatsPool whose connections are all created using nats.Connect. The size indicates the maximum number of idle connections to have waiting to be used at any given moment. If an error is encountered an empty (but still usable) pool is returned alongside that error

func NewPoolCustom

func NewPoolCustom(addr string, size int, df DialFunc) (*Pool, error)

NewCustom is like New except you can specify a DialFunc which will be used when creating new connections for the pool. The common use-case is to do authentication for new connections.

func (*Pool) Avail

func (p *Pool) Avail() int

Avail returns the number of connections currently available to be gotten from the NatsPool using Get. If the number is zero then subsequent calls to Get will be creating new connections on the fly

func (*Pool) Empty

func (p *Pool) Empty()

Empty removes and calls Close() on all the connections currently in the pool. Assuming there are no other connections waiting to be Put back this method effectively closes and cleans up the pool.

func (*Pool) Get

func (p *Pool) Get() (*nats.Conn, error)

Get retrieves an available nats connections. If there are none available it will create a new one on the fly

func (*Pool) Put

func (p *Pool) Put(conn *nats.Conn)

Put returns a client back to the pool. If the pool is full the client is closed instead. If the client is already closed (due to connection failure or what-have-you) it will not be put back in the pool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL