Giter VIP home page Giter VIP logo

generic-pool's Introduction

Generic Pool

Build Status

golang通用连接池,管理所有实现了Closer接口的资源。

Installation

$ go get github.com/goctx/generic-pool

(optional) To run unit tests:

$ cd $GOPATH/src/github.com/goctx/generic-pool
$ go test -v

ChangeLog

  • 添加超时处理机制,需要实现GetActiveTime方法返回最新活跃时间

Get Stated

type DemoCloser struct {
	Name     string
	activeAt time.Time
}

func (p *DemoCloser) Close() error {
	fmt.Println(p.Name, "closed")
	return nil
}

func (p *DemoCloser) GetActiveTime() time.Time {
	return p.activeAt
}

// 创建连接池
pool, err := NewGenericPool(0, 10, time.Minute*10, func() (io.Closer, error) {
	    // 模拟连接延迟
		time.Sleep(time.Second)
		return &DemoCloser{Name: "test"}, nil
})

// 从连接池获取连接
if s, err := pool.Acquire();err != nil {
	// 出错了
} else {
    // it work
    // 回收连接
    pool.Release(s)
}
// 不需要的时候关闭连接池
pool.Shutdown()

generic-pool's People

Contributors

xialeistudio avatar

Watchers

James Cloos avatar Legendary avatar  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.