Skip to content

Commit

Permalink
fetch config from backend on each app open
Browse files Browse the repository at this point in the history
  • Loading branch information
dhawal1248 committed Nov 2, 2020
1 parent 5134b37 commit ba9a44a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
6 changes: 3 additions & 3 deletions Rudder/Classes/RSEventRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ - (instancetype)init : (NSString*) _writeKey config:(RSConfig*) _config {
dbpersistenceManager = [[RSDBPersistentManager alloc] init];

[RSLogger logDebug:@"EventRepository: initiating server config manager"];
configManager = [RSServerConfigManager getInstance:writeKey rudderConfig:config];
configManager = [[RSServerConfigManager alloc] init:writeKey rudderConfig:config];

[RSLogger logDebug:@"EventRepository: initiating preferenceManager"];
self->preferenceManager = [RSPreferenceManager getInstance];
Expand Down Expand Up @@ -116,8 +116,8 @@ - (void) __initiateSDK {
[RSLogger logError:@"WRONG WRITE KEY"];
}else {
retryCount += 1;
[RSLogger logDebug:@"server config is null. retrying in 10s."];
usleep(10000000*retryCount);
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"server config is null. retrying in %ds.", retryCount]];
usleep(1000000*retryCount);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion Rudder/Classes/RSServerConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ NS_ASSUME_NONNULL_BEGIN
@property RSConfig *rudderConfig;
@property RSPreferenceManager *preferenceManager;

+ (instancetype) getInstance: (NSString*) writeKey rudderConfig:(RSConfig*) rudderConfig;
- (instancetype)init: (NSString*) writeKey rudderConfig:(RSConfig*) rudderConfig;
- (RSServerConfigSource*) getConfig;
- (int) getError;

Expand Down
60 changes: 32 additions & 28 deletions Rudder/Classes/RSServerConfigManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
#import "RSLogger.h"
#import "RSServerDestination.h"
#import "RSConstants.h"
#import <pthread.h>

static RSServerConfigManager *_instance;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static RSServerConfigSource *serverConfig;
typedef enum {
NETWORKERROR =1,
NETWORKSUCCESS =0,
Expand All @@ -23,42 +26,23 @@

@implementation RSServerConfigManager


+ (instancetype)getInstance:(NSString *)writeKey rudderConfig:(RSConfig *)rudderConfig {
if (_instance == nil) {
[RSLogger logDebug:@"Creating RSServerConfigManager instance"];
_instance = [[RSServerConfigManager alloc] init:writeKey rudderConfig:rudderConfig];
}
return _instance;
}

- (instancetype)init: (NSString*) writeKey rudderConfig:(RSConfig*) rudderConfig
{
self = [super init];
if (self) {
_preferenceManager = [RSPreferenceManager getInstance];
// TODO : is this required?
// pthread_mutex_init(&mutex, NULL);
if (writeKey == nil || [writeKey isEqualToString:@""]) {
[RSLogger logError:@"writeKey can not be null or empty"];
receivedError = WRONGWRITEKEY;
} else {
_writeKey = writeKey;
_rudderConfig = rudderConfig;
RSServerConfigSource *serverConfig = [self _retrieveConfig];
if (serverConfig == nil) {
[RSLogger logDebug:@"Server config is not present in preference storage. downloading config"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
[self _downloadConfig];
});
} else {
if([self _isServerConfigOutDated]) {
[RSLogger logDebug:@"Server config is outdated. downloading config again"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
[self _downloadConfig];
});
} else {
[RSLogger logDebug:@"Server config found. Using existing config"];
}
}
// fetchConfig and populate serverConfig
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
[self _fetchConfig];
});
}
}
return self;
Expand Down Expand Up @@ -136,6 +120,20 @@ - (RSServerConfigSource *)_parseConfig:(NSString *)configStr {
return source;
}

- (void)_fetchConfig {
// download and store config to storage
[self _downloadConfig];

// retrieve config from storage
pthread_mutex_lock(&mutex);
serverConfig = [self _retrieveConfig];
if (serverConfig == nil) {
[RSLogger logDebug:@"Server config retrieval failed.No config found in storage"];
[RSLogger logError:[[NSString alloc] initWithFormat:@"Failed to fetch server config for writeKey: %@", _writeKey]];
}
pthread_mutex_unlock(&mutex);
}

- (void)_downloadConfig {
BOOL isDone = NO;
int retryCount = 0;
Expand All @@ -154,12 +152,15 @@ - (void)_downloadConfig {
[RSLogger logInfo:@"Wrong write key"];
retryCount = 4;
}else{
[RSLogger logInfo:[[NSString alloc] initWithFormat:@"Retrying download in %d seconds", (10 * retryCount)]];
[RSLogger logInfo:[[NSString alloc] initWithFormat:@"Retrying download in %d seconds", retryCount]];
retryCount += 1;
usleep(10000000 * retryCount);
usleep(1000000 * retryCount);
}
}
}
if (!isDone) {
[RSLogger logError:@"Server config download failed.Using last stored config from storage"];
}
}

- (NSString *)_networkRequest {
Expand Down Expand Up @@ -204,7 +205,10 @@ - (NSString *)_networkRequest {
}

- (RSServerConfigSource *) getConfig {
return [self _retrieveConfig];
pthread_mutex_lock(&mutex);
RSServerConfigSource *config = serverConfig;
pthread_mutex_unlock(&mutex);
return config;
}

- (int) getError {
Expand Down

0 comments on commit ba9a44a

Please sign in to comment.