本文整理匯總了Golang中github.com/flynn/flynn/Godeps/_workspace/src/github.com/jackc/pgx.NewConnPool函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewConnPool函數的具體用法?Golang NewConnPool怎麽用?Golang NewConnPool使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewConnPool函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestNewConnPool
func TestNewConnPool(t *testing.T) {
t.Parallel()
var numCallbacks int
afterConnect := func(c *pgx.Conn) error {
numCallbacks++
return nil
}
config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: 2, AfterConnect: afterConnect}
pool, err := pgx.NewConnPool(config)
if err != nil {
t.Fatal("Unable to establish connection pool")
}
defer pool.Close()
// It initially connects once
stat := pool.Stat()
if stat.CurrentConnections != 1 {
t.Errorf("Expected 1 connection to be established immediately, but %v were", numCallbacks)
}
// Pool creation returns an error if any AfterConnect callback does
errAfterConnect := errors.New("Some error")
afterConnect = func(c *pgx.Conn) error {
return errAfterConnect
}
config = pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: 2, AfterConnect: afterConnect}
pool, err = pgx.NewConnPool(config)
if err != errAfterConnect {
t.Errorf("Expected errAfterConnect but received unexpected: %v", err)
}
}
示例2: SetUpSuite
func (s *S) SetUpSuite(c *C) {
dbname := "controllertest"
if err := pgtestutils.SetupPostgres(dbname); err != nil {
c.Fatal(err)
}
pgxpool, err := pgx.NewConnPool(pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: os.Getenv("PGHOST"),
Database: dbname,
},
})
if err != nil {
c.Fatal(err)
}
db := postgres.New(pgxpool, nil)
if err = migrateDB(db); err != nil {
c.Fatal(err)
}
// reconnect with que statements prepared now that schema is migrated
pgxpool, err = pgx.NewConnPool(pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: "/var/run/postgresql",
Database: dbname,
},
AfterConnect: schema.PrepareStatements,
})
if err != nil {
c.Fatal(err)
}
db = postgres.New(pgxpool, nil)
s.flac = newFakeLogAggregatorClient()
s.cc = tu.NewFakeCluster()
s.hc = handlerConfig{
db: db,
cc: s.cc,
lc: s.flac,
rc: newFakeRouter(),
keys: []string{authKey},
}
handler := appHandler(s.hc)
s.srv = httptest.NewServer(handler)
client, err := controller.NewClient(s.srv.URL, authKey)
c.Assert(err, IsNil)
s.c = client
}
示例3: TestOpenFromConnPool
func TestOpenFromConnPool(t *testing.T) {
connConfig := pgx.ConnConfig{
Host: "localhost",
User: "pgx_md5",
Password: "secret",
Database: "pgx_test",
}
config := pgx.ConnPoolConfig{ConnConfig: connConfig}
pool, err := pgx.NewConnPool(config)
if err != nil {
t.Fatalf("Unable to create connection pool: %v", err)
}
defer pool.Close()
db, err := stdlib.OpenFromConnPool(pool)
if err != nil {
t.Fatalf("Unable to create connection pool: %v", err)
}
defer closeDB(t, db)
// Can get pgx.ConnPool from driver
driver := db.Driver().(*stdlib.Driver)
if driver.Pool == nil {
t.Fatal("Expected driver opened through OpenFromConnPool to have Pool, but it did not")
}
// Normal sql/database still works
var n int64
err = db.QueryRow("select 1").Scan(&n)
if err != nil {
t.Fatalf("db.QueryRow unexpectedly failed: %v", err)
}
}
示例4: SetUpSuite
func (s *S) SetUpSuite(c *C) {
s.discoverd, s.cleanup = setup(c)
dbname := "routertest"
if err := pgtestutils.SetupPostgres(dbname); err != nil {
c.Fatal(err)
}
dsn := fmt.Sprintf("dbname=%s", dbname)
db, err := sql.Open("postgres", dsn)
if err != nil {
c.Fatal(err)
}
if err = migrateDB(db); err != nil {
c.Fatal(err)
}
db.Close()
pgxpool, err := pgx.NewConnPool(pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: os.Getenv("PGHOST"),
Database: dbname,
},
})
if err != nil {
c.Fatal(err)
}
s.pgx = pgxpool
s.pgx.Exec(sqlCreateTruncateTables)
}
示例5: TestPostgresFilesystem
func TestPostgresFilesystem(t *testing.T) {
dbname := "blobstoretest"
if err := pgtestutils.SetupPostgres(dbname); err != nil {
t.Fatal(err)
}
pgxpool, err := pgx.NewConnPool(pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: os.Getenv("PGHOST"),
Database: dbname,
},
})
if err != nil {
t.Fatal(err)
}
db := postgres.New(pgxpool, nil)
defer db.Close()
fs, err := NewPostgresFilesystem(db)
if err != nil {
t.Fatal(err)
}
testList(fs, t)
testDelete(fs, t)
testOffset(fs, t)
testFilesystem(fs, true, t)
}
示例6: main
func main() {
var err error
connPoolConfig := pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: "127.0.0.1",
User: "jack",
Password: "jack",
Database: "url_shortener",
Logger: log.New("module", "pgx"),
},
MaxConnections: 5,
AfterConnect: afterConnect,
}
pool, err = pgx.NewConnPool(connPoolConfig)
if err != nil {
log.Crit("Unable to create connection pool", "error", err)
os.Exit(1)
}
http.HandleFunc("/", urlHandler)
log.Info("Starting URL shortener on localhost:8080")
err = http.ListenAndServe("localhost:8080", nil)
if err != nil {
log.Crit("Unable to start web server", "error", err)
os.Exit(1)
}
}
示例7: createConnPool
func createConnPool(t *testing.T, maxConnections int) *pgx.ConnPool {
config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: maxConnections}
pool, err := pgx.NewConnPool(config)
if err != nil {
t.Fatalf("Unable to create connection pool: %v", err)
}
return pool
}
示例8: openTestClientMaxConns
func openTestClientMaxConns(t testing.TB, maxConnections int) *Client {
connPoolConfig := pgx.ConnPoolConfig{
ConnConfig: testConnConfig,
MaxConnections: maxConnections,
AfterConnect: PrepareStatements,
}
pool, err := pgx.NewConnPool(connPoolConfig)
if err != nil {
t.Fatal(err)
}
return NewClient(pool)
}
示例9: TestNewConnPoolMaxConnectionsCannotBeLessThan2
func TestNewConnPoolMaxConnectionsCannotBeLessThan2(t *testing.T) {
t.Parallel()
config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: 1}
pool, err := pgx.NewConnPool(config)
if err == nil {
pool.Close()
t.Fatal(`Expected NewConnPool to fail with "MaxConnections must be at least 2" error, but it succeeded`)
}
if err.Error() != "MaxConnections must be at least 2" {
t.Fatalf(`Expected NewConnPool to fail with "MaxConnections must be at least 2" error, but it failed with %v`, err)
}
}
示例10: TestNewConnPoolDefaultsTo5MaxConnections
func TestNewConnPoolDefaultsTo5MaxConnections(t *testing.T) {
t.Parallel()
config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig}
pool, err := pgx.NewConnPool(config)
if err != nil {
t.Fatal("Unable to establish connection pool")
}
defer pool.Close()
if n := pool.Stat().MaxConnections; n != 5 {
t.Fatalf("Expected pool to default to 5 max connections, but it was %d", n)
}
}
示例11: setupTestDB
func setupTestDB(c *C, dbname string) *postgres.DB {
if err := pgtestutils.SetupPostgres(dbname); err != nil {
c.Fatal(err)
}
pgxpool, err := pgx.NewConnPool(pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: os.Getenv("PGHOST"),
Database: dbname,
},
})
if err != nil {
c.Fatal(err)
}
return postgres.New(pgxpool, nil)
}
示例12: Open
func Open(conf *Conf, afterConn func(*pgx.Conn) error) (*DB, error) {
connConfig := pgx.ConnConfig{
Host: fmt.Sprintf("leader.%s.discoverd", conf.Service),
User: conf.User,
Database: conf.Database,
Password: conf.Password,
Dial: dialer.Retry.Dial,
}
connPool, err := pgx.NewConnPool(pgx.ConnPoolConfig{
ConnConfig: connConfig,
AfterConnect: afterConn,
MaxConnections: 20,
})
db := &DB{connPool, conf}
return db, err
}
示例13: BenchmarkConnPool
func BenchmarkConnPool(b *testing.B) {
config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: 5}
pool, err := pgx.NewConnPool(config)
if err != nil {
b.Fatalf("Unable to create connection pool: %v", err)
}
defer pool.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var conn *pgx.Conn
if conn, err = pool.Acquire(); err != nil {
b.Fatalf("Unable to acquire connection: %v", err)
}
pool.Release(conn)
}
}
示例14: main
func main() {
log := logger.New("fn", "main")
log.Info("creating controller client")
client, err := controller.NewClient("", os.Getenv("AUTH_KEY"))
if err != nil {
log.Error("error creating controller client", "err", err)
shutdown.Fatal()
}
log.Info("connecting to postgres")
db := postgres.Wait("", "")
log.Info("creating postgres connection pool")
pgxpool, err := pgx.NewConnPool(pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: os.Getenv("PGHOST"),
User: os.Getenv("PGUSER"),
Password: os.Getenv("PGPASSWORD"),
Database: os.Getenv("PGDATABASE"),
},
AfterConnect: que.PrepareStatements,
MaxConnections: workerCount,
})
if err != nil {
log.Error("error creating postgres connection pool", "err", err)
shutdown.Fatal()
}
shutdown.BeforeExit(func() { pgxpool.Close() })
workers := que.NewWorkerPool(
que.NewClient(pgxpool),
que.WorkMap{
"deployment": deployment.JobHandler(db, client, logger),
"app_deletion": app_deletion.JobHandler(db, client, logger),
},
workerCount,
)
workers.Interval = 5 * time.Second
log.Info("starting workers", "count", workerCount, "interval", workers.Interval)
workers.Start()
shutdown.BeforeExit(func() { workers.Shutdown() })
select {} // block and keep running
}
示例15: SetUpSuite
func (s *S) SetUpSuite(c *C) {
s.discoverd, s.cleanup = setup(c)
if err := pgtestutils.SetupPostgres(dbname); err != nil {
c.Fatal(err)
}
pgxpool, err := pgx.NewConnPool(newPgxConnPoolConfig())
if err != nil {
c.Fatal(err)
}
db := postgres.New(pgxpool, nil)
if err = migrateDB(db); err != nil {
c.Fatal(err)
}
s.pgx = db.ConnPool
s.pgx.Exec(sqlCreateTruncateTables)
}