|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Net.Http.Json; |
| 3 | +using System.Text.Json; |
| 4 | + |
| 5 | +namespace Sentinel.SDK.Core; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// ChainClient partial — LCD GET with failover, broadcast via failover, and endpoint health checks. |
| 9 | +/// </summary> |
| 10 | +public sealed partial class ChainClient |
| 11 | +{ |
| 12 | + // ─── Internal: LCD GET with Failover ─── |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Execute an LCD GET request with timeout, retry, and endpoint failover. |
| 16 | + /// </summary> |
| 17 | + private async Task<JsonElement> LcdGetAsync(string path, CancellationToken ct = default) |
| 18 | + { |
| 19 | + Exception? lastException = null; |
| 20 | + |
| 21 | + foreach (var baseUrl in _lcdUrls) |
| 22 | + { |
| 23 | + try |
| 24 | + { |
| 25 | + var url = baseUrl.TrimEnd('/') + path; |
| 26 | + |
| 27 | + using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct); |
| 28 | + cts.CancelAfter(_timeout); |
| 29 | + var response = await _publicHttpClient.GetAsync(url, cts.Token); |
| 30 | + |
| 31 | + if (response.StatusCode == System.Net.HttpStatusCode.NotFound) |
| 32 | + { |
| 33 | + throw new SentinelException("CLIENT_HTTP_404", |
| 34 | + $"Resource not found: {path}"); |
| 35 | + } |
| 36 | + |
| 37 | + response.EnsureSuccessStatusCode(); |
| 38 | + |
| 39 | + var body = await response.Content.ReadAsStringAsync(cts.Token); |
| 40 | + using var doc = JsonDocument.Parse(body); |
| 41 | + return doc.RootElement.Clone(); |
| 42 | + } |
| 43 | + catch (SentinelException) |
| 44 | + { |
| 45 | + throw; // Don't retry on known errors like 404 |
| 46 | + } |
| 47 | + catch (OperationCanceledException) when (ct.IsCancellationRequested) |
| 48 | + { |
| 49 | + throw; // Caller cancelled — propagate immediately |
| 50 | + } |
| 51 | + catch (Exception ex) when (ex is HttpRequestException or TaskCanceledException or OperationCanceledException) |
| 52 | + { |
| 53 | + lastException = ex; |
| 54 | + // Try next endpoint |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + throw new SentinelException("CLIENT_ALL_ENDPOINTS_FAILED", |
| 59 | + $"All LCD endpoints failed for {path}: {lastException?.Message}", lastException!); |
| 60 | + } |
| 61 | + |
| 62 | + // ─── Health ─── |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Check the health of all configured LCD endpoints by measuring response latency. |
| 66 | + /// Each endpoint is probed with a lightweight balance query. |
| 67 | + /// </summary> |
| 68 | + /// <param name="timeoutMs">Per-endpoint timeout in milliseconds (default: 5000).</param> |
| 69 | + /// <param name="ct">Cancellation token.</param> |
| 70 | + /// <returns>Health results for each configured LCD endpoint.</returns> |
| 71 | + public async Task<IReadOnlyList<EndpointHealth>> CheckEndpointHealthAsync( |
| 72 | + int timeoutMs = 5000, |
| 73 | + CancellationToken ct = default) |
| 74 | + { |
| 75 | + var results = new List<EndpointHealth>(); |
| 76 | + |
| 77 | + foreach (var baseUrl in _lcdUrls) |
| 78 | + { |
| 79 | + ct.ThrowIfCancellationRequested(); |
| 80 | + |
| 81 | + var name = "unknown"; |
| 82 | + try |
| 83 | + { |
| 84 | + var uri = new Uri(baseUrl); |
| 85 | + name = uri.Host; |
| 86 | + } |
| 87 | + catch |
| 88 | + { |
| 89 | + name = baseUrl; |
| 90 | + } |
| 91 | + |
| 92 | + try |
| 93 | + { |
| 94 | + var url = baseUrl.TrimEnd('/') + "/cosmos/base/tendermint/v1beta1/syncing"; |
| 95 | + using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct); |
| 96 | + cts.CancelAfter(TimeSpan.FromMilliseconds(timeoutMs)); |
| 97 | + |
| 98 | + var sw = Stopwatch.StartNew(); |
| 99 | + var response = await _publicHttpClient.GetAsync(url, cts.Token); |
| 100 | + sw.Stop(); |
| 101 | + |
| 102 | + response.EnsureSuccessStatusCode(); |
| 103 | + |
| 104 | + results.Add(new EndpointHealth(baseUrl, name, (int)sw.ElapsedMilliseconds)); |
| 105 | + } |
| 106 | + catch |
| 107 | + { |
| 108 | + results.Add(new EndpointHealth(baseUrl, name, null)); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return results; |
| 113 | + } |
| 114 | +} |
0 commit comments