Ejemplo n.º 1
0
func newConnPool(opt *Options) *pool.ConnPool {
	p := pool.NewConnPool(
		opt.getDialer(), opt.getPoolSize(), opt.getPoolTimeout(), opt.getIdleTimeout())
	p.OnClose = func(cn *pool.Conn) error {
		return terminateConn(cn)
	}
	return p
}
Ejemplo n.º 2
0
func benchmarkPoolGetPut(b *testing.B, poolSize int) {
	dial := func() (net.Conn, error) {
		return &net.TCPConn{}, nil
	}
	pool := pool.NewConnPool(dial, poolSize, time.Second, 0)
	pool.DialLimiter = nil

	b.ResetTimer()

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			conn, err := pool.Get()
			if err != nil {
				b.Fatalf("no error expected on pool.Get but received: %s", err.Error())
			}
			if err = pool.Put(conn); err != nil {
				b.Fatalf("no error expected on pool.Put but received: %s", err.Error())
			}
		}
	})
}
Ejemplo n.º 3
0
	"github.com/housinganywhere/pg/internal/pool"
)

func TestGinkgoSuite(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "pool")
}

var _ = Describe("conns reaper", func() {
	var connPool *pool.ConnPool

	BeforeEach(func() {
		dial := func() (net.Conn, error) {
			return &net.TCPConn{}, nil
		}
		connPool = pool.NewConnPool(dial, 10, 0, time.Minute)

		// add stale connections
		for i := 0; i < 3; i++ {
			cn := pool.NewConn(&net.TCPConn{})
			cn.UsedAt = time.Now().Add(-2 * time.Minute)
			Expect(connPool.Add(cn)).To(BeTrue())
			Expect(cn.Index()).To(Equal(i))
		}

		// add fresh connections
		for i := 0; i < 3; i++ {
			cn := pool.NewConn(&net.TCPConn{})
			Expect(connPool.Add(cn)).To(BeTrue())
			Expect(cn.Index()).To(Equal(3 + i))
		}