-
Notifications
You must be signed in to change notification settings - Fork 10
RR1-T102 Fixes and hardening and systemd support for cli #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,139 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using NUnit.Framework; | ||
| using Resgrid.Relay.Engine; | ||
| using Resgrid.Relay.Engine.Configuration; | ||
| using Resgrid.Relay.Engine.Services; | ||
|
|
||
| namespace Resgrid.Audio.Voice.Tests | ||
| { | ||
| /// <summary> | ||
| /// Behavioral coverage for the LiveKit-mode resilience loop in | ||
| /// <see cref="RelayServiceBase"/>: exponential back-off retries, the consecutive-failure | ||
| /// circuit breaker, and graceful stop while retrying. Uses a tiny test subclass with a | ||
| /// scripted <c>ExecuteAsync</c> and near-zero back-off so the tests run fast. | ||
| /// </summary> | ||
| [TestFixture] | ||
| public class RelayResilienceTests | ||
| { | ||
| private static RelayHostOptions OptionsWith(ResilienceOptions resilience) | ||
| { | ||
| return new RelayHostOptions | ||
| { | ||
| Resilience = resilience, | ||
| // No Sentry DSN ⇒ telemetry is the no-op singleton (no SDK init). | ||
| Telemetry = new RelayTelemetryOptions() | ||
| }; | ||
| } | ||
|
|
||
| private static ResilienceOptions FastResilience(int maxFailures, double healthySeconds = 1000) => new ResilienceOptions | ||
| { | ||
| Enabled = true, | ||
| MaxConsecutiveFailures = maxFailures, | ||
| InitialBackoffSeconds = 0.01, | ||
| MaxBackoffSeconds = 0.05, | ||
| HealthyRunSeconds = healthySeconds | ||
| }; | ||
|
|
||
| [Test] | ||
| public void ComputeBackoff_GrowsExponentially_AndIsCappedWithJitter() | ||
| { | ||
| var r = new ResilienceOptions | ||
| { | ||
| InitialBackoffSeconds = 2, | ||
| MaxBackoffSeconds = 60, | ||
| MaxConsecutiveFailures = 10, | ||
| HealthyRunSeconds = 30 | ||
| }; | ||
|
|
||
| // failure 1 ≈ 2s ±20% ⇒ [1.6, 2.4]; failure 3 ≈ 8s ±20% ⇒ [6.4, 9.6]; | ||
| // failure 10 caps at 60s ±20% ⇒ [48, 72]; floor is always >= 0.5s. | ||
| RelayServiceBase.ComputeBackoff(r, 1).TotalSeconds.Should().BeInRange(1.6, 2.4); | ||
| RelayServiceBase.ComputeBackoff(r, 3).TotalSeconds.Should().BeInRange(6.4, 9.6); | ||
| RelayServiceBase.ComputeBackoff(r, 10).TotalSeconds.Should().BeInRange(48, 72); | ||
| RelayServiceBase.ComputeBackoff(r, 1).TotalSeconds.Should().BeGreaterThanOrEqualTo(0.5); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task AlwaysFailing_LiveKitMode_TripsBreaker_AndFaults() | ||
| { | ||
| var svc = new ScriptedRelayService(OptionsWith(FastResilience(maxFailures: 3))); | ||
|
|
||
| // ExecuteAsync throws immediately every time; the breaker opens on the 3rd failure. | ||
| svc.ExecuteBehavior = (_, __) => throw new InvalidOperationException("boom"); | ||
|
|
||
| await svc.StartAsync(CancellationToken.None); | ||
|
|
||
| svc.State.Should().Be(RelayServiceState.Faulted); | ||
| svc.Attempts.Should().Be(3, "the breaker opens once MaxConsecutiveFailures quick failures pile up"); | ||
| svc.Status.LiveKit.Should().Be(ConnectionState.Degraded); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task TransientFailures_ThenGracefulStop_EndsStopped_NotFaulted() | ||
| { | ||
| var svc = new ScriptedRelayService(OptionsWith(FastResilience(maxFailures: 5))); | ||
|
|
||
| // First two attempts fail (transient); the third blocks until cancelled, then | ||
| // returns by honoring the token ⇒ graceful Stopped, never reaching the breaker. | ||
| svc.ExecuteBehavior = async (s, token) => | ||
| { | ||
| if (s.Attempts < 3) | ||
| throw new InvalidOperationException("transient"); | ||
| await Task.Delay(Timeout.Infinite, token).ConfigureAwait(false); | ||
| }; | ||
|
|
||
| var run = svc.StartAsync(CancellationToken.None); | ||
|
|
||
| // Wait until the run is parked in the long-lived (3rd) attempt, then stop it. | ||
| var spun = SpinWait.SpinUntil(() => svc.Attempts >= 3 && svc.State == RelayServiceState.Running, TimeSpan.FromSeconds(5)); | ||
| spun.Should().BeTrue("the service should reach its healthy long-lived run after the transient failures"); | ||
|
|
||
| await svc.StopAsync(); | ||
| await run; | ||
|
|
||
| svc.State.Should().Be(RelayServiceState.Stopped); | ||
| svc.Attempts.Should().Be(3); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task ResilienceDisabled_RunsExecuteOnce_AndPropagatesFault() | ||
| { | ||
| var resilience = FastResilience(maxFailures: 3); | ||
| resilience.Enabled = false; | ||
| var svc = new ScriptedRelayService(OptionsWith(resilience)); | ||
|
|
||
| svc.ExecuteBehavior = (_, __) => throw new InvalidOperationException("boom"); | ||
|
|
||
| await svc.StartAsync(CancellationToken.None); | ||
|
|
||
| svc.State.Should().Be(RelayServiceState.Faulted); | ||
| svc.Attempts.Should().Be(1, "with resilience disabled ExecuteAsync runs exactly once"); | ||
| } | ||
|
|
||
| /// <summary>Minimal LiveKit-mode service whose run is scripted by the test.</summary> | ||
| private sealed class ScriptedRelayService : RelayServiceBase | ||
| { | ||
| private int _attempts; | ||
|
|
||
| public ScriptedRelayService(RelayHostOptions options) | ||
| : base("test", options, null) | ||
| { | ||
| } | ||
|
|
||
| protected override bool IsLiveKitMode => true; | ||
|
|
||
| public int Attempts => Volatile.Read(ref _attempts); | ||
|
|
||
| public Func<ScriptedRelayService, CancellationToken, Task> ExecuteBehavior { get; set; } | ||
|
|
||
| protected override async Task ExecuteAsync(CancellationToken token) | ||
| { | ||
| Interlocked.Increment(ref _attempts); | ||
| await ExecuteBehavior(this, token).ConfigureAwait(false); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,4 @@ | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| // Exposes engine internals (e.g. RelayServiceBase.ComputeBackoff) to the resilience tests. | ||
| [assembly: InternalsVisibleTo("Resgrid.Audio.Voice.Tests")] |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Resgrid.Relay.Engine.Telemetry | ||
| { | ||
| /// <summary> | ||
| /// Lightweight observability hooks for the long-lived LiveKit voice modes | ||
| /// (radio / record / dispatch). Implementations forward lifecycle and | ||
| /// fault/retry events to a backend (e.g. Sentry) — or do nothing when | ||
| /// monitoring is disabled. | ||
| /// </summary> | ||
| public interface IRelayModeTelemetry : IAsyncDisposable | ||
| { | ||
| void ModeStarting(string mode); | ||
| void ModeRetrying(string mode, Exception ex, int attempt, TimeSpan nextDelay); | ||
| void ModeFaulted(string mode, Exception ex); | ||
| void ModeStopped(string mode); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PlatformNotSupportedExceptionthrown at startup on Windows becausePosixSignalRegistration.Create(PosixSignal.SIGTERM)runs without a platform guard and sits outside thetryblock inMain, causing an unhandled exception since the Engine targetsnet10.0-windows. Guard the registration withOperatingSystem.IsLinux()orOperatingSystem.IsMacOS()so the SIGTERM handler only registers on supported platforms.Prompt for LLM
Talk to Kody by mentioning @kody
Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.