Skip to content

Commit 3e16ce7

Browse files
committed
daemon: Throttle group and outbound subscription pushes
- Limit SubscribeGroups and SubscribeOutbounds to one push per 250ms, coalescing bursts of URLTest updates
1 parent 14cca98 commit 3e16ce7

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

daemon/started_service.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636

3737
const APIVersion = 3
3838

39+
const urlTestPushMinInterval = 250 * time.Millisecond
40+
3941
var _ StartedServiceServer = (*StartedService)(nil)
4042

4143
type StartedService struct {
@@ -488,6 +490,7 @@ func (s *StartedService) SubscribeGroups(empty *emptypb.Empty, server grpc.Serve
488490
return err
489491
}
490492
defer s.serviceStatusObserver.UnSubscribe(statusSubscription)
493+
var lastSendTime time.Time
491494
for {
492495
s.serviceAccess.RLock()
493496
var groups *Groups
@@ -501,6 +504,7 @@ func (s *StartedService) SubscribeGroups(empty *emptypb.Empty, server grpc.Serve
501504
if err != nil {
502505
return err
503506
}
507+
lastSendTime = time.Now()
504508
select {
505509
case <-subscription:
506510
case <-statusSubscription:
@@ -513,6 +517,34 @@ func (s *StartedService) SubscribeGroups(empty *emptypb.Empty, server grpc.Serve
513517
case <-statusDone:
514518
return nil
515519
}
520+
throttleDelay := urlTestPushMinInterval - time.Since(lastSendTime)
521+
if throttleDelay <= 0 {
522+
continue
523+
}
524+
throttleTimer := time.NewTimer(throttleDelay)
525+
select {
526+
case <-throttleTimer.C:
527+
case <-s.ctx.Done():
528+
throttleTimer.Stop()
529+
return s.ctx.Err()
530+
case <-server.Context().Done():
531+
throttleTimer.Stop()
532+
return server.Context().Err()
533+
case <-done:
534+
throttleTimer.Stop()
535+
return nil
536+
case <-statusDone:
537+
throttleTimer.Stop()
538+
return nil
539+
}
540+
select {
541+
case <-subscription:
542+
default:
543+
}
544+
select {
545+
case <-statusSubscription:
546+
default:
547+
}
516548
}
517549
}
518550

@@ -1122,6 +1154,7 @@ func (s *StartedService) SubscribeOutbounds(_ *emptypb.Empty, server grpc.Server
11221154
return err
11231155
}
11241156
defer s.serviceStatusObserver.UnSubscribe(statusSubscription)
1157+
var lastSendTime time.Time
11251158
for {
11261159
s.serviceAccess.RLock()
11271160
boxService := s.instance
@@ -1157,6 +1190,7 @@ func (s *StartedService) SubscribeOutbounds(_ *emptypb.Empty, server grpc.Server
11571190
if err != nil {
11581191
return err
11591192
}
1193+
lastSendTime = time.Now()
11601194
select {
11611195
case <-subscription:
11621196
case <-statusSubscription:
@@ -1169,6 +1203,34 @@ func (s *StartedService) SubscribeOutbounds(_ *emptypb.Empty, server grpc.Server
11691203
case <-statusDone:
11701204
return nil
11711205
}
1206+
throttleDelay := urlTestPushMinInterval - time.Since(lastSendTime)
1207+
if throttleDelay <= 0 {
1208+
continue
1209+
}
1210+
throttleTimer := time.NewTimer(throttleDelay)
1211+
select {
1212+
case <-throttleTimer.C:
1213+
case <-s.ctx.Done():
1214+
throttleTimer.Stop()
1215+
return s.ctx.Err()
1216+
case <-server.Context().Done():
1217+
throttleTimer.Stop()
1218+
return server.Context().Err()
1219+
case <-done:
1220+
throttleTimer.Stop()
1221+
return nil
1222+
case <-statusDone:
1223+
throttleTimer.Stop()
1224+
return nil
1225+
}
1226+
select {
1227+
case <-subscription:
1228+
default:
1229+
}
1230+
select {
1231+
case <-statusSubscription:
1232+
default:
1233+
}
11721234
}
11731235
}
11741236

0 commit comments

Comments
 (0)