-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TestProxy] Add auto-shutdown to the proxy (#9540)
* if `auto-shutdown-in-seconds X` is provided to the StartInvocation, enable the proxy to shutdown after X seconds of no traffic over its routes
- Loading branch information
Showing
6 changed files
with
106 additions
and
3 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
8 changes: 8 additions & 0 deletions
8
tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/AutoShutdown/ShutdownConfiguration.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,8 @@ | ||
namespace Azure.Sdk.Tools.TestProxy.Common.AutoShutdown | ||
{ | ||
public class ShutdownConfiguration | ||
{ | ||
public bool EnableAutoShutdown { get; set; } = false; | ||
public int TimeoutInSeconds { get; set; } = 300; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/AutoShutdown/ShutdownTimer.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,42 @@ | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using System; | ||
|
||
namespace Azure.Sdk.Tools.TestProxy.Common.AutoShutdown | ||
{ | ||
public class ShutdownTimer | ||
{ | ||
private readonly ShutdownConfiguration _shutdownConfig; | ||
private CancellationTokenSource _cts; | ||
private Task _shutdownTask; | ||
|
||
public ShutdownTimer(ShutdownConfiguration shutdownConfiguration) | ||
{ | ||
_shutdownConfig = shutdownConfiguration; | ||
} | ||
|
||
public void ResetTimer() | ||
{ | ||
_cts?.Cancel(); | ||
_cts = new CancellationTokenSource(); | ||
|
||
_shutdownTask = Task.Run(async () => | ||
{ | ||
try | ||
{ | ||
await Task.Delay(_shutdownConfig.TimeoutInSeconds * 1000, _cts.Token); | ||
|
||
if (_shutdownConfig.EnableAutoShutdown) | ||
{ | ||
System.Console.WriteLine("Server idle timeout reached. Shutting down..."); | ||
Environment.Exit(0); | ||
} | ||
} | ||
catch (TaskCanceledException) | ||
{ | ||
// Timer was reset or canceled | ||
} | ||
}); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/AutoShutdown/ShutdownTimerMiddleware.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,28 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using System.Threading.Tasks; | ||
|
||
namespace Azure.Sdk.Tools.TestProxy.Common.AutoShutdown | ||
{ | ||
public class ShutdownTimerMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly ShutdownTimer _shutdownTimer; | ||
private readonly ShutdownConfiguration _shutdownConfig; | ||
|
||
public ShutdownTimerMiddleware(RequestDelegate next, ShutdownTimer shutdownTimer, ShutdownConfiguration shutdownConfig) | ||
{ | ||
_next = next; | ||
_shutdownTimer = shutdownTimer; | ||
_shutdownConfig = shutdownConfig; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
if (_shutdownConfig.EnableAutoShutdown) | ||
{ | ||
_shutdownTimer.ResetTimer(); | ||
} | ||
await _next(context); | ||
} | ||
} | ||
} |
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