-
Notifications
You must be signed in to change notification settings - Fork 885
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(IoT): Fixing a potential race condition in the timer ring queue
- Loading branch information
Showing
5 changed files
with
117 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// Copyright 2010-2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// A copy of the License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0 | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
/// A circular collection containing the messages that need to be retried at a given clock tick. | ||
/// The maximum number of ticks is 60 | ||
@interface AWSMQTTTimerRing: NSObject | ||
|
||
- (void)addMsgId:(NSNumber *)msgId atTick:(NSUInteger)tick; | ||
- (void)removeMsgId:(NSNumber *)msgId atTick:(NSUInteger)tick; | ||
- (NSArray<NSNumber *> *)allMsgIdsAtTick:(NSUInteger)tick; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// Copyright 2010-2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// A copy of the License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0 | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
// | ||
|
||
#import "AWSMQTTTimerRing.h" | ||
|
||
@interface AWSMQTTTimerRing() | ||
|
||
@property (nonatomic, strong) NSLock *lock; | ||
// Array of 60, with each index being a tick, and its value a set containing the messages that need to be retried. | ||
@property (strong,atomic) NSMutableArray<NSMutableSet *>* timerRing; | ||
|
||
@end | ||
|
||
@implementation AWSMQTTTimerRing | ||
|
||
- (instancetype)init | ||
{ | ||
self = [super init]; | ||
if (self) { | ||
_lock = [[NSLock alloc] init]; | ||
_timerRing = [[NSMutableArray alloc] initWithCapacity:60]; | ||
int i; | ||
for (i = 0; i < 60; i++) { | ||
[_timerRing addObject:[NSMutableSet new]]; | ||
} | ||
} | ||
return self; | ||
} | ||
- (void)addMsgId:(NSNumber *)msgId atTick:(NSUInteger)tick { | ||
[self.lock lock]; | ||
[[self.timerRing objectAtIndex:(tick % 60)] addObject:msgId]; | ||
[self.lock unlock]; | ||
} | ||
|
||
- (void)removeMsgId:(NSNumber *)msgId atTick:(NSUInteger)tick { | ||
[self.lock lock]; | ||
[[self.timerRing objectAtIndex:(tick % 60)] removeObject:msgId]; | ||
[self.lock unlock]; | ||
} | ||
|
||
- (NSArray<NSNumber *> *)allMsgIdsAtTick:(NSUInteger)tick { | ||
[self.lock lock]; | ||
NSArray<NSNumber *> *result = [[self.timerRing objectAtIndex:(tick % 60)] allObjects]; | ||
[self.lock unlock]; | ||
return result; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters