Giter VIP home page Giter VIP logo

thrift_pool's Introduction

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

thrift_pool's People

Contributors

michaelyou avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.