From e30c7b7100654ab508edf854b7c1f9bfbae62134 Mon Sep 17 00:00:00 2001 From: Richard Park Date: Tue, 19 Nov 2019 22:54:01 +0900 Subject: [PATCH] pool --- go.sum | 1 + pkg/loafer/app/app.go | 28 ++++++++++++++++++++++------ pkg/modules/user/user-dao.go | 18 +++++++++++++----- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/go.sum b/go.sum index 6db8dca..6f16ffc 100644 --- a/go.sum +++ b/go.sum @@ -51,6 +51,7 @@ github.com/jackc/pgx/v4 v4.1.2 h1:xZwqiD9cP6zF7oJ1NO2j9txtjpA7I+MdfP3h/TAT1Q8= github.com/jackc/pgx/v4 v4.1.2/go.mod h1:0cQ5ee0A6fEsg29vZekucSFk5OcWy8sT4qkhuPXHuIE= github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jackc/puddle v1.0.0 h1:rbjAshlgKscNa7j0jAM0uNQflis5o2XUogPMVAwtcsM= github.com/jackc/puddle v1.0.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/klauspost/compress v1.8.2 h1:Bx0qjetmNjdFXASH02NSAREKpiaDwkO1DRZ3dV2KCcs= github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= diff --git a/pkg/loafer/app/app.go b/pkg/loafer/app/app.go index 7025938..a80d13a 100644 --- a/pkg/loafer/app/app.go +++ b/pkg/loafer/app/app.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "net/url" "reflect" "git.loafle.net/loafer/di-go" @@ -12,7 +13,7 @@ import ( "git.loafle.net/totopia/server/pkg/loafer/web/router" "github.com/valyala/fasthttp" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v4/pgxpool" ) type MethodMapping struct { @@ -21,15 +22,30 @@ type MethodMapping struct { } func Run(t reflect.Type) error { - conn, err := pgx.Connect(context.Background(), fmt.Sprintf("host=%s port=%d user=%s "+ - "password=%s dbname=%s sslmode=disable", - "localhost", 54320, "totopia", "totopia", "totopia")) + pgURL := &url.URL{ + Scheme: "postgres", + User: url.UserPassword("totopia", "totopia"), + Path: "totopia", + } + q := pgURL.Query() + q.Add("sslmode", "disable") + pgURL.RawQuery = q.Encode() + + config, err := pgxpool.ParseConfig(pgURL.String()) if err != nil { return fmt.Errorf("[%v]", err) } - defer conn.Close(context.Background()) + config.ConnConfig.Host = "localhost" + config.ConnConfig.Port = 54320 - di.RegisterResource("dbConn", conn) + pool, err := pgxpool.ConnectConfig(context.Background(), config) + if err != nil { + return fmt.Errorf("[%v]", err) + } + + defer pool.Close() + + di.RegisterResource("dbConnPool", pool) taServer := di.GetTypeAnnotation(t, appAnnotation.ServerAnnotationType) if nil == taServer { diff --git a/pkg/modules/user/user-dao.go b/pkg/modules/user/user-dao.go index ef075a5..86801c8 100644 --- a/pkg/modules/user/user-dao.go +++ b/pkg/modules/user/user-dao.go @@ -8,7 +8,7 @@ import ( "git.loafle.net/loafer/annotation-go" "git.loafle.net/loafer/di-go" - "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v4/pgxpool" ) var UserDaoType = reflect.TypeOf((*UserDao)(nil)) @@ -20,20 +20,28 @@ func init() { type UserDao struct { annotation.TypeAnnotation `annotation:"@web:Dao(\"name\": \"/userDao\")"` - Conn *pgx.Conn `annotation:"@Resource(\"name\": \"dbConn\")"` + Pool *pgxpool.Pool `annotation:"@Resource(\"name\": \"dbConnPool\")"` } func (ud *UserDao) FindAll() { log.Printf("UserDao FindAll") - row, err := ud.Conn.Query(context.Background(), "SELECT NOW()") + + conn, err := ud.Pool.Acquire(context.Background()) + if nil != err { + log.Printf("%v", err) + return + } + defer conn.Release() + + row, err := conn.Query(context.Background(), "select count(*) from users") if nil != err { log.Printf("%v", err) return } if row.Next() { - var date string + var date int64 row.Scan(&date) - log.Printf("%s", date) + log.Printf("%d", date) } }