DeviceLoaderSynchronizer GetOrCreateLoadingDevicesRequestQueue(string devAddr) { // Need to get and ensure it has started since the GetOrAdd can create multiple objects // https://github.com/aspnet/Extensions/issues/708 lock (this.getOrCreateLoadingDevicesRequestQueueLock) { return(this.cache.GetOrCreate <DeviceLoaderSynchronizer>( $"devaddrloader:{devAddr}", (ce) => { var cts = new CancellationTokenSource(); ce.ExpirationTokens.Add(new CancellationChangeToken(cts.Token)); var destinationDictionary = this.InternalGetCachedDevicesForDevAddr(devAddr); var originalDeviceCount = destinationDictionary.Count; var loader = new DeviceLoaderSynchronizer( devAddr, this.loRaDeviceAPIService, this.deviceFactory, destinationDictionary, this.initializers, this.configuration, (t, l) => { // If the operation to load was successfull // wait for 30 seconds for pending requests to go through and avoid additional calls if (t.IsCompletedSuccessfully && !l.HasLoadingDeviceError && this.DevAddrReloadInterval > TimeSpan.Zero) { // remove from cache after 30 seconds cts.CancelAfter(this.DevAddrReloadInterval); } else { // remove from cache now cts.Cancel(); } }, (l) => this.UpdateDeviceRegistration(l)); return loader; })); } }
private DeviceLoaderSynchronizer GetOrCreateLoadingDevicesRequestQueue(DevAddr devAddr) { // Need to get and ensure it has started since the GetOrAdd can create multiple objects // https://github.com/aspnet/Extensions/issues/708 lock (this.getOrCreateLoadingDevicesRequestQueueLock) { return(this.cache.GetOrCreate( GetDevLoaderCacheKey(devAddr), (ce) => { var cts = new CancellationTokenSource(); ce.ExpirationTokens.Add(new CancellationChangeToken(cts.Token)); var loader = new DeviceLoaderSynchronizer( devAddr, this.loRaDeviceAPIService, this.deviceFactory, this.configuration, this.deviceCache, this.initializers, this.loggerFactory.CreateLogger <DeviceLoaderSynchronizer>()); _ = loader.LoadAsync().ContinueWith((t) => { // If the operation to load was successfull // wait for 30 seconds for pending requests to go through and avoid additional calls if (t.IsCompletedSuccessfully && !loader.HasLoadingDeviceError && DevAddrReloadInterval > TimeSpan.Zero) { // remove from cache after 30 seconds cts.CancelAfter(DevAddrReloadInterval); } else { // remove from cache now cts.Cancel(); } }, TaskScheduler.Default); return loader; })); } }