-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve RpcPeer connection handling, + RpcLimits, add WebSocket…
…Channel.MaxItemSize, improve RpcPeerStateMonitor, etc.
- Loading branch information
1 parent
f89eb3b
commit c153b97
Showing
21 changed files
with
402 additions
and
141 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
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,32 @@ | ||
@using Stl.Rpc | ||
@inherits StatefulComponentBase<IState<RpcPeerComputedState>> | ||
|
||
@{ | ||
var m = State.Value; | ||
var isConnected = m.IsOrLikelyConnected; | ||
} | ||
|
||
<Div Margin="Margin.Is1.OnY" TextColor="@(isConnected ? TextColor.Default : TextColor.Warning)"> | ||
<span>Connection state: </span> | ||
<strong> | ||
<span>@(m.GetActivityDescription(true))</span> | ||
@if (m.ReconnectsIn is { } reconnectsIn) { | ||
<span> Will reconnect <TimerBadge ExpiresAt="@(Clock.Now + reconnectsIn)"/>. </span> | ||
<Anchor TextColor="@TextColor.Success" Clicked="@Reconnect">Reconnect</Anchor> | ||
} | ||
</strong> | ||
</Div> | ||
|
||
@code { | ||
[Inject] private RpcPeerStateMonitor Monitor { get; init; } = null!; | ||
[Inject] private IMomentClock Clock { get; init; } = null!; | ||
|
||
[Parameter] | ||
public string CssClass { get; set; } = ""; | ||
|
||
protected override IState<RpcPeerComputedState> CreateState() | ||
=> Monitor.ComputedState; | ||
|
||
private void Reconnect() | ||
=> Services.RpcHub().InternalServices.ClientPeerReconnectDelayer.CancelDelays(); | ||
} |
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
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,41 @@ | ||
namespace Stl.Fusion.Extensions; | ||
|
||
public enum RpcPeerComputedStateKind | ||
{ | ||
Connected = 0, | ||
JustDisconnected, | ||
Disconnected, | ||
Reconnecting, | ||
} | ||
|
||
public sealed record RpcPeerComputedState( | ||
RpcPeerComputedStateKind Kind, | ||
Exception? LastError = null, | ||
TimeSpan? ReconnectsIn = null) | ||
{ | ||
public bool IsConnected => Kind == RpcPeerComputedStateKind.Connected; | ||
public bool IsOrLikelyConnected => | ||
Kind == RpcPeerComputedStateKind.Connected | ||
|| Kind == RpcPeerComputedStateKind.JustDisconnected; | ||
|
||
public string GetActivityDescription(bool useLastError = false) | ||
{ | ||
switch (Kind) { | ||
case RpcPeerComputedStateKind.Connected: | ||
return "Connected."; | ||
case RpcPeerComputedStateKind.JustDisconnected: | ||
return "Connected, checking..."; | ||
case RpcPeerComputedStateKind.Reconnecting: | ||
return "Reconnecting..."; | ||
} | ||
if (LastError == null || !useLastError) | ||
return "Disconnected."; | ||
|
||
var message = LastError.Message.Trim(); | ||
if (!(message.EndsWith(".", StringComparison.Ordinal) | ||
|| message.EndsWith("!", StringComparison.Ordinal) | ||
|| message.EndsWith("?", StringComparison.Ordinal))) | ||
message += "."; | ||
return "Disconnected: " + message; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,42 @@ | ||
using Stl.Rpc.Infrastructure; | ||
|
||
namespace Stl.Fusion.Extensions; | ||
|
||
public record RpcPeerState( | ||
bool IsConnected, | ||
Exception? Error = null, | ||
Moment ReconnectsAt = default); // Relative to CpuClock.Now | ||
// Any Moment below is derived with RpcHub.Clock, which is CpuClock | ||
|
||
public abstract record RpcPeerState | ||
{ | ||
public abstract Moment EnteredAt { get; } | ||
|
||
public RpcPeerConnectedState ToConnected(Moment now) | ||
=> this as RpcPeerConnectedState ?? new RpcPeerConnectedState(now); | ||
|
||
public RpcPeerDisconnectedState ToDisconnected(Moment now, Moment reconnectsAt, RpcPeerConnectionState state) | ||
=> ToDisconnected(now, reconnectsAt, state.Error); | ||
public RpcPeerDisconnectedState ToDisconnected(Moment now, Moment reconnectsAt, Exception? lastError) | ||
{ | ||
if (this is not RpcPeerDisconnectedState d) | ||
return new RpcPeerDisconnectedState(now, reconnectsAt, lastError); | ||
|
||
if (reconnectsAt == d.ReconnectsAt && d.LastError == lastError) | ||
return d; | ||
|
||
return new RpcPeerDisconnectedState(d.DisconnectedAt, reconnectsAt, lastError ?? d.LastError); | ||
} | ||
} | ||
|
||
public sealed record RpcPeerConnectedState( | ||
Moment ConnectedAt | ||
) : RpcPeerState | ||
{ | ||
public override Moment EnteredAt => ConnectedAt; | ||
} | ||
|
||
public sealed record RpcPeerDisconnectedState( | ||
Moment DisconnectedAt, | ||
Moment ReconnectsAt, // < Now = tries to reconnect now | ||
Exception? LastError | ||
) : RpcPeerState | ||
{ | ||
public override Moment EnteredAt => DisconnectedAt; | ||
} |
Oops, something went wrong.