Giter Club home page Giter Club logo

pool-1's Introduction

pool

GoDoc

Golang 实现的连接池

功能:

  • 连接池中连接类型为interface{},使得更加通用
  • 链接的最大空闲时间,超时的链接将关闭丢弃,可避免空闲时链接自动失效问题
  • 使用channel处理池中的链接,高效

基本用法

//factory 创建连接的方法
factory := func() (interface{}, error) { return net.Dial("tcp", "127.0.0.1:4000") }

//close 关闭链接的方法
close := func(v interface{}) error { return v.(net.Conn).Close() }

//创建一个连接池: 初始化5,最大链接30
poolConfig := &pool.PoolConfig{
	InitialCap: 5,
	MaxCap:     30,
	Factory:    factory,
	Close:      close,
	//链接最大空闲时间,超过该时间的链接 将会关闭,可避免空闲时链接EOF,自动失效的问题
	IdleTimeout: 15 * time.Second,
}
p, err := pool.NewChannelPool(poolConfig)
if err != nil {
	fmt.Println("err=", err)
}

//从连接池中取得一个链接
v, err := p.Get()

//do something
//conn=v.(net.Conn)

//将链接放回连接池中
p.Put(v)

//释放连接池中的所有链接
p.Release()

//查看当前链接中的数量
current := p.Len()

注:

该连接池参考 https://github.com/fatih/pool 实现,改变以及增加原有的一些功能。

License

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

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.