]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Improve 连接db超时提示
authorqydysky <qydysky@foxmail.com>
Mon, 22 Apr 2024 18:44:44 +0000 (18:44 +0000)
committerqydysky <qydysky@foxmail.com>
Mon, 22 Apr 2024 18:44:44 +0000 (18:44 +0000)
CV/Var.go
README.md
Reply/F.go
Reply/F_test.go
go.mod
go.sum

index 1be1c65aca56b0154f1ab073365b350569e70f65..239dad981752c8c9054ca1d979b6ec744082c0ce 100644 (file)
--- a/CV/Var.go
+++ b/CV/Var.go
@@ -1,7 +1,6 @@
 package cv
 
 import (
-       "context"
        "database/sql"
        _ "embed"
        "encoding/json"
@@ -21,7 +20,8 @@ import (
 
        "github.com/dustin/go-humanize"
        _ "github.com/go-sql-driver/mysql"
-       _ "github.com/lib/pq"
+       _ "github.com/jackc/pgx/v5/stdlib"
+       pctx "github.com/qydysky/part/ctx"
        file "github.com/qydysky/part/file"
        log "github.com/qydysky/part/log"
        mq "github.com/qydysky/part/msgq"
@@ -467,17 +467,20 @@ func (t *Common) Init() *Common {
                        if dbnameok && urlok && insertok && dbname != "" && url != "" && insert != "" {
                                db, e := sql.Open(dbname, url)
                                if e != nil {
-                                       panic("保存日志至db打开连接错误" + e.Error())
+                                       panic("保存日志至db打开连接错误 " + e.Error())
                                }
                                if createok {
-                                       tx := psql.BeginTx[any](db, context.Background())
+                                       fmt.Println(11)
+                                       tx := psql.BeginTx[any](db, pctx.GenTOCtx(time.Second*5))
                                        tx.Do(psql.SqlFunc[any]{
                                                Query:      create,
                                                SkipSqlErr: true,
                                        })
-                                       _, _ = tx.Fin()
+                                       if _, e := tx.Fin(); e != nil {
+                                               panic("保存日志至db打开连接错误 " + e.Error())
+                                       }
                                }
-                               t.Log = t.Log.LDB(db, insert)
+                               t.Log = t.Log.LDB(db, insert, time.Second*5)
                        }
                }
 
index ec218cbdd17f35c3fa9f9544cc2caf173eed3e26..9ef441e0953b050c343c96d65497180f256e750a 100644 (file)
--- a/README.md
+++ b/README.md
@@ -176,7 +176,7 @@ postgreSql:
 ```json
 {
   "保存弹幕至db": {
-      "dbname": "postgres",
+      "dbname": "pgx",
       "url":"postgres://postgres:qydysky@192.168.31.103:5432/postgres?sslmode=disable",
       "字段help":"time.Now().Format(time.DateTime), time.Now().Unix(), item.msg, item.color, item.auth, item.uid, item.roomid",
       "create":"create table danmu (created varchar(20), createdunix varchar(20), msg varchar(100), color varchar(20), auth varchar(50), uid varchar(30), roomid varchar(30))",
@@ -443,14 +443,14 @@ ass编码GB18030支持中文
     "生成pgo":"default.pgo",
     "扫码登录":true,
     "保存弹幕至db": {
-        "dbname": "postgres",
+        "dbname": "pgx",
         "url":"postgres://postgres:qydysky@192.168.31.103:5432/postgres?sslmode=disable",
         "字段help":"time.Now().Format(time.DateTime), time.Now().Unix(), item.msg, item.color, item.auth, item.uid, item.roomid",
         "create":"create table danmu (created varchar(20), createdunix varchar(20), msg varchar(100), color varchar(20), auth varchar(50), uid varchar(30), roomid varchar(30))",
         "insert":"insert into danmu (created, createdunix, msg, color, auth, uid, roomid) values ({Date},{Unix},{Msg},{Color},{Auth},{Uid},{Roomid})"
     },
     "保存日志至db":{
-        "dbname": "postgres",
+        "dbname": "pgx",
         "url":"postgres://postgres:qydysky@192.168.31.103:5432/postgres?sslmode=disable",
         "字段help":"$1:Prefix $2:Base $3:Msgs, use ? or $%d not placeholder",
         "create":"create table log (created timestamp, Prefix varchar(10), Base varchar(30), Msgs varchar(500))",
index 4a0c8d2a325f022db0b81035cab8328f5183a2e4..784a4a184dfacbf06c60ad543351bbcb9465b3db 100644 (file)
@@ -1782,7 +1782,7 @@ func (t *SaveDanmuToDB) init(c *c.Common) {
                                db.SetMaxIdleConns(10)
                                t.db = db
                                if createok {
-                                       tx := psql.BeginTx[any](db, context.Background())
+                                       tx := psql.BeginTx[any](db, pctx.GenTOCtx(time.Second*5))
                                        tx.Do(psql.SqlFunc[any]{Query: create, SkipSqlErr: true})
                                        if _, e := tx.Fin(); e != nil {
                                                c.Log.Base_add("保存弹幕至db").L(`E: `, e)
@@ -1817,7 +1817,7 @@ func (t *SaveDanmuToDB) danmu(item Danmu_item) {
                        })
                }
 
-               tx := psql.BeginTx[any](t.db, context.Background())
+               tx := psql.BeginTx[any](t.db, pctx.GenTOCtx(time.Second*5))
                tx.DoPlaceHolder(psql.SqlFunc[any]{Query: t.insert}, &DanmuI{
                        Date:   time.Now().Format(time.DateTime),
                        Unix:   time.Now().Unix(),
index 72824290d4408c54d70beaf619a829f366cece49..086dc6745cc2cd8fe579f68ccbbc0263f0df3e08 100644 (file)
@@ -5,9 +5,11 @@ import (
        "database/sql"
        "errors"
        "testing"
+       "time"
 
        c "github.com/qydysky/bili_danmu/CV"
        videoInfo "github.com/qydysky/bili_danmu/Reply/F/videoInfo"
+       pctx "github.com/qydysky/part/ctx"
        psql "github.com/qydysky/part/sql"
 )
 
@@ -31,7 +33,7 @@ func TestSaveDanmuToDB(t *testing.T) {
        if db, e := sql.Open("sqlite", "danmu.sqlite3"); e != nil {
                t.Fatal(e)
        } else {
-               tx := psql.BeginTx[any](db, context.Background())
+               tx := psql.BeginTx[any](db, pctx.GenTOCtx(time.Second*5))
                tx.Do(psql.SqlFunc[any]{Query: "select msg as Msg from danmu"})
                tx.AfterQF(func(_ *any, rows *sql.Rows, e *error) {
                        type row struct {
diff --git a/go.mod b/go.mod
index aac896dd2c31edd23a8235369bea075c5c86d170..55d85756baf6b881a767fb79e72076d72bcac981 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -5,7 +5,7 @@ go 1.22.2
 require (
        github.com/gotk3/gotk3 v0.6.3
        github.com/mdp/qrterminal/v3 v3.2.0
-       github.com/qydysky/part v0.28.20240418171545
+       github.com/qydysky/part v0.28.20240422183953
        github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
        github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
        golang.org/x/text v0.14.0
@@ -20,9 +20,14 @@ require (
 require (
        filippo.io/edwards25519 v1.1.0 // indirect
        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/mattn/go-isatty v0.0.20 // indirect
        github.com/ncruces/go-strftime v0.1.9 // indirect
        github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
+       golang.org/x/crypto v0.21.0 // indirect
+       golang.org/x/sync v0.6.0 // indirect
        golang.org/x/term v0.18.0 // indirect
        modernc.org/gc/v3 v3.0.0-20240304020402-f0dba7c97c2b // indirect
        modernc.org/libc v1.49.0 // indirect
@@ -34,11 +39,11 @@ require (
 
 require (
        github.com/andybalholm/brotli v1.1.0
-       github.com/davecgh/go-spew v1.1.1 // indirect
        github.com/dustin/go-humanize v1.0.1
        github.com/go-ole/go-ole v1.3.0 // indirect
        github.com/go-sql-driver/mysql v1.8.1
        github.com/gorilla/websocket v1.5.1 // indirect
+       github.com/jackc/pgx/v5 v5.5.5
        github.com/klauspost/compress v1.17.7 // indirect
        github.com/lib/pq v1.10.9
        github.com/miekg/dns v1.1.58 // indirect
@@ -51,11 +56,10 @@ require (
        golang.org/x/net v0.22.0 // indirect
        golang.org/x/sys v0.18.0 // indirect
        golang.org/x/tools v0.19.0 // indirect
-       gopkg.in/yaml.v3 v3.0.1 // indirect
        modernc.org/sqlite v1.29.5
        rsc.io/qr v0.2.0 // indirect
 )
 
-// replace github.com/qydysky/part => ../part
+replace github.com/qydysky/part => ../part
 
-// replace github.com/qydysky/biliApi => ../biliapi
+replace github.com/qydysky/biliApi => ../biliapi
diff --git a/go.sum b/go.sum
index a95773d0122b65204c265ac43c8cfd2116b78261..eba18941a3bd7dad0adc69ba10eb6995679c1506 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -2,6 +2,7 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
 filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
 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=
@@ -21,6 +22,14 @@ github.com/gotk3/gotk3 v0.6.3 h1:+Ke4WkM1TQUNOlM2TZH6szqknqo+zNbX3BZWVXjSHYw=
 github.com/gotk3/gotk3 v0.6.3/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q=
 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=
@@ -37,10 +46,6 @@ github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdh
 github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
-github.com/qydysky/biliApi v0.0.0-20240418173820-af01f8c9ae11 h1:oLBPZWV4guuYNnlHJFnIL+0PJq0ZutneV6g40T0w3ng=
-github.com/qydysky/biliApi v0.0.0-20240418173820-af01f8c9ae11/go.mod h1:81lx2cYjv590O3ZK1AVtnLJ8JMXMluMtyB1NGAlTuPM=
-github.com/qydysky/part v0.28.20240418171545 h1:j8ZRnhUoGjm6xLGwXsz8yDHJjbAICP3JmPwYdnhKZWU=
-github.com/qydysky/part v0.28.20240418171545/go.mod h1:XytV5dI1Y7+qvjhsa2TMvi55RBZQQf0LCDYQ1kUCYqM=
 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
 github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
@@ -49,6 +54,9 @@ github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1
 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
 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=
@@ -59,6 +67,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/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
 golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
 golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
@@ -79,6 +89,7 @@ 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.5 h1:QlsZyQ1zf78DGeqnQ9ILi9hXyMdoC5e1qoGNUyBjHQw=