Skip to content

Commit

Permalink
Merge pull request #12 from jolestar/abandoned-test
Browse files Browse the repository at this point in the history
add abandoned config test
  • Loading branch information
jolestar committed Jan 14, 2016
2 parents 2cfabf8 + cab7663 commit 538dba6
Show file tree
Hide file tree
Showing 6 changed files with 444 additions and 29 deletions.
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ type AbandonedConfig struct {
RemoveAbandonedTimeout int
}

func NewDefaultAbandonedConfig() *AbandonedConfig {
return &AbandonedConfig{RemoveAbandonedOnBorrow: false, RemoveAbandonedOnMaintenance: false, RemoveAbandonedTimeout: 300}
}

type EvictionConfig struct {
IdleEvictTime int64
IdleSoftEvictTime int64
Expand Down
14 changes: 3 additions & 11 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ const (
)

type TrackedUse interface {
/**
Get the last time this object was used in ms.
*/
//Get the last time this object was used in ms.
GetLastUsed() int64
}

Expand Down Expand Up @@ -140,16 +138,10 @@ func (this *PooledObject) invalidate() {
this.state = INVALID
}

func (this *PooledObject) Use() {
this.LastUseTime = currentTimeMillis()
//usedBy = new Exception("The last code to use this object was:");
}

func (this *PooledObject) GetState() PooledObjectState {
this.lock.Lock()
result := this.state
this.lock.Unlock()
return result
defer this.lock.Unlock()
return this.state
}

func (this *PooledObject) MarkAbandoned() {
Expand Down
16 changes: 16 additions & 0 deletions object_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pool

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestPooledObject(t *testing.T) {
object := &TestObject{Num: 1}
pooledObject := NewPooledObject(object)
pooledObject.MarkReturning()
assert.Equal(t, RETURNING, pooledObject.GetState())

pooledObject.MarkAbandoned()
assert.Equal(t, ABANDONED, pooledObject.GetState())
}
53 changes: 35 additions & 18 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,25 @@ type ObjectPool struct {
}

func NewObjectPool(factory PooledObjectFactory, config *ObjectPoolConfig) *ObjectPool {
return NewObjectPoolWithAbandonedConfig(factory, config, nil)
}

func NewObjectPoolWithDefaultConfig(factory PooledObjectFactory) *ObjectPool {
return NewObjectPool(factory, NewDefaultPoolConfig())
}

func NewObjectPoolWithAbandonedConfig(factory PooledObjectFactory, config *ObjectPoolConfig, abandonedConfig *AbandonedConfig) *ObjectPool {
pool := ObjectPool{factory: factory, Config: config,
idleObjects: collections.NewDeque(math.MaxInt32),
allObjects: collections.NewSyncMap(),
createCount: concurrent.AtomicInteger(0),
destroyedByEvictorCount: concurrent.AtomicInteger(0),
destroyedCount: concurrent.AtomicInteger(0)}
destroyedCount: concurrent.AtomicInteger(0),
AbandonedConfig: abandonedConfig}
pool.StartEvictor()
return &pool
}

func NewObjectPoolWithDefaultConfig(factory PooledObjectFactory) *ObjectPool {
return NewObjectPool(factory, NewDefaultPoolConfig())
}

// Create an object using the PooledObjectFactory factory, passivate it, and then place it in
// the idle object pool. AddObject is useful for "pre-loading"
// a pool with idle objects. (Optional operation).
Expand All @@ -75,7 +80,11 @@ func (this *ObjectPool) AddObject() error {
if this.factory == nil {
return NewIllegalStatusErr("Cannot add objects without a factory.")
}
this.addIdleObject(this.create())
p, e := this.create()
if e != nil {
return e
}
this.addIdleObject(p)
return nil
}

Expand Down Expand Up @@ -128,10 +137,10 @@ func (this *ObjectPool) removeAbandoned(config *AbandonedConfig) {
// Generate a list of abandoned objects to remove
now := currentTimeMillis()
timeout := now - int64((config.RemoveAbandonedTimeout * 1000))
var remove []PooledObject
var remove []*PooledObject
objects := this.allObjects.Values()
for _, o := range objects {
pooledObject := o.(PooledObject)
pooledObject := o.(*PooledObject)
pooledObject.lock.Lock()
if pooledObject.state == ALLOCATED &&
pooledObject.GetLastUsedTime() <= timeout {
Expand All @@ -150,28 +159,27 @@ func (this *ObjectPool) removeAbandoned(config *AbandonedConfig) {
}
}

func (this *ObjectPool) create() *PooledObject {
func (this *ObjectPool) create() (*PooledObject, error) {
localMaxTotal := this.Config.MaxTotal
newCreateCount := this.createCount.IncrementAndGet()
if localMaxTotal > -1 && int(newCreateCount) > localMaxTotal ||
newCreateCount >= math.MaxInt32 {
this.createCount.DecrementAndGet()
return nil
return nil, nil
}

p, e := this.factory.MakeObject()
if e != nil {
this.createCount.DecrementAndGet()
//return error ?
return nil
return nil, e
}

// ac := this.abandonedConfig;
// if (ac != null && ac.getLogAbandoned()) {
// p.setLogAbandoned(true);
// }
this.allObjects.Put(p.Object, p)
return p
return p, nil
}

func (this *ObjectPool) destroy(toDestroy *PooledObject) {
Expand Down Expand Up @@ -214,7 +222,7 @@ func (this *ObjectPool) borrowObject(borrowMaxWaitMillis int64) (interface{}, er
}

var p *PooledObject

var e error
// Get local copy of current config so it is consistent for entire
// method execution
blockWhenExhausted := this.Config.BlockWhenExhausted
Expand All @@ -227,7 +235,10 @@ func (this *ObjectPool) borrowObject(borrowMaxWaitMillis int64) (interface{}, er
if blockWhenExhausted {
p, ok = this.idleObjects.PollFirst().(*PooledObject)
if !ok {
p = this.create()
p, e = this.create()
if e != nil {
return nil, e
}
if p != nil {
create = true
ok = true
Expand Down Expand Up @@ -258,7 +269,10 @@ func (this *ObjectPool) borrowObject(borrowMaxWaitMillis int64) (interface{}, er
} else {
p, ok = this.idleObjects.PollFirst().(*PooledObject)
if !ok {
p = this.create()
p, e = this.create()
if e != nil {
return nil, e
}
if p != nil {
create = true
}
Expand Down Expand Up @@ -308,7 +322,8 @@ func (this *ObjectPool) ensureIdle(idleCount int, always bool) {
}

for this.idleObjects.Size() < idleCount {
p := this.create()
//just ignore create error
p, _ := this.create()
if p == nil {
// Can't create objects, no reason to think another call to
// create will work. Give up.
Expand Down Expand Up @@ -621,7 +636,9 @@ func (this *ObjectPool) ensureMinIdle() {
this.ensureIdle(this.getMinIdle(), true)
}

func (this *ObjectPool) preparePool() {
//Tries to ensure that {@link #getMinIdle()} idle instances are available
//in the pool.
func (this *ObjectPool) PreparePool() {
if this.getMinIdle() < 1 {
return
}
Expand Down
Loading

0 comments on commit 538dba6

Please sign in to comment.