Skip to content

Commit

Permalink
EdgeHub: Improve startup time (#291)
Browse files Browse the repository at this point in the history
* Parallelize EdgeHub initialization

* Change default Operation timeout to 20 secs
  • Loading branch information
varunpuranik committed Sep 14, 2018
1 parent e372fa5 commit 3ac39ac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CloudConnection : ICloudConnection
static readonly TimeSpan TokenExpiryBuffer = TimeSpan.FromMinutes(5); // Token is usable if it does not expire in 5 mins
const int TokenTimeToLiveSeconds = 3600; // Unused - Token is generated by downstream clients
const int TokenExpiryBufferPercentage = 8; // Assuming a standard token for 1 hr, we set expiry time to around 5 mins.
const uint OperationTimeoutMilliseconds = 1 * 60 * 1000; // 1 min
const uint OperationTimeoutMilliseconds = 20 * 1000; // 20 secs
static readonly TimeSpan TokenRetryWaitTime = TimeSpan.FromSeconds(20);

readonly Action<string, CloudConnectionStatus> connectionStatusChangedHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,32 @@ IDeviceScopeIdentitiesCache deviceScopeIdentitiesCache
Preconditions.CheckNotNull(deviceScopeIdentitiesCache, nameof(deviceScopeIdentitiesCache));

var edgeHubConnection = new EdgeHubConnection(
edgeHubCredentials.Identity as IModuleIdentity, twinManager, routeFactory,
twinCollectionMessageConverter, twinMessageConverter,
edgeHubCredentials.Identity as IModuleIdentity,
twinManager,
routeFactory,
twinCollectionMessageConverter,
twinMessageConverter,
versionInfo ?? VersionInfo.Empty,
deviceScopeIdentitiesCache
);

IDeviceProxy deviceProxy = new EdgeHubDeviceProxy(edgeHubConnection);
await connectionManager.AddDeviceConnection(edgeHubCredentials.Identity, deviceProxy);

await edgeHub.AddSubscription(edgeHubCredentials.Identity.Id, DeviceSubscription.DesiredPropertyUpdates);
await edgeHub.AddSubscription(edgeHubCredentials.Identity.Id, DeviceSubscription.Methods);

// Clear out all the reported devices.
await edgeHubConnection.ClearDeviceConnectionStatuses();

await InitEdgeHub(edgeHubConnection, connectionManager, edgeHubCredentials, edgeHub);
connectionManager.DeviceConnected += edgeHubConnection.DeviceConnected;
connectionManager.DeviceDisconnected += edgeHubConnection.DeviceDisconnected;
Events.Initialized(edgeHubCredentials.Identity);
return edgeHubConnection;
}

static Task InitEdgeHub(EdgeHubConnection edgeHubConnection, IConnectionManager connectionManager, IClientCredentials edgeHubCredentials, IEdgeHub edgeHub)
{
IDeviceProxy deviceProxy = new EdgeHubDeviceProxy(edgeHubConnection);
Task addDeviceConnectionTask = connectionManager.AddDeviceConnection(edgeHubCredentials.Identity, deviceProxy);
Task desiredPropertyUpdatesSubscriptionTask = edgeHub.AddSubscription(edgeHubCredentials.Identity.Id, DeviceSubscription.DesiredPropertyUpdates);
Task methodsSubscriptionTask = edgeHub.AddSubscription(edgeHubCredentials.Identity.Id, DeviceSubscription.Methods);
Task clearDeviceConnectionStatusesTask = edgeHubConnection.ClearDeviceConnectionStatuses();
return Task.WhenAll(addDeviceConnectionTask, desiredPropertyUpdatesSubscriptionTask, methodsSubscriptionTask, clearDeviceConnectionStatusesTask);
}

public async Task<Option<EdgeHubConfig>> GetConfig()
{
using (await this.edgeHubConfigLock.LockAsync())
Expand Down Expand Up @@ -359,7 +364,7 @@ internal async Task<DirectMethodResponse> HandleMethodInvocation(DirectMethodReq
{
Events.ErrorRefreshingServiceIdentities(e);
return new DirectMethodResponse(e, HttpStatusCode.InternalServerError);
}
}
}
else
{
Expand Down

0 comments on commit 3ac39ac

Please sign in to comment.