// New returns a new Pool structure. func New(setters ...Configurator) (*Pool, error) { pc := &config{ size: DefaultSize, } for _, setter := range setters { if err := setter(pc); err != nil { return nil, err } } if pc.address == "" { return nil, errors.New("cannot create pool with empty address") } pool, err := gopool.New( gopool.Size(pc.size), gopool.Close(func(conn interface{}) error { return conn.(io.ReadWriteCloser).Close() }), gopool.Factory(func() (interface{}, error) { if pc.printer != nil { return goperconn.New(goperconn.Address(pc.address), goperconn.Logger(pc.printer)) } return goperconn.New(goperconn.Address(pc.address)) }), ) if err != nil { return nil, err } return &Pool{pool: pool}, nil }
func TestChanPool(t *testing.T) { pool, err := gopool.New(gopool.Factory(makeBuffer), gopool.Reset(resetBuffer), gopool.Close(closeBuffer)) if err != nil { t.Fatalf("Actual: %#v; Expected: %#v", err, nil) } test(t, pool) }
func TestChanPoolInvokesClose(t *testing.T) { var closeInvoked int pool, err := gopool.New(gopool.Size(1), gopool.Factory(func() (interface{}, error) { return nil, nil }), gopool.Close(func(_ interface{}) error { closeInvoked++ return errors.New("foo") })) if err != nil { t.Fatal(err) } if err := pool.Close(); err == nil || err.Error() != "foo" { t.Errorf("Actual: %#v; Expected: %#v", err, "foo") } if actual, expected := closeInvoked, 1; actual != expected { t.Errorf("Actual: %#v; Expected: %#v", actual, expected) } }
func BenchmarkChanHighConcurrency(b *testing.B) { pool, _ := gopool.New(gopool.Factory(makeBuffer), gopool.Reset(resetBuffer), gopool.Close(closeBuffer), gopool.Size(10000)) bench(b, pool, highConcurrency) }
func BenchmarkArrayMediumConcurrency(b *testing.B) { pool, _ := gopool.NewArrayPool(gopool.Factory(makeBuffer), gopool.Reset(resetBuffer), gopool.Close(closeBuffer), gopool.Size(1000)) bench(b, pool, medConcurrency) }