Skip to content

Commit 384a179

Browse files
jrainvillealaibe
andauthored
fix(login): put slow operations in go routines to speed up login (#7349)
* fix(login): put slow operations in go routines to speed up login Needed for status-im/status-app#19856 During my investigation, I found that these two services take a long time to initiate and block the login process. Putting these calls in go routines fixes the issue * fix: started no in coroutine --------- Co-authored-by: Anthony Laibe <491074+alaibe@users.noreply.github.com>
1 parent da9e55d commit 384a179

3 files changed

Lines changed: 28 additions & 25 deletions

File tree

internal/timesource/ntp.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,6 @@ func (s *ntpTimeSource) updateOffset() error {
213213
// runPeriodically runs periodically the given function based on ntpTimeSource
214214
// synchronization limits (fastNTPSyncPeriod / slowNTPSyncPeriod)
215215
func (s *ntpTimeSource) runPeriodically(ctx context.Context, fn func() error, starWithSlowSyncPeriod bool) {
216-
if s.started {
217-
return
218-
}
219-
220216
period := s.fastNTPSyncPeriod
221217
if starWithSlowSyncPeriod {
222218
period = s.slowNTPSyncPeriod
@@ -289,25 +285,27 @@ func (s *ntpTimeSource) Start(ctx context.Context) error {
289285
currentTime := s.now()
290286
s.lastMonotonic = currentTime
291287
ctx, cancel := context.WithCancel(ctx)
292-
293-
// Attempt to update the offset synchronously so that user can have reliable messages right away
294-
err := s.updateOffset()
295-
if err != nil {
296-
// Failure to update can occur if the node is offline.
297-
// Instead of returning an error, continue with the process as the update will be retried periodically.
298-
logutils.ZapLogger().Error("failed to update offset", zap.Error(err))
299-
}
300-
301-
s.runPeriodically(ctx, s.updateOffset, err == nil)
302-
303-
s.started = true
304288
s.cancel = cancel
289+
s.started = true
290+
291+
go func() {
292+
defer common.LogOnPanic()
293+
err := s.updateOffset()
294+
if err != nil {
295+
// Failure to update can occur if the node is offline.
296+
// Instead of returning an error, continue with the process as the update will be retried periodically.
297+
logutils.ZapLogger().Error("failed to update offset", zap.Error(err))
298+
}
299+
s.runPeriodically(ctx, s.updateOffset, err == nil)
300+
}()
305301

306302
return nil
307303
}
308304

309305
// Stop goroutine that updates time source.
310306
func (s *ntpTimeSource) Stop() {
307+
s.stateMu.Lock()
308+
defer s.stateMu.Unlock()
311309
if s.cancel == nil {
312310
return
313311
}

internal/timesource/ntp_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ func TestGetCurrentTimeInMillis(t *testing.T) {
274274
}
275275

276276
expectedTime := convertToMillis(currentTime.Add(responseOffset))
277+
err := ts.updateOffset()
278+
require.NoError(t, err)
277279
n := ts.GetCurrentTimeInMillis()
278280
require.Equal(t, expectedTime, n)
279281
// test repeat invoke GetCurrentTimeInMillis

services/wallet/currency/service.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,19 @@ func NewService(db *sql.DB, walletFeed *event.Feed, tokenManager *token.Manager,
4444
}
4545

4646
func (s *Service) Start(ctx context.Context) {
47-
// Update all fiat currency formats in cache
48-
fiatFormats, err := s.getAllFiatCurrencyFormats()
49-
if err == nil {
50-
_ = s.db.UpdateCachedFormats(fiatFormats)
51-
}
47+
go func() {
48+
defer gocommon.LogOnPanic()
49+
// Update all fiat currency formats in cache
50+
fiatFormats, err := s.getAllFiatCurrencyFormats()
51+
if err == nil {
52+
_ = s.db.UpdateCachedFormats(fiatFormats)
53+
}
5254

53-
fixedTokenFormats, err := s.getAllFixedTokenCurrencyFormats()
54-
if err == nil {
55-
_ = s.db.UpdateCachedFormats(fixedTokenFormats)
56-
}
55+
fixedTokenFormats, err := s.getAllFixedTokenCurrencyFormats()
56+
if err == nil {
57+
_ = s.db.UpdateCachedFormats(fixedTokenFormats)
58+
}
59+
}()
5760

5861
go func() {
5962
defer gocommon.LogOnPanic()

0 commit comments

Comments
 (0)