-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AppInsights] delaying QuickPulse initialization to help with cold start
- Loading branch information
Showing
4 changed files
with
91 additions
and
23 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
48 changes: 48 additions & 0 deletions
48
src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/QuickPulseInitializationScheduler.cs
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,48 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.WebJobs.Logging.ApplicationInsights | ||
{ | ||
// We need to be able to delay initialization in Functions for cold start, | ||
// so perform this initialization in a service that can handle cancellation. | ||
internal class QuickPulseInitializationScheduler : IDisposable | ||
{ | ||
private readonly CancellationTokenSource _cts = new CancellationTokenSource(); | ||
private bool _disposed = false; | ||
private object _lock = new object(); | ||
|
||
public void ScheduleInitialization(Action initialization, TimeSpan delay) | ||
{ | ||
Task.Delay(delay, _cts.Token) | ||
.ContinueWith(_ => | ||
{ | ||
lock (_lock) | ||
{ | ||
if (!_disposed) | ||
{ | ||
initialization(); | ||
} | ||
} | ||
}, | ||
TaskContinuationOptions.OnlyOnRanToCompletion); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
lock (_lock) | ||
{ | ||
if (!_disposed) | ||
{ | ||
_cts.Cancel(); | ||
_cts.Dispose(); | ||
|
||
_disposed = true; | ||
} | ||
} | ||
} | ||
} | ||
} |
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