Code Monkey home page Code Monkey logo

go-commons-pool's Introduction

Go Commons Pool

Build Status Circle CI Coverage Status GoDoc

The Go Commons Pool is a generic object pool for Golang, direct rewrite from Apache Commons Pool.

Features

  1. Support custom PooledObjectFactory.
  2. Rich pool configuration option, can precise control pooled object lifecycle. see ObjectPoolConfig.
    • Pool LIFO (last in, first out) or FIFO (first in, first out)
    • Pool cap config
    • Pool object validate config
    • Pool object borrow block and max waiting time config
    • Pool object eviction config
    • Pool object abandon config

Pool Configuration Option

Configuration option table, more detail description see ObjectPoolConfig

Option Default Description
Lifo true If pool is LIFO (last in, first out)
MaxTotal 8 The cap of pool
MaxIdle 8 Max "idle" instances in the pool
MinIdle 0 Min "idle" instances in the pool
TestOnCreate false Validate when object is created
TestOnBorrow false Validate when object is borrowed
TestOnReturn false Validate when object is returned
TestWhileIdle false Validate when object is idle, see TimeBetweenEvictionRunsMillis
BlockWhenExhausted true Whether to block when the pool is exhausted
MaxWaitMillis -1 Max block time, less than 0 mean indefinitely
MinEvictableIdleTimeMillis 1000 * 60 * 30 Eviction configuration,see DefaultEvictionPolicy
SoftMinEvictableIdleTimeMillis math.MaxInt64 Eviction configuration,see DefaultEvictionPolicy
NumTestsPerEvictionRun 3 The maximum number of objects to examine during each run evictor goroutine
TimeBetweenEvictionRunsMillis -1 The number of milliseconds to sleep between runs of the evictor goroutine, less than 0 mean not run

Usage

import "github.com/jolestar/go-commons-pool"

//use create func
p := pool.NewObjectPoolWithDefaultConfig(pool.NewPooledObjectFactorySimple(
		func() (interface{}, error) {
			return &MyPoolObject{}, nil
		}))
obj, _ := p.BorrowObject()
p.ReturnObject(obj)
	
//use custom Object factory

type MyObjectFactory struct {
}

func (f *MyObjectFactory) MakeObject() (*pool.PooledObject, error) {
	return pool.NewPooledObject(&MyPoolObject{}), nil
}

func (f *MyObjectFactory) DestroyObject(object *PooledObject) error {
	//do destroy
	return nil
}

func (f *MyObjectFactory) ValidateObject(object *pool.PooledObject) bool {
	//do validate
	return true
}

func (f *MyObjectFactory) ActivateObject(object *pool.PooledObject) error {
	//do activate
	return nil
}

func (f *MyObjectFactory) PassivateObject(object *pool.PooledObject) error {
	//do passivate
	return nil
}

p := pool.NewObjectPoolWithDefaultConfig(new(MyObjectFactory))
p.Config.MaxTotal = 100
obj, _ := p.BorrowObject()
p.ReturnObject(obj)

more example please see pool_test.go and example_test.go

Note

PooledObjectFactory.MakeObject must return a pointer, not value. The following code will complain error.

p := pool.NewObjectPoolWithDefaultConfig(pool.NewPooledObjectFactorySimple(
	func() (interface{}, error) {
		return "hello", nil
	}))
obj, _ := p.BorrowObject()
p.ReturnObject(obj)

The right way is:

p := pool.NewObjectPoolWithDefaultConfig(pool.NewPooledObjectFactorySimple(
	func() (interface{}, error) {
		var stringPointer = new(string)
		*stringPointer = "hello"
		return stringPointer, nil
	}))

more example please see example_test.go

Dependency

PerformanceTest

The results of running the pool_perf_test is almost equal to the java version PerformanceTest

go test --perf=true

For Apache commons pool user

  • Direct use pool.Config.xxx to change pool config
  • Default config value is same as java version
  • If TimeBetweenEvictionRunsMillis changed after ObjectPool created, should call ObjectPool.StartEvictor to take effect. Java version do this on set method.
  • No KeyedObjectPool (TODO)
  • No ProxiedObjectPool
  • No pool stats (TODO)

FAQ

FAQ

How to contribute

  • Choose one open issue you want to solve, if not create one and describe what you want to change.
  • Fork the repository on GitHub.
  • Write code to solve the issue.
  • Create PR and link to the issue.
  • Make sure test and coverage pass.
  • Wait maintainers to merge.

中文文档

License

Go Commons Pool is available under the Apache License, Version 2.0.

go-commons-pool's People

Contributors

jolestar avatar ls0f avatar

Watchers

 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.