pool

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2019 License: MIT Imports: 12 Imported by: 4

README

Pool

GoDoc Go Report Card Build Status Travis Build Status Semaphore Sourcegraph Open Source Helpers LICENSE Release

Pool is Used to manage and reuse client connections to service cluster.

Pool provides several key features:

  • General Purpose - Pool for GRPC,RPC,TCP.support RPC timeout.

  • Support Cluster - Connet to Cluster.

  • Danamic Update - Danamic update targets.

Pool runs on Linux, Mac OS X, and Windows.

Note: Random to pick a target to get one connection,just now.

Install

go get -u gopkg.in/flyaways/pool.v1

Usage

import "gopkg.in/flyaways/pool.v1"

Example

package main

import (
	"log"
	"time"

	"gopkg.in/flyaways/pool.v1"
	"google.golang.org/grpc"
)

func main() {
	options := &pool.Options{
		InitTargets:  []string{"127.0.0.1:8080"},
		InitCap:      5,
		MaxCap:       30,
		DialTimeout:  time.Second * 5,
		IdleTimeout:  time.Second * 60,
		ReadTimeout:  time.Second * 5,
		WriteTimeout: time.Second * 5,
	}

	p, err := pool.NewGRPCPool(options, grpc.WithInsecure())

	if err != nil {
		log.Printf("%#v\n", err)
		return
	}

	if p == nil {
		log.Printf("p= %#v\n", p)
		return
	}

	defer p.Close()

	//todo
	//danamic update targets
	//options.Input()<-&[]string{}

	conn, err := p.Get()
	if err != nil {
		log.Printf("%#v\n", err)
		return
	}

	defer p.Put(conn)

	//todo
	//conn.DoSomething()

	log.Printf("len=%d\n", p.IdleCount())
}

Reference

License

  • The MIT License (MIT) - see LICENSE for more details

FOSSA Status

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec struct {
	Timeout time.Duration
	Closer  io.ReadWriteCloser
	Decoder *gob.Decoder
	Encoder *gob.Encoder
	EncBuf  *bufio.Writer
}

Codec ...

func (*Codec) Close

func (c *Codec) Close() error

Close ...

func (*Codec) ReadResponseBody

func (c *Codec) ReadResponseBody(body interface{}) error

ReadResponseBody ...

func (*Codec) ReadResponseHeader

func (c *Codec) ReadResponseHeader(r *rpc.Response) error

ReadResponseHeader ...

func (*Codec) WriteRequest

func (c *Codec) WriteRequest(r *rpc.Request, body interface{}) (err error)

WriteRequest ...

type GRPCPool

type GRPCPool struct {
	Mu          sync.Mutex
	IdleTimeout time.Duration
	// contains filtered or unexported fields
}

GRPCPool pool info

Example
options := &Options{
	InitTargets:  []string{"127.0.0.1:8080"},
	InitCap:      5,
	MaxCap:       30,
	DialTimeout:  time.Second * 5,
	IdleTimeout:  time.Second * 60,
	ReadTimeout:  time.Second * 5,
	WriteTimeout: time.Second * 5,
}

p, err := NewGRPCPool(options, grpc.WithInsecure())

if err != nil {
	log.Printf("%#v\n", err)
	return
}

if p == nil {
	log.Printf("p= %#v\n", p)
	return
}

defer p.Close()

//todo
//danamic update targets
//options.Input()<-&[]string{}

conn, err := p.Get()
if err != nil {
	log.Printf("%#v\n", err)
	return
}

defer p.Put(conn)

//todo
//conn.DoSomething()

log.Printf("len=%d\n", p.IdleCount())
Output:

func NewGRPCPool

func NewGRPCPool(o *Options, dialOptions ...grpc.DialOption) (*GRPCPool, error)

NewGRPCPool init grpc pool

func (*GRPCPool) Close

func (c *GRPCPool) Close()

Close close pool

func (*GRPCPool) Get

func (c *GRPCPool) Get() (*grpc.ClientConn, error)

Get get from pool

func (*GRPCPool) IdleCount

func (c *GRPCPool) IdleCount() int

IdleCount idle connection count

func (*GRPCPool) Put

func (c *GRPCPool) Put(conn *grpc.ClientConn) error

Put put back to pool

type Options

type Options struct {

	//InitTargets init targets
	InitTargets []string
	// init connection
	InitCap int
	// max connections
	MaxCap       int
	DialTimeout  time.Duration
	IdleTimeout  time.Duration
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	// contains filtered or unexported fields
}

Options pool options

func NewOptions

func NewOptions() *Options

NewOptions returns a new newOptions instance with sane defaults.

func (*Options) Input

func (o *Options) Input() chan<- *[]string

Input is the input channel

type RPCPool

type RPCPool struct {
	Mu          sync.Mutex
	IdleTimeout time.Duration
	// contains filtered or unexported fields
}

RPCPool pool info

Example
options := &Options{
	InitTargets:  []string{"127.0.0.1:8080"},
	InitCap:      5,
	MaxCap:       30,
	DialTimeout:  time.Second * 5,
	IdleTimeout:  time.Second * 60,
	ReadTimeout:  time.Second * 5,
	WriteTimeout: time.Second * 5,
}

p, err := NewRPCPool(options)

if err != nil {
	log.Printf("%#v\n", err)
	return
}

if p == nil {
	log.Printf("p= %#v\n", p)
	return
}

defer p.Close()

//todo
//danamic update targets
//options.Input()<-&[]string{}

conn, err := p.Get()
if err != nil {
	log.Printf("%#v\n", err)
	return
}

defer p.Put(conn)

//todo
//conn.DoSomething()

log.Printf("len=%d\n", p.IdleCount())
Output:

func NewRPCPool

func NewRPCPool(o *Options) (*RPCPool, error)

NewRPCPool init rpc pool

func (*RPCPool) Close

func (c *RPCPool) Close()

Close close all connection

func (*RPCPool) Get

func (c *RPCPool) Get() (*rpc.Client, error)

Get get from pool

func (*RPCPool) IdleCount

func (c *RPCPool) IdleCount() int

IdleCount idle connection count

func (*RPCPool) Put

func (c *RPCPool) Put(conn *rpc.Client) error

Put put back to pool

type TCPPool

type TCPPool struct {
	Mu          sync.Mutex
	IdleTimeout time.Duration
	// contains filtered or unexported fields
}

TCPPool pool info

Example
options := &Options{
	InitTargets:  []string{"127.0.0.1:8080"},
	InitCap:      5,
	MaxCap:       30,
	DialTimeout:  time.Second * 5,
	IdleTimeout:  time.Second * 60,
	ReadTimeout:  time.Second * 5,
	WriteTimeout: time.Second * 5,
}

p, err := NewTCPPool(options)

if err != nil {
	log.Printf("%#v\n", err)
	return
}

if p == nil {
	log.Printf("p= %#v\n", p)
	return
}

defer p.Close()

//todo
//danamic update targets
//options.Input()<-&[]string{}

conn, err := p.Get()
if err != nil {
	log.Printf("%#v\n", err)
	return
}

defer p.Put(conn)

//todo
//conn.DoSomething()

log.Printf("len=%d\n", p.IdleCount())
Output:

func NewTCPPool

func NewTCPPool(o *Options) (*TCPPool, error)

NewTCPPool init tcp pool

func (*TCPPool) Close

func (c *TCPPool) Close()

Close close all connection

func (*TCPPool) Get

func (c *TCPPool) Get() (net.Conn, error)

Get get from pool

func (*TCPPool) IdleCount

func (c *TCPPool) IdleCount() int

IdleCount idle connection count

func (*TCPPool) Put

func (c *TCPPool) Put(conn net.Conn) error

Put put back to pool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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