Skip to content

Commit

Permalink
* Use a slightly shorter interval with its own define for deviceBecam…
Browse files Browse the repository at this point in the history
…eActive

  throttling.
* Log when we start throttling deviceBecameActive callbacks.
  • Loading branch information
bzbarsky-apple committed Feb 6, 2025
1 parent b80388a commit ce5b841
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/darwin/Framework/CHIP/MTRDevice_Concrete.mm
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ @interface MTRDevice_Concrete ()
@property (nonatomic) NSDate * estimatedStartTimeFromGeneralDiagnosticsUpTime;

@property (nonatomic) NSDate * lastDeviceBecameActiveCallbackTime;
@property (nonatomic) BOOL throttlingDeviceBecameActiveCallbacks;

/**
* If currentReadClient is non-null, that means that we successfully
Expand Down Expand Up @@ -472,6 +473,7 @@ - (instancetype)initWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceControlle
_persistedClusters = [NSMutableSet set];
_highestObservedEventNumber = nil;
_matterCPPObjectsHolder = [[MTRDeviceMatterCPPObjectsHolder alloc] init];
_throttlingDeviceBecameActiveCallbacks = NO;

// If there is a data store, make sure we have an observer to monitor system clock changes, so
// NSDate-based write coalescing could be reset and not get into a bad state.
Expand Down Expand Up @@ -866,6 +868,7 @@ - (void)_setDSTOffsets:(NSArray<MTRTimeSynchronizationClusterDSTOffsetStruct *>
// subscription intervals are in seconds
#define MTR_DEVICE_SUBSCRIPTION_MAX_INTERVAL_MIN (10 * 60) // 10 minutes (for now)
#define MTR_DEVICE_SUBSCRIPTION_MAX_INTERVAL_MAX (60 * 60) // 60 minutes
#define MTR_DEVICE_MIN_SECONDS_BETWEEN_DEVICE_BECAME_ACTIVE_CALLBACKS (1 * 60) // 1 minute (for now)

- (BOOL)_subscriptionsAllowed
{
Expand Down Expand Up @@ -1640,7 +1643,7 @@ - (void)_handleUnsolicitedMessageFromPublisher
BOOL shouldCallDelegate = NO;
if (self.lastDeviceBecameActiveCallbackTime) {
NSTimeInterval intervalSinceLastCallback = -[self.lastDeviceBecameActiveCallbackTime timeIntervalSinceNow];
if (intervalSinceLastCallback > MTR_DEVICE_SUBSCRIPTION_MAX_INTERVAL_MIN) {
if (intervalSinceLastCallback > MTR_DEVICE_MIN_SECONDS_BETWEEN_DEVICE_BECAME_ACTIVE_CALLBACKS) {
shouldCallDelegate = YES;
}
} else {
Expand All @@ -1654,6 +1657,10 @@ - (void)_handleUnsolicitedMessageFromPublisher
}
}];
self.lastDeviceBecameActiveCallbackTime = [NSDate now];
self.throttlingDeviceBecameActiveCallbacks = NO;
} else if (!self.throttlingDeviceBecameActiveCallbacks) {
MTR_LOG("%@ throttling deviceBecameActive callbacks because report came in too soon after %@", self, self.lastDeviceBecameActiveCallbackTime);
self.throttlingDeviceBecameActiveCallbacks = YES;
}

// in case this is called during exponential back off of subscription
Expand Down

0 comments on commit ce5b841

Please sign in to comment.