@@ -63,12 +63,6 @@ func RunInTxContext(ctx context.Context, db BeginTxer, opts *TxOptions, fn func(
63
63
option .BackoffDelayFunc = opts .BackoffDelayFunc
64
64
}
65
65
66
- var backoffTimer * backoffTimer
67
- if option .BackoffDelayFunc != nil {
68
- backoffTimer = newBackoffTimer (option .BackoffDelayFunc )
69
- defer backoffTimer .Stop ()
70
- }
71
-
72
66
f := func () error {
73
67
tx , err := db .BeginTx (ctx , & option .TxOptions )
74
68
if err != nil {
@@ -94,8 +88,8 @@ func RunInTxContext(ctx context.Context, db BeginTxer, opts *TxOptions, fn func(
94
88
return err
95
89
}
96
90
97
- if backoffTimer != nil && i < option .MaxAttempts - 1 {
98
- if err = backoffTimer . Wait (ctx , i ); err != nil {
91
+ if i < option .MaxAttempts - 1 && option . BackoffDelayFunc != nil {
92
+ if err = wait (ctx , i , option . BackoffDelayFunc ); err != nil {
99
93
return err
100
94
}
101
95
}
@@ -104,34 +98,16 @@ func RunInTxContext(ctx context.Context, db BeginTxer, opts *TxOptions, fn func(
104
98
return err
105
99
}
106
100
107
- type backoffTimer struct {
108
- timer * time.Timer
109
- backOffDelayFunc BackoffDelayFunc
110
- }
111
-
112
- func newBackoffTimer (backoffDelayFunc BackoffDelayFunc ) * backoffTimer {
113
- return & backoffTimer {
114
- timer : time .NewTimer (0 ),
115
- backOffDelayFunc : backoffDelayFunc ,
116
- }
117
- }
118
-
119
- func (b * backoffTimer ) Wait (ctx context.Context , attempt int ) error {
120
- delay := b .backOffDelayFunc (attempt )
101
+ func wait (ctx context.Context , attempt int , backOffDelayFunc BackoffDelayFunc ) error {
102
+ delay := backOffDelayFunc (attempt )
121
103
if delay <= 0 {
122
104
return nil
123
105
}
124
106
125
- b .timer .Reset (delay )
126
-
127
107
select {
128
108
case <- ctx .Done ():
129
109
return ctx .Err ()
130
- case <- b . timer . C :
110
+ case <- time . After ( delay ) :
131
111
return nil
132
112
}
133
113
}
134
-
135
- func (b * backoffTimer ) Stop () bool {
136
- return b .timer .Stop ()
137
- }
0 commit comments