From: qydysky Date: Mon, 22 Apr 2024 17:57:42 +0000 (+0000) Subject: 1 X-Git-Tag: v0.28.20240422182232~1 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=72cc9f751ede15a81cb2d83bed2f44fa6d7eeac6;p=part%2F.git 1 --- diff --git a/ctx/Ctx.go b/ctx/Ctx.go index db0174e..c47703f 100644 --- a/ctx/Ctx.go +++ b/ctx/Ctx.go @@ -151,3 +151,11 @@ func CallCancel(ctx context.Context) error { } return nil } + +func GenTOCtx(t time.Duration) context.Context { + return CarryCancel(context.WithTimeout(context.Background(), t)) +} + +func GenDLCtx(t time.Time) context.Context { + return CarryCancel(context.WithDeadline(context.Background(), t)) +} diff --git a/go.mod b/go.mod index 416a8cd..4155288 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,12 @@ require ( require ( github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/ncruces/go-strftime v0.1.9 // indirect + golang.org/x/crypto v0.21.0 // indirect + golang.org/x/sync v0.6.0 // indirect modernc.org/gc/v3 v3.0.0-20240304020402-f0dba7c97c2b // indirect ) @@ -38,7 +43,7 @@ require ( require ( github.com/dustin/go-humanize v1.0.1 github.com/go-ole/go-ole v1.3.0 // indirect - github.com/lib/pq v1.10.9 + github.com/jackc/pgx/v5 v5.5.5 github.com/stretchr/testify v1.9.0 // indirect golang.org/x/net v0.22.0 // indirect golang.org/x/sys v0.18.0 // indirect diff --git a/go.sum b/go.sum index 537021b..a654eb9 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,6 @@ github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= @@ -15,10 +16,16 @@ github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/ github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw= +github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg= github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= -github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= -github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= @@ -35,6 +42,9 @@ github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKl github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA= github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/thedevsaddam/gojsonq/v2 v2.5.2 h1:CoMVaYyKFsVj6TjU6APqAhAvC07hTI6IQen8PHzHYY0= @@ -45,6 +55,8 @@ github.com/tklauser/numcpus v0.7.0 h1:yjuerZP127QG9m5Zh/mSO4wqurYil27tHrqwRoRjpr github.com/tklauser/numcpus v0.7.0/go.mod h1:bb6dMVcj8A42tSE7i32fsIUCbQNllK5iDguyOZRUzAY= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= +golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= @@ -60,6 +72,8 @@ golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw= golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= modernc.org/cc/v4 v4.19.3 h1:vE9kmJqUcyvNOf8F2Hn8od14SOMq34BiqcZ2tMzLk5c= diff --git a/log/Log.go b/log/Log.go index 0e2ec6d..3ee5437 100644 --- a/log/Log.go +++ b/log/Log.go @@ -1,7 +1,6 @@ package part import ( - "context" "database/sql" "fmt" "io" @@ -10,6 +9,7 @@ import ( "strings" "time" + pctx "github.com/qydysky/part/ctx" f "github.com/qydysky/part/file" m "github.com/qydysky/part/msgq" psql "github.com/qydysky/part/sql" @@ -73,7 +73,7 @@ func New(c Config) (o *Log_interface) { } } if msg.DBConn != nil && msg.DBInsert != `` { - sqlTx := psql.BeginTx[any](msg.DBConn, context.Background()) + sqlTx := psql.BeginTx[any](msg.DBConn, pctx.GenTOCtx(o.To)) sqlTx.SimpleDo( msg.DBInsert, strings.TrimSpace(msg.Prefix), diff --git a/log/Log_test.go b/log/Log_test.go index 58af2de..49aecc4 100644 --- a/log/Log_test.go +++ b/log/Log_test.go @@ -3,15 +3,17 @@ package part import ( // "fmt" - "context" "database/sql" "errors" + "log/slog" "testing" + "time" _ "net/http/pprof" _ "modernc.org/sqlite" + pctx "github.com/qydysky/part/ctx" psql "github.com/qydysky/part/sql" ) @@ -47,7 +49,7 @@ func Test_2(t *testing.T) { defer db.Close() { - tx := psql.BeginTx[any](db, context.Background(), &sql.TxOptions{}) + tx := psql.BeginTx[any](db, pctx.GenTOCtx(time.Second), &sql.TxOptions{}) tx = tx.Do(psql.SqlFunc[any]{ Query: "create table log (p test,base text,msg text)", SkipSqlErr: true, @@ -74,7 +76,7 @@ func Test_2(t *testing.T) { Base string Msg string `sql:"s"` } - tx := psql.BeginTx[any](db, context.Background(), &sql.TxOptions{}) + tx := psql.BeginTx[any](db, pctx.GenTOCtx(time.Second), &sql.TxOptions{}) tx = tx.SimpleDo("select p,base,msg as s from log") tx.AfterQF(func(_ *any, rows *sql.Rows, e *error) { if ls, err := psql.DealRows[logg](rows, func() logg { return logg{} }); err == nil { @@ -93,3 +95,9 @@ func Test_2(t *testing.T) { } } } + +func Test_3(t *testing.T) { + logger := slog.Default() + logger = logger.WithGroup("122") + logger.Info("sss", slog.String("1", "3")) +} diff --git a/sql/Sql_test.go b/sql/Sql_test.go index 1880c0e..6b9ad28 100644 --- a/sql/Sql_test.go +++ b/sql/Sql_test.go @@ -9,7 +9,8 @@ import ( "testing" "time" - _ "github.com/lib/pq" + _ "github.com/jackc/pgx/v5/stdlib" + pctx "github.com/qydysky/part/ctx" file "github.com/qydysky/part/file" _ "modernc.org/sqlite" ) @@ -260,17 +261,22 @@ func TestMain4(t *testing.T) { func Local_TestPostgresql(t *testing.T) { // connect - db, err := sql.Open("postgres", "postgres://postgres:qydysky@192.168.31.103:5432/postgres?sslmode=disable") + db, err := sql.Open("pgx", "postgres://postgres:qydysky@192.168.31.103:5432/postgres?sslmode=disable") if err != nil { t.Fatal(err) } defer db.Close() + // c := pctx.CarryCancel(context.WithTimeout(context.Background(), time.Second)) + // if e := db.PingContext(c); e != nil { + // t.Fatal(e) + // } + type test1 struct { Created string `sql:"sss"` } - if _, e := BeginTx[any](db, context.Background(), &sql.TxOptions{}).Do(SqlFunc[any]{ + if _, e := BeginTx[any](db, pctx.GenTOCtx(time.Second), &sql.TxOptions{}).Do(SqlFunc[any]{ Query: "create table test (created varchar(20))", SkipSqlErr: true, }).Fin(); e != nil {