@@ -5,12 +5,13 @@ import (
55 "context"
66 "errors"
77 "fmt"
8+ "strings"
9+ "time"
10+
811 "github.com/xssnick/tonutils-go/address"
912 "github.com/xssnick/tonutils-go/tl"
1013 "github.com/xssnick/tonutils-go/tlb"
1114 "github.com/xssnick/tonutils-go/tvm/cell"
12- "strings"
13- "time"
1415)
1516
1617func init () {
@@ -176,52 +177,60 @@ func (c *APIClient) GetTransaction(ctx context.Context, block *BlockIDExt, addr
176177}
177178
178179func (c * APIClient ) SubscribeOnTransactions (workerCtx context.Context , addr * address.Address , lastProcessedLT uint64 , channel chan <- * tlb.Transaction ) {
179- defer func () {
180- close (channel )
181- }()
180+ defer close (channel )
182181
183182 wait := 0 * time .Second
183+ timer := time .NewTimer (wait )
184+ defer timer .Stop ()
185+
184186 for {
185187 select {
186188 case <- workerCtx .Done ():
187189 return
188- case <- time . After ( wait ) :
190+ case <- timer . C :
189191 }
190192 wait = 3 * time .Second
191193
192194 ctx , cancel := context .WithTimeout (workerCtx , 10 * time .Second )
193195 master , err := c .CurrentMasterchainInfo (ctx )
194196 cancel ()
195197 if err != nil {
198+ timer .Reset (wait )
196199 continue
197200 }
198201
199202 ctx , cancel = context .WithTimeout (workerCtx , 10 * time .Second )
200203 acc , err := c .GetAccount (ctx , master , addr )
201204 cancel ()
202205 if err != nil {
206+ timer .Reset (wait )
203207 continue
204208 }
205209 if ! acc .IsActive || acc .LastTxLT == 0 {
206210 // no transactions
211+ timer .Reset (wait )
207212 continue
208213 }
209214
210215 if lastProcessedLT == acc .LastTxLT {
211216 // already processed all
217+ timer .Reset (wait )
212218 continue
213219 }
214220
215221 var transactions []* tlb.Transaction
216222 lastHash , lastLT := acc .LastTxHash , acc .LastTxLT
217223
218224 waitList := 0 * time .Second
225+ listTimer := time .NewTimer (waitList )
226+
219227 list:
220228 for {
221229 select {
222230 case <- workerCtx .Done ():
231+ listTimer .Stop ()
223232 return
224- case <- time . After ( waitList ) :
233+ case <- listTimer . C :
225234 }
226235
227236 ctx , cancel = context .WithTimeout (workerCtx , 10 * time .Second )
@@ -230,9 +239,11 @@ func (c *APIClient) SubscribeOnTransactions(workerCtx context.Context, addr *add
230239 if err != nil {
231240 if lsErr , ok := err .(LSError ); ok && lsErr .Code == - 400 {
232241 // lt not in db error
242+ listTimer .Stop ()
233243 return
234244 }
235245 waitList = 3 * time .Second
246+ listTimer .Reset (waitList )
236247 continue
237248 }
238249
@@ -255,8 +266,11 @@ func (c *APIClient) SubscribeOnTransactions(workerCtx context.Context, addr *add
255266 lastLT , lastHash = res [len (res )- 1 ].PrevTxLT , res [len (res )- 1 ].PrevTxHash
256267 transactions = append (transactions , res ... )
257268 waitList = 0 * time .Second
269+ listTimer .Reset (waitList )
258270 }
259271
272+ listTimer .Stop ()
273+
260274 if len (transactions ) > 0 {
261275 lastProcessedLT = transactions [0 ].LT // mark last transaction as known to not trigger twice
262276
@@ -271,6 +285,8 @@ func (c *APIClient) SubscribeOnTransactions(workerCtx context.Context, addr *add
271285
272286 wait = 0 * time .Second
273287 }
288+
289+ timer .Reset (wait )
274290 }
275291}
276292
0 commit comments