pool

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: MIT Imports: 12 Imported by: 0

README

Pool

GoDoc Go Report Card Build Status Travis Build Status Semaphore Sourcegraph Open Source Helpers LICENSE GitHub code size in bytes Release LICENSE

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 for loadbalance.

Install

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

Usage

//import "gopkg.in/flyaways/pool.v1"
import "github.com/GitHub121380/pool"

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())//for grpc
	//p, err := pool.NewRPCPool(options) 			//for rpc
	//p, err := pool.NewTCPPool(options)			//for tcp

	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

Contribution Welcomed !

Contributors

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,
	timeoutType:  IdleTimeoutType,
	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() (*GrpcIdleConn, 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 *GrpcIdleConn) error

Put put back to pool

type GrpcIdleConn

type GrpcIdleConn struct {
	Conn *grpc.ClientConn
	// contains filtered or unexported fields
}

type Options

type Options struct {
	InitTargets  []string      //InitTargets init targets
	InitCap      int           // init connection
	MaxCap       int           // max connections
	TimeoutType  TimeoutType   //timeout type, fixed or idle
	DialTimeout  time.Duration //dial timeout
	IdleTimeout  time.Duration //timeout in program
	ReadTimeout  time.Duration //unused
	WriteTimeout time.Duration //unused
	// 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

type TimeoutType

type TimeoutType int
const (
	IdleTimeoutType  TimeoutType = iota + 1 //idled during timeout
	FixedTimeoutType                        //alive during timeout, like life cycle
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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