[go] database/sql: simplify retry logic when got bad connection

350 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Jul 25, 2022, 7:59:34 AM7/25/22
to goph...@pubsubhelper.golang.org, Jinzhu Zhang, golang-co...@googlegroups.com

Gerrit Bot has uploaded this change for review.

View Change

database/sql: simplify retry logic when got bad connection

Simplify retry logic when got bad connection

Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
GitHub-Last-Rev: 58acbceedea74ea57d36b26f2ceb238deda204d9
GitHub-Pull-Request: golang/go#54043
---
M src/database/sql/sql.go
1 file changed, 79 insertions(+), 96 deletions(-)

diff --git a/src/database/sql/sql.go b/src/database/sql/sql.go
index 854a895..4f08ee4 100644
--- a/src/database/sql/sql.go
+++ b/src/database/sql/sql.go
@@ -846,17 +846,12 @@
func (db *DB) PingContext(ctx context.Context) error {
var dc *driverConn
var err error
- var isBadConn bool
- for i := 0; i < maxBadConnRetries; i++ {
- dc, err = db.conn(ctx, cachedOrNewConn)
- isBadConn = errors.Is(err, driver.ErrBadConn)
- if !isBadConn {
- break
- }
- }
- if isBadConn {
- dc, err = db.conn(ctx, alwaysNewConn)
- }
+
+ db.retry(func(strategy connReuseStrategy) error {
+ dc, err = db.conn(ctx, strategy)
+ return err
+ })
+
if err != nil {
return err
}
@@ -1539,6 +1534,21 @@
// connection to be opened.
const maxBadConnRetries = 2

+func (db *DB) retry(fn func(strategy connReuseStrategy) error) error {
+ for i := int64(0); i < maxBadConnRetries; i++ {
+ err := fn(cachedOrNewConn)
+ // retry if err is driver.ErrBadConn
+ if err == nil || !errors.Is(err, driver.ErrBadConn) {
+ return err
+ }
+ }
+
+ if maxBadConnRetries > 0 {
+ return fn(alwaysNewConn)
+ }
+ return fn(cachedOrNewConn)
+}
+
// PrepareContext creates a prepared statement for later queries or executions.
// Multiple queries or executions may be run concurrently from the
// returned statement.
@@ -1550,17 +1560,12 @@
func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
var stmt *Stmt
var err error
- var isBadConn bool
- for i := 0; i < maxBadConnRetries; i++ {
- stmt, err = db.prepare(ctx, query, cachedOrNewConn)
- isBadConn = errors.Is(err, driver.ErrBadConn)
- if !isBadConn {
- break
- }
- }
- if isBadConn {
- return db.prepare(ctx, query, alwaysNewConn)
- }
+
+ db.retry(func(strategy connReuseStrategy) error {
+ stmt, err = db.prepare(ctx, query, strategy)
+ return err
+ })
+
return stmt, err
}

@@ -1628,17 +1633,11 @@
func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (Result, error) {
var res Result
var err error
- var isBadConn bool
- for i := 0; i < maxBadConnRetries; i++ {
- res, err = db.exec(ctx, query, args, cachedOrNewConn)
- isBadConn = errors.Is(err, driver.ErrBadConn)
- if !isBadConn {
- break
- }
- }
- if isBadConn {
- return db.exec(ctx, query, args, alwaysNewConn)
- }
+ db.retry(func(strategy connReuseStrategy) error {
+ res, err = db.exec(ctx, query, args, strategy)
+ return err
+ })
+
return res, err
}

@@ -1703,17 +1702,12 @@
func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error) {
var rows *Rows
var err error
- var isBadConn bool
- for i := 0; i < maxBadConnRetries; i++ {
- rows, err = db.query(ctx, query, args, cachedOrNewConn)
- isBadConn = errors.Is(err, driver.ErrBadConn)
- if !isBadConn {
- break
- }
- }
- if isBadConn {
- return db.query(ctx, query, args, alwaysNewConn)
- }
+
+ db.retry(func(strategy connReuseStrategy) error {
+ rows, err = db.query(ctx, query, args, strategy)
+ return err
+ })
+
return rows, err
}

@@ -1840,17 +1834,12 @@
func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error) {
var tx *Tx
var err error
- var isBadConn bool
- for i := 0; i < maxBadConnRetries; i++ {
- tx, err = db.begin(ctx, opts, cachedOrNewConn)
- isBadConn = errors.Is(err, driver.ErrBadConn)
- if !isBadConn {
- break
- }
- }
- if isBadConn {
- return db.begin(ctx, opts, alwaysNewConn)
- }
+
+ db.retry(func(strategy connReuseStrategy) error {
+ tx, err = db.begin(ctx, opts, strategy)
+ return err
+ })
+
return tx, err
}

@@ -1921,17 +1910,12 @@
func (db *DB) Conn(ctx context.Context) (*Conn, error) {
var dc *driverConn
var err error
- var isBadConn bool
- for i := 0; i < maxBadConnRetries; i++ {
- dc, err = db.conn(ctx, cachedOrNewConn)
- isBadConn = errors.Is(err, driver.ErrBadConn)
- if !isBadConn {
- break
- }
- }
- if isBadConn {
- dc, err = db.conn(ctx, alwaysNewConn)
- }
+
+ db.retry(func(strategy connReuseStrategy) error {
+ dc, err = db.conn(ctx, strategy)
+ return err
+ })
+
if err != nil {
return nil, err
}
@@ -2620,26 +2604,18 @@
defer s.closemu.RUnlock()

var res Result
- strategy := cachedOrNewConn
- for i := 0; i < maxBadConnRetries+1; i++ {
- if i == maxBadConnRetries {
- strategy = alwaysNewConn
- }
+ err := s.db.retry(func(strategy connReuseStrategy) error {
dc, releaseConn, ds, err := s.connStmt(ctx, strategy)
if err != nil {
- if errors.Is(err, driver.ErrBadConn) {
- continue
- }
- return nil, err
+ return err
}

res, err = resultFromStatement(ctx, dc.ci, ds, args...)
releaseConn(err)
- if !errors.Is(err, driver.ErrBadConn) {
- return res, err
- }
- }
- return nil, driver.ErrBadConn
+ return err
+ })
+
+ return res, err
}

// Exec executes a prepared statement with the given arguments and
@@ -2768,24 +2744,19 @@
defer s.closemu.RUnlock()

var rowsi driver.Rows
- strategy := cachedOrNewConn
- for i := 0; i < maxBadConnRetries+1; i++ {
- if i == maxBadConnRetries {
- strategy = alwaysNewConn
- }
+ var rows *Rows
+
+ err := s.db.retry(func(strategy connReuseStrategy) error {
dc, releaseConn, ds, err := s.connStmt(ctx, strategy)
if err != nil {
- if errors.Is(err, driver.ErrBadConn) {
- continue
- }
- return nil, err
+ return err
}

rowsi, err = rowsiFromStatement(ctx, dc.ci, ds, args...)
if err == nil {
// Note: ownership of ci passes to the *Rows, to be freed
// with releaseConn.
- rows := &Rows{
+ rows = &Rows{
dc: dc,
rowsi: rowsi,
// releaseConn set below
@@ -2805,15 +2776,14 @@
txctx = s.cg.txCtx()
}
rows.initContextClose(ctx, txctx)
- return rows, nil
+ return nil
}

releaseConn(err)
- if !errors.Is(err, driver.ErrBadConn) {
- return nil, err
- }
- }
- return nil, driver.ErrBadConn
+ return err
+ })
+
+ return rows, err
}

// Query executes a prepared query statement with the given arguments

To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
Gerrit-Change-Number: 419182
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
Gerrit-MessageType: newchange

Daniel Theophanes (Gerrit)

unread,
Jul 25, 2022, 8:40:18 AM7/25/22
to Gerrit Bot, Jinzhu Zhang, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

View Change

1 comment:

  • Patchset:

    • Patch Set #1:

      Right now you are inconsistent with whether you use the error that retry returns or not. If you return an error, please use it. If you don't don't return it, please comment the error must be captured within the closure.

      I think your current code is correct, it is just inconsistent.

To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
Gerrit-Change-Number: 419182
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Daniel Theophanes <kard...@gmail.com>
Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
Gerrit-Comment-Date: Mon, 25 Jul 2022 12:40:14 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Jinzhu Zhang (Gerrit)

unread,
Jul 25, 2022, 9:03:18 AM7/25/22
to Gerrit Bot, goph...@pubsubhelper.golang.org, Daniel Theophanes, golang-co...@googlegroups.com

Attention is currently required from: Daniel Theophanes.

View Change

1 comment:

  • Patchset:

    • Patch Set #1:

      Right now you are inconsistent with whether you use the error that retry returns or not. […]

      Changed to use the returned error.

To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
Gerrit-Change-Number: 419182
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Daniel Theophanes <kard...@gmail.com>
Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
Gerrit-Comment-Date: Mon, 25 Jul 2022 13:03:12 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Daniel Theophanes <kard...@gmail.com>
Gerrit-MessageType: comment

Gerrit Bot (Gerrit)

unread,
Jul 25, 2022, 9:04:49 AM7/25/22
to Jinzhu Zhang, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Daniel Theophanes.

Gerrit Bot uploaded patch set #2 to this change.

View Change

database/sql: simplify retry logic when got bad connection

Simplify retry logic when got bad connection

Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
GitHub-Last-Rev: a3848947b9407a20248e9d1cd04eec8839c2adf1

GitHub-Pull-Request: golang/go#54043
---
M src/database/sql/sql.go
1 file changed, 80 insertions(+), 96 deletions(-)

To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
Gerrit-Change-Number: 419182
Gerrit-PatchSet: 2
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Daniel Theophanes <kard...@gmail.com>
Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
Gerrit-MessageType: newpatchset

Daniel Theophanes (Gerrit)

unread,
Jul 25, 2022, 9:51:53 AM7/25/22
to Gerrit Bot, Jinzhu Zhang, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Patch set 2:Run-TryBot +1

View Change

    To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
    Gerrit-Change-Number: 419182
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
    Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
    Gerrit-Comment-Date: Mon, 25 Jul 2022 13:51:49 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Daniel Theophanes (Gerrit)

    unread,
    Jul 26, 2022, 10:21:16 AM7/26/22
    to Gerrit Bot, Jinzhu Zhang, goph...@pubsubhelper.golang.org, Gopher Robot, golang-co...@googlegroups.com

    Patch set 2:Code-Review +2

    View Change

    1 comment:

    • Patchset:

      • Patch Set #2:

        I like how this simplifies the bad conn retry logic. It also simplifies a patch to remove it or make it configurable in a fork or in the future.

    To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
    Gerrit-Change-Number: 419182
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
    Gerrit-Comment-Date: Tue, 26 Jul 2022 14:21:12 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Cherry Mui (Gerrit)

    unread,
    Jul 26, 2022, 8:10:34 PM7/26/22
    to Gerrit Bot, Jinzhu Zhang, goph...@pubsubhelper.golang.org, Daniel Theophanes, Gopher Robot, golang-co...@googlegroups.com

    Patch set 2:Code-Review +1

    View Change

    2 comments:

    • Patchset:

      • Patch Set #2:

        Thanks for the CL. We're still in the Go 1.19 release freeze. This needs to wait for the next release.

    • File src/database/sql/sql.go:

      • Patch Set #2, Line 1549: return fn(cachedOrNewConn)

        This line is never reached as the constant maxBadConnRetries is not 0. Or the purpose for this line is to handle the case maxBadConnRetries is 0 (say, if it changes in the future)? If this is the case, it may be cleaner to write it separately at the top, something like

        if maxBadConnRetries == 0 { return fn(cachedOrNewConn) }
        for i := int64(0);... { ... }
        return fn(alwaysNewConn)

        ?

    To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
    Gerrit-Change-Number: 419182
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Cherry Mui <cher...@google.com>
    Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
    Gerrit-Comment-Date: Wed, 27 Jul 2022 00:10:30 +0000

    Jinzhu Zhang (Gerrit)

    unread,
    Jul 26, 2022, 11:12:20 PM7/26/22
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Cherry Mui, Daniel Theophanes, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Cherry Mui, Daniel Theophanes.

    View Change

    1 comment:

    • File src/database/sql/sql.go:

      • This line is never reached as the constant maxBadConnRetries is not 0. […]

        Good catch, the `maxBadConnRetries` was allowed to be configured in the original PR.

        So should we just remove the > 0 check? or we can change the `maxBadConnRetries` from `const` to `var`, which allow developers change its value with `go:linkname`

        cc @Daniel Theophanes

    To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
    Gerrit-Change-Number: 419182
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Cherry Mui <cher...@google.com>
    Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
    Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
    Gerrit-Attention: Cherry Mui <cher...@google.com>
    Gerrit-Comment-Date: Wed, 27 Jul 2022 03:12:14 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Cherry Mui <cher...@google.com>
    Gerrit-MessageType: comment

    Cherry Mui (Gerrit)

    unread,
    Jul 27, 2022, 10:53:37 AM7/27/22
    to Gerrit Bot, Jinzhu Zhang, goph...@pubsubhelper.golang.org, Daniel Theophanes, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Daniel Theophanes, Jinzhu Zhang.

    View Change

    1 comment:

    • File src/database/sql/sql.go:

      • I think it is fine to remove the >0 check.

      • or we can change the maxBadConnRetries from const to var , which allow developers change its value with go:linkname

      • Please don't. Using linkname to change a non-exported variable is VERY BAD practice. We should not do that. Thanks.

    To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
    Gerrit-Change-Number: 419182
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Cherry Mui <cher...@google.com>
    Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
    Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
    Gerrit-Attention: Jinzhu Zhang <wos...@gmail.com>
    Gerrit-Comment-Date: Wed, 27 Jul 2022 14:53:33 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Jinzhu Zhang <wos...@gmail.com>

    Jinzhu Zhang (Gerrit)

    unread,
    Jul 27, 2022, 11:13:01 AM7/27/22
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Cherry Mui, Daniel Theophanes, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Cherry Mui, Daniel Theophanes.

    View Change

    1 comment:

    • File src/database/sql/sql.go:

      • I think it is fine to remove the >0 check. […]

        removed the >0 check.

    To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
    Gerrit-Change-Number: 419182
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Cherry Mui <cher...@google.com>
    Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
    Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
    Gerrit-Attention: Cherry Mui <cher...@google.com>
    Gerrit-Comment-Date: Wed, 27 Jul 2022 15:12:55 +0000

    Gerrit Bot (Gerrit)

    unread,
    Jul 27, 2022, 11:13:05 AM7/27/22
    to Jinzhu Zhang, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Attention is currently required from: Cherry Mui, Daniel Theophanes.

    Gerrit Bot uploaded patch set #3 to this change.

    View Change

    The following approvals got outdated and were removed: Run-TryBot+1 by Daniel Theophanes, TryBot-Result+1 by Gopher Robot

    database/sql: simplify retry logic when got bad connection

    Simplify retry logic when got bad connection

    Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
    GitHub-Last-Rev: 7499b0c9419a31c9adce6d5096a1924aa3612f1d

    GitHub-Pull-Request: golang/go#54043
    ---
    M src/database/sql/sql.go
    1 file changed, 77 insertions(+), 96 deletions(-)

    To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
    Gerrit-Change-Number: 419182
    Gerrit-PatchSet: 3
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Cherry Mui <cher...@google.com>
    Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
    Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
    Gerrit-Attention: Cherry Mui <cher...@google.com>
    Gerrit-MessageType: newpatchset

    Jinzhu Zhang (Gerrit)

    unread,
    Aug 24, 2022, 11:13:24 PM8/24/22
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Cherry Mui, Daniel Theophanes, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Cherry Mui, Daniel Theophanes.

    View Change

    1 comment:

    • Patchset:

      • Patch Set #3:

        Hello @kardianos @cherryyz, is there any other feedback for this PR?

        Thank you.

    To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
    Gerrit-Change-Number: 419182
    Gerrit-PatchSet: 3
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Cherry Mui <cher...@google.com>
    Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
    Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
    Gerrit-Attention: Cherry Mui <cher...@google.com>
    Gerrit-Comment-Date: Thu, 25 Aug 2022 03:13:17 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    hopehook (Gerrit)

    unread,
    Sep 1, 2022, 8:26:04 AM9/1/22
    to Gerrit Bot, Jinzhu Zhang, goph...@pubsubhelper.golang.org, Cherry Mui, Daniel Theophanes, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Cherry Mui, Daniel Theophanes.

    Patch set 3:Run-TryBot +1

    View Change

      To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
      Gerrit-Change-Number: 419182
      Gerrit-PatchSet: 3
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Cherry Mui <cher...@google.com>
      Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: hopehook <hope...@golangcn.org>
      Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
      Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
      Gerrit-Attention: Cherry Mui <cher...@google.com>
      Gerrit-Comment-Date: Thu, 01 Sep 2022 12:25:58 +0000

      Benny Siegert (Gerrit)

      unread,
      Sep 1, 2022, 10:13:27 AM9/1/22
      to Gerrit Bot, Jinzhu Zhang, goph...@pubsubhelper.golang.org, Benny Siegert, Gopher Robot, hopehook, Cherry Mui, Daniel Theophanes, golang-co...@googlegroups.com

      Attention is currently required from: Cherry Mui, Daniel Theophanes.

      Patch set 3:Code-Review +1

      View Change

      1 comment:

      • Commit Message:

      To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
      Gerrit-Change-Number: 419182
      Gerrit-PatchSet: 3
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Benny Siegert <bsie...@gmail.com>
      Gerrit-Reviewer: Cherry Mui <cher...@google.com>
      Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: hopehook <hope...@golangcn.org>
      Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
      Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
      Gerrit-Attention: Cherry Mui <cher...@google.com>
      Gerrit-Comment-Date: Thu, 01 Sep 2022 14:13:21 +0000

      Jinzhu Zhang (Gerrit)

      unread,
      Sep 1, 2022, 10:11:53 PM9/1/22
      to Gerrit Bot, goph...@pubsubhelper.golang.org, Benny Siegert, Gopher Robot, hopehook, Cherry Mui, Daniel Theophanes, golang-co...@googlegroups.com

      Attention is currently required from: Benny Siegert, Cherry Mui, Daniel Theophanes.

      View Change

      1 comment:

      • Commit Message:

        • In case of ErrBadConn happen? usually network problem.

      To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
      Gerrit-Change-Number: 419182
      Gerrit-PatchSet: 3
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Benny Siegert <bsie...@gmail.com>
      Gerrit-Reviewer: Cherry Mui <cher...@google.com>
      Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: hopehook <hope...@golangcn.org>
      Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
      Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
      Gerrit-Attention: Benny Siegert <bsie...@gmail.com>
      Gerrit-Attention: Cherry Mui <cher...@google.com>
      Gerrit-Comment-Date: Fri, 02 Sep 2022 02:11:46 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Benny Siegert <bsie...@gmail.com>
      Gerrit-MessageType: comment

      Zeke Lu (Gerrit)

      unread,
      Sep 2, 2022, 2:23:12 AM9/2/22
      to Gerrit Bot, Jinzhu Zhang, goph...@pubsubhelper.golang.org, Benny Siegert, Gopher Robot, hopehook, Cherry Mui, Daniel Theophanes, golang-co...@googlegroups.com

      Attention is currently required from: Benny Siegert, Cherry Mui, Daniel Theophanes, Jinzhu Zhang.

      View Change

      1 comment:

      • Commit Message:

        • In case of ErrBadConn happen? usually network problem.

          I think Benny suggested to change the commit message to `Simplify retry logic when the connection is bad`.

      To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
      Gerrit-Change-Number: 419182
      Gerrit-PatchSet: 3
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Benny Siegert <bsie...@gmail.com>
      Gerrit-Reviewer: Cherry Mui <cher...@google.com>
      Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: hopehook <hope...@golangcn.org>
      Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
      Gerrit-CC: Zeke Lu <lvz...@gmail.com>
      Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
      Gerrit-Attention: Jinzhu Zhang <wos...@gmail.com>
      Gerrit-Attention: Benny Siegert <bsie...@gmail.com>
      Gerrit-Attention: Cherry Mui <cher...@google.com>
      Gerrit-Comment-Date: Fri, 02 Sep 2022 06:23:05 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Jinzhu Zhang <wos...@gmail.com>

      Gerrit Bot (Gerrit)

      unread,
      Sep 5, 2022, 5:02:18 AM9/5/22
      to Jinzhu Zhang, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

      Attention is currently required from: Benny Siegert, Cherry Mui, Daniel Theophanes, Jinzhu Zhang.

      Gerrit Bot uploaded patch set #4 to this change.

      View Change

      database/sql: simplify retry logic when the connection is bad


      Simplify retry logic when got bad connection

      Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
      GitHub-Last-Rev: 7499b0c9419a31c9adce6d5096a1924aa3612f1d
      GitHub-Pull-Request: golang/go#54043
      ---
      M src/database/sql/sql.go
      1 file changed, 77 insertions(+), 96 deletions(-)

      To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
      Gerrit-Change-Number: 419182
      Gerrit-PatchSet: 4
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Benny Siegert <bsie...@gmail.com>
      Gerrit-Reviewer: Cherry Mui <cher...@google.com>
      Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: hopehook <hope...@golangcn.org>
      Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
      Gerrit-CC: Zeke Lu <lvz...@gmail.com>
      Gerrit-Attention: Daniel Theophanes <kard...@gmail.com>
      Gerrit-Attention: Jinzhu Zhang <wos...@gmail.com>
      Gerrit-Attention: Benny Siegert <bsie...@gmail.com>
      Gerrit-Attention: Cherry Mui <cher...@google.com>
      Gerrit-MessageType: newpatchset

      Daniel Theophanes (Gerrit)

      unread,
      Sep 7, 2022, 10:34:02 AM9/7/22
      to Gerrit Bot, Jinzhu Zhang, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Zeke Lu, Benny Siegert, Gopher Robot, hopehook, Cherry Mui, golang-co...@googlegroups.com

      Daniel Theophanes submitted this change.

      View Change



      2 is the latest approved patch-set.
      The change was submitted with unreviewed changes in the following files:

      ```
      The name of the file: src/database/sql/sql.go
      Insertions: 1, Deletions: 4.

      @@ -1543,10 +1543,7 @@
      }
      }

      - if maxBadConnRetries > 0 {
      - return fn(alwaysNewConn)
      - }
      - return fn(cachedOrNewConn)
      + return fn(alwaysNewConn)

      }

      // PrepareContext creates a prepared statement for later queries or executions.
      ```

      Approvals: Benny Siegert: Looks good to me, but someone else must approve Gopher Robot: TryBots succeeded Daniel Theophanes: Looks good to me, approved Cherry Mui: Looks good to me, but someone else must approve hopehook: Run TryBots
      database/sql: simplify retry logic when the connection is bad

      Simplify retry logic when got bad connection

      Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
      GitHub-Last-Rev: 7499b0c9419a31c9adce6d5096a1924aa3612f1d
      GitHub-Pull-Request: golang/go#54043
      Reviewed-on: https://go-review.googlesource.com/c/go/+/419182
      Reviewed-by: Daniel Theophanes <kard...@gmail.com>
      Reviewed-by: Cherry Mui <cher...@google.com>
      Reviewed-by: Benny Siegert <bsie...@gmail.com>
      Run-TryBot: hopehook <hope...@golangcn.org>
      TryBot-Result: Gopher Robot <go...@golang.org>
      ---
      M src/database/sql/sql.go
      1 file changed, 83 insertions(+), 96 deletions(-)

      diff --git a/src/database/sql/sql.go b/src/database/sql/sql.go
      index 854a895..e74dd87 100644

      --- a/src/database/sql/sql.go
      +++ b/src/database/sql/sql.go
      @@ -846,17 +846,12 @@
      func (db *DB) PingContext(ctx context.Context) error {
      var dc *driverConn
      var err error
      - var isBadConn bool
      - for i := 0; i < maxBadConnRetries; i++ {
      - dc, err = db.conn(ctx, cachedOrNewConn)
      - isBadConn = errors.Is(err, driver.ErrBadConn)
      - if !isBadConn {
      - break
      - }
      - }
      - if isBadConn {
      - dc, err = db.conn(ctx, alwaysNewConn)
      - }
      +
      +	err = db.retry(func(strategy connReuseStrategy) error {

      + dc, err = db.conn(ctx, strategy)
      + return err
      + })
      +
      if err != nil {
      return err
      }
      @@ -1539,6 +1534,18 @@

      // connection to be opened.
      const maxBadConnRetries = 2

      +func (db *DB) retry(fn func(strategy connReuseStrategy) error) error {
      + for i := int64(0); i < maxBadConnRetries; i++ {
      + err := fn(cachedOrNewConn)
      + // retry if err is driver.ErrBadConn
      + if err == nil || !errors.Is(err, driver.ErrBadConn) {
      + return err
      + }
      + }
      +
      +	return fn(alwaysNewConn)

      +}
      +
      // PrepareContext creates a prepared statement for later queries or executions.
      // Multiple queries or executions may be run concurrently from the
      // returned statement.
      @@ -1550,17 +1557,12 @@

      func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
      var stmt *Stmt
      var err error
      - var isBadConn bool
      - for i := 0; i < maxBadConnRetries; i++ {
      - stmt, err = db.prepare(ctx, query, cachedOrNewConn)
      - isBadConn = errors.Is(err, driver.ErrBadConn)
      - if !isBadConn {
      - break
      - }
      - }
      - if isBadConn {
      - return db.prepare(ctx, query, alwaysNewConn)
      - }
      +
      +	err = db.retry(func(strategy connReuseStrategy) error {

      + stmt, err = db.prepare(ctx, query, strategy)
      + return err
      + })
      +
      return stmt, err
      }

      @@ -1628,17 +1630,12 @@

      func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (Result, error) {
      var res Result
      var err error
      - var isBadConn bool
      - for i := 0; i < maxBadConnRetries; i++ {
      - res, err = db.exec(ctx, query, args, cachedOrNewConn)
      - isBadConn = errors.Is(err, driver.ErrBadConn)
      - if !isBadConn {
      - break
      - }
      - }
      - if isBadConn {
      - return db.exec(ctx, query, args, alwaysNewConn)
      - }
      +
      +	err = db.retry(func(strategy connReuseStrategy) error {

      + res, err = db.exec(ctx, query, args, strategy)
      + return err
      + })
      +
      return res, err
      }

      @@ -1703,17 +1700,12 @@

      func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error) {
      var rows *Rows
      var err error
      - var isBadConn bool
      - for i := 0; i < maxBadConnRetries; i++ {
      - rows, err = db.query(ctx, query, args, cachedOrNewConn)
      - isBadConn = errors.Is(err, driver.ErrBadConn)
      - if !isBadConn {
      - break
      - }
      - }
      - if isBadConn {
      - return db.query(ctx, query, args, alwaysNewConn)
      - }
      +
      +	err = db.retry(func(strategy connReuseStrategy) error {

      + rows, err = db.query(ctx, query, args, strategy)
      + return err
      + })
      +
      return rows, err
      }

      @@ -1840,17 +1832,12 @@

      func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error) {
      var tx *Tx
      var err error
      - var isBadConn bool
      - for i := 0; i < maxBadConnRetries; i++ {
      - tx, err = db.begin(ctx, opts, cachedOrNewConn)
      - isBadConn = errors.Is(err, driver.ErrBadConn)
      - if !isBadConn {
      - break
      - }
      - }
      - if isBadConn {
      - return db.begin(ctx, opts, alwaysNewConn)
      - }
      +
      +	err = db.retry(func(strategy connReuseStrategy) error {

      + tx, err = db.begin(ctx, opts, strategy)
      + return err
      + })
      +
      return tx, err
      }

      @@ -1921,17 +1908,12 @@

      func (db *DB) Conn(ctx context.Context) (*Conn, error) {
      var dc *driverConn
      var err error
      - var isBadConn bool
      - for i := 0; i < maxBadConnRetries; i++ {
      - dc, err = db.conn(ctx, cachedOrNewConn)
      - isBadConn = errors.Is(err, driver.ErrBadConn)
      - if !isBadConn {
      - break
      - }
      - }
      - if isBadConn {
      - dc, err = db.conn(ctx, alwaysNewConn)
      - }
      +
      +	err = db.retry(func(strategy connReuseStrategy) error {

      + dc, err = db.conn(ctx, strategy)
      + return err
      + })
      +
      if err != nil {
      return nil, err
      }
      @@ -2620,26 +2602,18 @@
      @@ -2768,24 +2742,19 @@
      @@ -2805,15 +2774,14 @@

      txctx = s.cg.txCtx()
      }
      rows.initContextClose(ctx, txctx)
      - return rows, nil
      + return nil
      }

      releaseConn(err)
      - if !errors.Is(err, driver.ErrBadConn) {
      - return nil, err
      - }
      - }
      - return nil, driver.ErrBadConn
      + return err
      + })
      +
      + return rows, err
      }

      // Query executes a prepared query statement with the given arguments

      To view, visit change 419182. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I92494c6c020576ec01bc4868334ee920ded7aa57
      Gerrit-Change-Number: 419182
      Gerrit-PatchSet: 5
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Benny Siegert <bsie...@gmail.com>
      Gerrit-Reviewer: Cherry Mui <cher...@google.com>
      Gerrit-Reviewer: Daniel Theophanes <kard...@gmail.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: hopehook <hope...@golangcn.org>
      Gerrit-CC: Jinzhu Zhang <wos...@gmail.com>
      Gerrit-CC: Zeke Lu <lvz...@gmail.com>
      Gerrit-MessageType: merged
      Reply all
      Reply to author
      Forward
      0 new messages