| 1 |
using System.Net.Sockets; |
| 2 |
using BeamSharp.Node; |
| 3 |
using Blog.Models; |
| 4 |
using Microsoft.Extensions.Caching.Hybrid; |
| 5 |
using Microsoft.Extensions.Options; |
| 6 |
|
| 7 |
namespace Blog.Services; |
| 8 |
|
| 9 |
|
| 10 |
public sealed class RvrbOptions |
| 11 |
{ |
| 12 |
public const string Section = "Rvrb"; |
| 13 |
|
| 14 |
|
| 15 |
public string Node { get; set; } = "rvrb@127.0.0.1"; |
| 16 |
|
| 17 |
|
| 18 |
public string LocalNode { get; set; } = "blog@127.0.0.1"; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public string? Cookie { get; set; } |
| 26 |
|
| 27 |
|
| 28 |
public TimeSpan CallTimeout { get; set; } = TimeSpan.FromSeconds(5); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public TimeSpan CacheFor { get; set; } = TimeSpan.FromSeconds(15); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public sealed class RvrbService( |
| 52 |
HybridCache cache, |
| 53 |
IOptions<RvrbOptions> options, |
| 54 |
ILogger<RvrbService> logger) : IAsyncDisposable |
| 55 |
{ |
| 56 |
private readonly RvrbOptions _options = options.Value; |
| 57 |
private readonly SemaphoreSlim _startGate = new(1, 1); |
| 58 |
private ErlangNode? _node; |
| 59 |
|
| 60 |
public async ValueTask<RvrbStatus> GetStatusAsync(CancellationToken cancellationToken = default) |
| 61 |
{ |
| 62 |
return await cache.GetOrCreateAsync( |
| 63 |
"rvrb/snapshot", |
| 64 |
this, |
| 65 |
static (service, cancel) => service.FetchAsync(cancel), |
| 66 |
new HybridCacheEntryOptions |
| 67 |
{ |
| 68 |
Expiration = _options.CacheFor, |
| 69 |
LocalCacheExpiration = _options.CacheFor |
| 70 |
}, |
| 71 |
cancellationToken: cancellationToken) |
| 72 |
.ConfigureAwait(false); |
| 73 |
} |
| 74 |
|
| 75 |
private async ValueTask<RvrbStatus> FetchAsync(CancellationToken cancellationToken) |
| 76 |
{ |
| 77 |
try |
| 78 |
{ |
| 79 |
var node = await StartedNodeAsync(cancellationToken).ConfigureAwait(false); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
var reply = await node |
| 84 |
.RpcAsync(_options.Node, "Elixir.Rvrb.Stats", "snapshot", [], _options.CallTimeout, |
| 85 |
cancellationToken) |
| 86 |
.ConfigureAwait(false); |
| 87 |
|
| 88 |
return RvrbStatus.Reachable(RvrbSnapshotReader.Read(reply)); |
| 89 |
} |
| 90 |
catch (Exception ex) when (ex is IOException or SocketException or TimeoutException |
| 91 |
or ErlangRpcException or ErlangExitException |
| 92 |
or InvalidOperationException or FormatException) |
| 93 |
{ |
| 94 |
logger.LogWarning(ex, "could not read stats from {Node}", _options.Node); |
| 95 |
return RvrbStatus.Unreachable(Describe(ex)); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
private async ValueTask<ErlangNode> StartedNodeAsync(CancellationToken cancellationToken) |
| 104 |
{ |
| 105 |
if (_node is { } running) return running; |
| 106 |
|
| 107 |
await _startGate.WaitAsync(cancellationToken).ConfigureAwait(false); |
| 108 |
try |
| 109 |
{ |
| 110 |
if (_node is { } started) return started; |
| 111 |
|
| 112 |
var node = new ErlangNode(_options.LocalNode, new ErlangNodeOptions |
| 113 |
{ |
| 114 |
Cookie = _options.Cookie, |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
BindAddress = "127.0.0.1", |
| 119 |
EpmdHost = "127.0.0.1", |
| 120 |
Log = line => logger.LogDebug("beamsharp: {Message}", line) |
| 121 |
}); |
| 122 |
|
| 123 |
try |
| 124 |
{ |
| 125 |
await node.StartAsync(cancellationToken).ConfigureAwait(false); |
| 126 |
} |
| 127 |
catch |
| 128 |
{ |
| 129 |
|
| 130 |
|
| 131 |
await node.DisposeAsync().ConfigureAwait(false); |
| 132 |
throw; |
| 133 |
} |
| 134 |
|
| 135 |
_node = node; |
| 136 |
return node; |
| 137 |
} |
| 138 |
finally |
| 139 |
{ |
| 140 |
_startGate.Release(); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
private static string Describe(Exception ex) => |
| 147 |
ex.InnerException is { } inner ? $"{ex.Message}: {inner.Message}" : ex.Message; |
| 148 |
|
| 149 |
public async ValueTask DisposeAsync() |
| 150 |
{ |
| 151 |
if (_node is { } node) await node.DisposeAsync().ConfigureAwait(false); |
| 152 |
_startGate.Dispose(); |
| 153 |
} |
| 154 |
} |