Skip to content

Commit d1bd95b

Browse files
committed
[online] fix edge cases
1 parent d5e212e commit d1bd95b

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

internal/sms-gateway/modules/auth/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ func (s *Service) AuthorizeDevice(token string) (models.Device, error) {
145145

146146
go func(id string) {
147147
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
148+
defer cancel()
148149
s.onlineSvc.SetOnline(ctx, id)
149-
cancel()
150150
}(device.ID)
151151

152152
device.LastSeen = time.Now()

internal/sms-gateway/modules/devices/repository.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,19 @@ func (r *repository) UpdatePushToken(id, token string) error {
7272
}
7373

7474
func (r *repository) SetLastSeen(ctx context.Context, id string, lastSeen time.Time) error {
75-
return r.db.WithContext(ctx).
75+
if lastSeen.IsZero() {
76+
return nil // ignore zero timestamps
77+
}
78+
res := r.db.WithContext(ctx).
7679
Model(&models.Device{}).
77-
Where("id", id).
78-
Update("last_seen", lastSeen).
79-
Error
80+
Where("id = ? AND last_seen < ?", id, lastSeen).
81+
Update("last_seen", lastSeen)
82+
if res.Error != nil {
83+
return res.Error
84+
}
85+
86+
// RowsAffected==0 => not found or stale timestamp; treat as no-op.
87+
return nil
8088
}
8189

8290
func (r *repository) Remove(filter ...SelectFilter) error {

internal/sms-gateway/modules/devices/service.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,21 @@ func (s *Service) UpdatePushToken(deviceId string, token string) error {
101101
}
102102

103103
func (s *Service) SetLastSeen(ctx context.Context, batch map[string]time.Time) error {
104+
if len(batch) == 0 {
105+
return nil
106+
}
107+
104108
for deviceId, lastSeen := range batch {
109+
if ctx.Err() != nil {
110+
break
111+
}
112+
105113
if err := s.devices.SetLastSeen(ctx, deviceId, lastSeen); err != nil {
106114
s.logger.Error("can't set last seen", zap.String("device_id", deviceId), zap.Error(err))
107115
}
108116
}
109117

110-
return nil
118+
return ctx.Err()
111119
}
112120

113121
// Remove removes devices for a specific user that match the provided filters.

internal/sms-gateway/online/service.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ func (s *service) Run(ctx context.Context) {
4343
case <-ctx.Done():
4444
return
4545
case <-ticker.C:
46-
s.logger.Info("Persisting online status")
46+
s.logger.Debug("Persisting online status")
4747
if err := s.persist(ctx); err != nil {
4848
s.logger.Error("Can't persist online status", zap.Error(err))
49-
} else {
50-
s.logger.Info("Online status persisted")
5149
}
5250
}
5351
}
@@ -72,7 +70,11 @@ func (s *service) persist(ctx context.Context) error {
7270
return fmt.Errorf("can't drain cache: %w", err)
7371
}
7472

75-
s.logger.Info("Drained cache", zap.Int("count", len(items)))
73+
if len(items) == 0 {
74+
s.logger.Debug("No online statuses to persist")
75+
return nil
76+
}
77+
s.logger.Debug("Drained cache", zap.Int("count", len(items)))
7678

7779
timestamps := maps.MapValues(items, func(v string) time.Time {
7880
t, err := time.Parse(time.RFC3339, v)
@@ -84,7 +86,7 @@ func (s *service) persist(ctx context.Context) error {
8486
return t
8587
})
8688

87-
s.logger.Info("Parsed last seen timestamps", zap.Int("count", len(timestamps)))
89+
s.logger.Debug("Parsed last seen timestamps", zap.Int("count", len(timestamps)))
8890

8991
if err := s.devicesSvc.SetLastSeen(ctx, timestamps); err != nil {
9092
return fmt.Errorf("can't set last seen: %w", err)

0 commit comments

Comments
 (0)