pool

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

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

Go to latest
Published: Sep 10, 2018 License: MIT Imports: 9 Imported by: 0

README

Pool GoDoc Build Status

实现了一个thrift client连接池

安装

go get github.com/michaelyou/thrift_pool

Example

import (
    "github.com/michaelyou/thrift_pool"
    "thrift_practise/gen-go/tutorial"
)

// 创建一个函数用来生成需要的thrift client
func NewClient(ctx context.Context, c *thrift.TStandardClient) interface{} {
    return tutorial.NewCalculatorClient(c)
}

ctx := context.Background()
cp, err := pool.NewConnectionPool(ctx, "127.0.0.1:9090", "binary", false, false, NewClient)
// 意义同database/sql
cp.SetMaxOpenConns(10)
cp.SetMaxIdleConns(5)
// cp.SetConnMaxLifetime(60 * time.Second) 可以设置连接的过期时间,过期的连接将被回收,生成新的连接,如果不设置,将不对连接的过期进行检查
if err != nil {
	fmt.Println("new connection pool error", err)
	return err
}

// 取出一个连接
conn, err := cp.Get(ctx, pool.CachedOrNewConn)
if err != nil {
	fmt.Println("get conn error", err)
}
// 从conn拿出client,client是一个interface{},需要type assertions
client := conn.ThriftClient
handleClient(client.(*tutorial.CalculatorClient))
// 千万不要忘记将连接重新放回池中
conn.Release(nil)

License

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

Documentation

Index

Constants

View Source
const (
	AlwaysNewConn connReuseStrategy = iota
	CachedOrNewConn
)

Variables

View Source
var ErrBadConn = errors.New("bad connection")

Functions

func NewConnectionPool

func NewConnectionPool(ctx context.Context, addrs, protocol string, frame, buffered bool, factory Factory) (*connectionPool, error)

@addrs: server地址,逗号分隔,eg: 127.0.0.1:2000,127.0.0.2:3000 @protocol: Specify the protocol (binary, compact, json, simplejson) @frame: Use framed transport @buffered: Use buffered transport

Types

type Connection

type Connection struct {
	sync.Mutex

	ThriftClient interface{} // this is what we return in Get
	// contains filtered or unexported fields
}

func (*Connection) Close

func (c *Connection) Close()

func (*Connection) IsExpired

func (c *Connection) IsExpired(timeout time.Duration) bool

func (*Connection) Release

func (c *Connection) Release(err error)

type Factory

type Factory func(ctx context.Context, c *thrift.TStandardClient) interface{}

Factory is a function to create new connections.

Jump to

Keyboard shortcuts

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