mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-10 16:30:01 +08:00
feat: use breaker with ctx to prevent deadline exceeded (#4091)
Signed-off-by: kevin <wanjunfeng@gmail.com>
This commit is contained in:
@@ -83,7 +83,7 @@ func NewSqlConn(driverName, datasource string, opts ...SqlOption) SqlConn {
|
||||
}
|
||||
|
||||
// NewSqlConnFromDB returns a SqlConn with the given sql.DB.
|
||||
// Use it with caution, it's provided for other ORM to interact with.
|
||||
// Use it with caution; it's provided for other ORM to interact with.
|
||||
func NewSqlConnFromDB(db *sql.DB, opts ...SqlOption) SqlConn {
|
||||
conn := &commonSqlConn{
|
||||
connProv: func() (*sql.DB, error) {
|
||||
@@ -120,7 +120,7 @@ func (db *commonSqlConn) ExecCtx(ctx context.Context, q string, args ...any) (
|
||||
endSpan(span, err)
|
||||
}()
|
||||
|
||||
err = db.brk.DoWithAcceptable(func() error {
|
||||
err = db.brk.DoWithAcceptableCtx(ctx, func() error {
|
||||
var conn *sql.DB
|
||||
conn, err = db.connProv()
|
||||
if err != nil {
|
||||
@@ -148,7 +148,7 @@ func (db *commonSqlConn) PrepareCtx(ctx context.Context, query string) (stmt Stm
|
||||
endSpan(span, err)
|
||||
}()
|
||||
|
||||
err = db.brk.DoWithAcceptable(func() error {
|
||||
err = db.brk.DoWithAcceptableCtx(ctx, func() error {
|
||||
var conn *sql.DB
|
||||
conn, err = db.connProv()
|
||||
if err != nil {
|
||||
@@ -256,7 +256,7 @@ func (db *commonSqlConn) TransactCtx(ctx context.Context, fn func(context.Contex
|
||||
endSpan(span, err)
|
||||
}()
|
||||
|
||||
err = db.brk.DoWithAcceptable(func() error {
|
||||
err = db.brk.DoWithAcceptableCtx(ctx, func() error {
|
||||
return transact(ctx, db, db.beginTx, fn)
|
||||
}, db.acceptable)
|
||||
if errors.Is(err, breaker.ErrServiceUnavailable) {
|
||||
@@ -287,7 +287,7 @@ func (db *commonSqlConn) acceptable(err error) bool {
|
||||
func (db *commonSqlConn) queryRows(ctx context.Context, scanner func(*sql.Rows) error,
|
||||
q string, args ...any) (err error) {
|
||||
var scanFailed bool
|
||||
err = db.brk.DoWithAcceptable(func() error {
|
||||
err = db.brk.DoWithAcceptableCtx(ctx, func() error {
|
||||
conn, err := db.connProv()
|
||||
if err != nil {
|
||||
db.onError(ctx, err)
|
||||
|
||||
@@ -65,7 +65,7 @@ func (s statement) ExecCtx(ctx context.Context, args ...any) (result sql.Result,
|
||||
endSpan(span, err)
|
||||
}()
|
||||
|
||||
err = s.brk.DoWithAcceptable(func() error {
|
||||
err = s.brk.DoWithAcceptableCtx(ctx, func() error {
|
||||
result, err = execStmt(ctx, s.stmt, s.query, args...)
|
||||
return err
|
||||
}, func(err error) bool {
|
||||
@@ -141,7 +141,7 @@ func (s statement) QueryRowsPartialCtx(ctx context.Context, v any, args ...any)
|
||||
func (s statement) queryRows(ctx context.Context, scanFn func(any, rowsScanner) error,
|
||||
v any, args ...any) error {
|
||||
var scanFailed bool
|
||||
err := s.brk.DoWithAcceptable(func() error {
|
||||
err := s.brk.DoWithAcceptableCtx(ctx, func() error {
|
||||
return queryStmt(ctx, s.stmt, func(rows *sql.Rows) error {
|
||||
err := scanFn(v, rows)
|
||||
if isScanFailed(err) {
|
||||
|
||||
Reference in New Issue
Block a user