| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Etcdserverpb; |
| | | 7 | | using Google.Protobuf; |
| | | 8 | | using Grpc.Core; |
| | | 9 | | |
| | | 10 | | namespace dotnet_etcd; |
| | | 11 | | |
| | | 12 | | public partial class EtcdClient |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Pairs watch requests with callbacks. Three shapes are accepted: one callback per request |
| | | 16 | | /// (paired by index), a single callback shared by every request, or every callback attached |
| | | 17 | | /// to a single request. The last two are what the key-based overloads produce, e.g. |
| | | 18 | | /// <c>Watch(key, methods)</c> and <c>Watch(keys, method)</c>, which used to be rejected here. |
| | | 19 | | /// </summary> |
| | | 20 | | private static Action<T>[] PairCallbacks<T>(int requestCount, Action<T>[] methods) |
| | 31 | 21 | | { |
| | 31 | 22 | | ArgumentNullException.ThrowIfNull(methods); |
| | | 23 | | |
| | 31 | 24 | | if (methods.Length == requestCount) |
| | 21 | 25 | | { |
| | 21 | 26 | | return methods; |
| | | 27 | | } |
| | | 28 | | |
| | 10 | 29 | | if (methods.Length == 1) |
| | 4 | 30 | | { |
| | 4 | 31 | | return Enumerable.Repeat(methods[0], requestCount).ToArray(); |
| | | 32 | | } |
| | | 33 | | |
| | 6 | 34 | | if (requestCount == 1) |
| | 4 | 35 | | { |
| | 4 | 36 | | Action<T>[] all = methods; |
| | 4 | 37 | | return [value => |
| | 8 | 38 | | { |
| | 56 | 39 | | foreach (Action<T> method in all) |
| | 16 | 40 | | { |
| | 16 | 41 | | method(value); |
| | 16 | 42 | | } |
| | 12 | 43 | | }]; |
| | | 44 | | } |
| | | 45 | | |
| | 2 | 46 | | throw new ArgumentException( |
| | 2 | 47 | | $"Expected one callback per request ({requestCount}), a single callback for all requests, " + |
| | 2 | 48 | | $"or all callbacks for a single request; got {methods.Length} callbacks.", nameof(methods)); |
| | 29 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Watches the specified requests and passes the watch events to the methods provided asynchronously. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="requests">Watch Requests containing keys to be watched</param> |
| | | 55 | | /// <param name="methods">Methods to which watch events should be passed on</param> |
| | | 56 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 57 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 58 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 59 | | /// <returns>A task that completes with an array of watch IDs</returns> |
| | | 60 | | public async Task<long[]> WatchAsync(WatchRequest[] requests, Action<WatchEvent[]>[] methods, |
| | | 61 | | Metadata headers = null, |
| | | 62 | | DateTime? deadline = null, |
| | | 63 | | CancellationToken cancellationToken = default) |
| | 6 | 64 | | { |
| | 6 | 65 | | methods = PairCallbacks(requests.Length, methods); |
| | | 66 | | |
| | 5 | 67 | | long[] watchIds = new long[requests.Length]; |
| | | 68 | | |
| | 26 | 69 | | for (int i = 0; i < requests.Length; i++) |
| | 8 | 70 | | { |
| | | 71 | | // Capture the loop index per iteration; otherwise the closure below would read the |
| | | 72 | | // shared loop variable (== requests.Length when an event later arrives) and throw |
| | | 73 | | // IndexOutOfRangeException, silently dropping every event. |
| | 8 | 74 | | int index = i; |
| | | 75 | | |
| | | 76 | | // Create a wrapper that converts WatchResponse to WatchEvent[] |
| | 8 | 77 | | Action<WatchResponse> wrapper = response => |
| | 13 | 78 | | { |
| | 18 | 79 | | WatchEvent[] events = response.Events.Select(e => new WatchEvent |
| | 18 | 80 | | { |
| | 18 | 81 | | Key = e.Kv.Key.ToStringUtf8(), Value = e.Kv.Value.ToStringUtf8(), Type = e.Type |
| | 18 | 82 | | }).ToArray(); |
| | 8 | 83 | | |
| | 13 | 84 | | methods[index](events); |
| | 21 | 85 | | }; |
| | | 86 | | |
| | 8 | 87 | | watchIds[i] = await _watchManager.WatchAsync(requests[i], wrapper, headers, deadline, cancellationToken) |
| | 8 | 88 | | .ConfigureAwait(false); |
| | 8 | 89 | | } |
| | | 90 | | |
| | 5 | 91 | | return watchIds; |
| | 5 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Watches the specified requests and passes the watch events of every one of them to the same method. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="requests">Watch Requests containing keys to be watched</param> |
| | | 98 | | /// <param name="method">Method to which watch events should be passed on</param> |
| | | 99 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 100 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 101 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 102 | | /// <returns>A task that completes with an array of watch IDs, in request order</returns> |
| | | 103 | | public Task<long[]> WatchAsync(WatchRequest[] requests, Action<WatchEvent[]> method, Metadata headers = null, |
| | | 104 | | DateTime? deadline = null, CancellationToken cancellationToken = default) |
| | 1 | 105 | | { |
| | 1 | 106 | | List<Task<long>> tasks = []; |
| | | 107 | | |
| | 7 | 108 | | foreach (WatchRequest request in requests) |
| | 2 | 109 | | { |
| | | 110 | | // Create a wrapper that converts WatchResponse to WatchEvent[] |
| | 2 | 111 | | Action<WatchResponse> wrapper = response => |
| | 4 | 112 | | { |
| | 6 | 113 | | WatchEvent[] events = response.Events.Select(e => new WatchEvent |
| | 6 | 114 | | { |
| | 6 | 115 | | Key = e.Kv.Key.ToStringUtf8(), Value = e.Kv.Value.ToStringUtf8(), Type = e.Type |
| | 6 | 116 | | }).ToArray(); |
| | 2 | 117 | | |
| | 4 | 118 | | method(events); |
| | 6 | 119 | | }; |
| | | 120 | | |
| | 2 | 121 | | tasks.Add(_watchManager.WatchAsync(request, wrapper, headers, deadline, cancellationToken)); |
| | 2 | 122 | | } |
| | | 123 | | |
| | 1 | 124 | | return Task.WhenAll(tasks); |
| | 1 | 125 | | } |
| | | 126 | | // CancelWatch methods are already defined in EtcdClient.cs |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Watches the specified requests and passes the watch response to the methods provided. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <param name="requests">Watch Requests containing keys to be watched</param> |
| | | 132 | | /// <param name="methods">Methods to which a watch response should be passed on</param> |
| | | 133 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 134 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 135 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 136 | | public void Watch(WatchRequest[] requests, Action<WatchResponse>[] methods, Metadata headers = null, |
| | | 137 | | DateTime? deadline = null, |
| | | 138 | | CancellationToken cancellationToken = default) |
| | 16 | 139 | | { |
| | 16 | 140 | | methods = PairCallbacks(requests.Length, methods); |
| | | 141 | | |
| | 64 | 142 | | for (int i = 0; i < requests.Length; i++) |
| | 17 | 143 | | { |
| | 17 | 144 | | _watchManager.Watch(requests[i], methods[i], headers, deadline, cancellationToken); |
| | 17 | 145 | | } |
| | 15 | 146 | | } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Watches the specified requests and passes the watch events to the methods provided. |
| | | 150 | | /// </summary> |
| | | 151 | | /// <param name="requests">Watch Requests containing keys to be watched</param> |
| | | 152 | | /// <param name="methods">Methods to which watch events should be passed on</param> |
| | | 153 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 154 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 155 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 156 | | public void Watch(WatchRequest[] requests, Action<WatchEvent[]>[] methods, Metadata headers = null, |
| | | 157 | | DateTime? deadline = null, |
| | | 158 | | CancellationToken cancellationToken = default) |
| | 5 | 159 | | { |
| | 5 | 160 | | methods = PairCallbacks(requests.Length, methods); |
| | | 161 | | |
| | 26 | 162 | | for (int i = 0; i < requests.Length; i++) |
| | 8 | 163 | | { |
| | | 164 | | // Capture the loop index per iteration; otherwise the closure below would read the |
| | | 165 | | // shared loop variable (== requests.Length when an event later arrives) and throw |
| | | 166 | | // IndexOutOfRangeException, silently dropping every event. |
| | 8 | 167 | | int index = i; |
| | | 168 | | |
| | | 169 | | // Create a wrapper that converts WatchResponse to WatchEvent[] |
| | 8 | 170 | | Action<WatchResponse> wrapper = response => |
| | 13 | 171 | | { |
| | 18 | 172 | | WatchEvent[] events = [.. response.Events.Select(e => new WatchEvent |
| | 18 | 173 | | { |
| | 18 | 174 | | Key = e.Kv.Key.ToStringUtf8(), Value = e.Kv.Value.ToStringUtf8(), Type = e.Type |
| | 18 | 175 | | })]; |
| | 8 | 176 | | |
| | 13 | 177 | | methods[index](events); |
| | 21 | 178 | | }; |
| | | 179 | | |
| | 8 | 180 | | _watchManager.Watch(requests[i], wrapper, headers, deadline, cancellationToken); |
| | 8 | 181 | | } |
| | 5 | 182 | | } |
| | | 183 | | |
| | | 184 | | #region Watch Key |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Watches a key according to the specified watch request and |
| | | 188 | | /// passes the watch response to the method provided. |
| | | 189 | | /// </summary> |
| | | 190 | | /// <param name="request">Watch Request containing a key to be watched</param> |
| | | 191 | | /// <param name="method">Method to which a watch response should be passed on</param> |
| | | 192 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 193 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 194 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 195 | | /// <returns>A task that completes with the watch ID</returns> |
| | | 196 | | public Task<long> WatchAsync(WatchRequest request, Action<WatchResponse> method, Metadata headers = null, |
| | | 197 | | DateTime? deadline = null, |
| | | 198 | | CancellationToken cancellationToken = default) => |
| | 1 | 199 | | _watchManager.WatchAsync(request, method, headers, deadline, cancellationToken); |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// Watches a key according to the specified watch request and |
| | | 203 | | /// passes the watch response to the methods provided. |
| | | 204 | | /// </summary> |
| | | 205 | | /// <param name="request">Watch Request containing a key to be watched</param> |
| | | 206 | | /// <param name="methods">Methods to which a watch response should be passed on</param> |
| | | 207 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 208 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 209 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 210 | | /// <returns>A task that completes with the watch ID</returns> |
| | | 211 | | public Task<long> WatchAsync(WatchRequest request, Action<WatchResponse>[] methods, Metadata headers = null, |
| | | 212 | | DateTime? deadline = null, |
| | | 213 | | CancellationToken cancellationToken = default) |
| | 1 | 214 | | { |
| | | 215 | | // Create a wrapper method that calls all the methods |
| | 1 | 216 | | Action<WatchResponse> wrapper = response => |
| | 2 | 217 | | { |
| | 14 | 218 | | foreach (Action<WatchResponse> method in methods) |
| | 4 | 219 | | { |
| | 4 | 220 | | method(response); |
| | 4 | 221 | | } |
| | 3 | 222 | | }; |
| | | 223 | | |
| | 1 | 224 | | return _watchManager.WatchAsync(request, wrapper, headers, deadline, cancellationToken); |
| | 1 | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Watches a key according to the specified watch requests and |
| | | 229 | | /// passes the watch response to the method provided. |
| | | 230 | | /// </summary> |
| | | 231 | | /// <param name="requests">Watch Requests containing keys to be watched</param> |
| | | 232 | | /// <param name="method">Method to which a watch response should be passed on</param> |
| | | 233 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 234 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 235 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 236 | | /// <returns>A task that completes with an array of watch IDs</returns> |
| | | 237 | | public async Task<long[]> WatchAsync(WatchRequest[] requests, Action<WatchResponse> method, Metadata headers = null, |
| | | 238 | | DateTime? deadline = null, |
| | | 239 | | CancellationToken cancellationToken = default) |
| | 1 | 240 | | { |
| | 1 | 241 | | long[] watchIds = new long[requests.Length]; |
| | | 242 | | |
| | 6 | 243 | | for (int i = 0; i < requests.Length; i++) |
| | 2 | 244 | | { |
| | 2 | 245 | | watchIds[i] = await _watchManager.WatchAsync(requests[i], method, headers, deadline, cancellationToken) |
| | 2 | 246 | | .ConfigureAwait(false); |
| | 2 | 247 | | } |
| | | 248 | | |
| | 1 | 249 | | return watchIds; |
| | 1 | 250 | | } |
| | | 251 | | |
| | | 252 | | /// <summary> |
| | | 253 | | /// Watches a key according to the specified watch requests and |
| | | 254 | | /// passes the watch response to the methods provided. |
| | | 255 | | /// </summary> |
| | | 256 | | /// <param name="requests">Watch Requests containing keys to be watched</param> |
| | | 257 | | /// <param name="methods">Methods to which a watch response should be passed on</param> |
| | | 258 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 259 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 260 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 261 | | /// <returns>A task that completes with an array of watch IDs</returns> |
| | | 262 | | public async Task<long[]> WatchAsync(WatchRequest[] requests, Action<WatchResponse>[] methods, |
| | | 263 | | Metadata headers = null, |
| | | 264 | | DateTime? deadline = null, |
| | | 265 | | CancellationToken cancellationToken = default) |
| | 4 | 266 | | { |
| | 4 | 267 | | methods = PairCallbacks(requests.Length, methods); |
| | | 268 | | |
| | 4 | 269 | | long[] watchIds = new long[requests.Length]; |
| | | 270 | | |
| | 20 | 271 | | for (int i = 0; i < requests.Length; i++) |
| | 6 | 272 | | { |
| | 6 | 273 | | watchIds[i] = await _watchManager.WatchAsync(requests[i], methods[i], headers, deadline, cancellationToken) |
| | 6 | 274 | | .ConfigureAwait(false); |
| | 6 | 275 | | } |
| | | 276 | | |
| | 4 | 277 | | return watchIds; |
| | 4 | 278 | | } |
| | | 279 | | |
| | | 280 | | /// <summary> |
| | | 281 | | /// Watches a key according to the specified watch request and |
| | | 282 | | /// passes the minimal watch event data to the method provided. |
| | | 283 | | /// </summary> |
| | | 284 | | /// <param name="request">Watch Request containing a key to be watched</param> |
| | | 285 | | /// <param name="method">Method to which minimal watch events data should be passed on</param> |
| | | 286 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 287 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 288 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 289 | | public Task WatchAsync(WatchRequest request, Action<WatchEvent[]> method, Metadata headers = null, |
| | | 290 | | DateTime? deadline = null, |
| | | 291 | | CancellationToken cancellationToken = default) |
| | 1 | 292 | | { |
| | | 293 | | // Create a wrapper that converts WatchResponse to WatchEvent[] |
| | 1 | 294 | | Action<WatchResponse> wrapper = response => |
| | 2 | 295 | | { |
| | 2 | 296 | | WatchEvent[] events = response.Events.Select(i => |
| | 1 | 297 | | { |
| | 1 | 298 | | return new WatchEvent |
| | 1 | 299 | | { |
| | 1 | 300 | | Key = i.Kv.Key.ToStringUtf8(), Value = i.Kv.Value.ToStringUtf8(), Type = i.Type |
| | 1 | 301 | | }; |
| | 3 | 302 | | }).ToArray(); |
| | 1 | 303 | | |
| | 2 | 304 | | method(events); |
| | 3 | 305 | | }; |
| | | 306 | | |
| | 1 | 307 | | return _watchManager.WatchAsync(request, wrapper, headers, deadline, cancellationToken); |
| | 1 | 308 | | } |
| | | 309 | | |
| | | 310 | | /// <summary> |
| | | 311 | | /// Watches a key according to the specified watch request and |
| | | 312 | | /// passes the minimal watch event data to the methods provided. |
| | | 313 | | /// </summary> |
| | | 314 | | /// <param name="request">Watch Request containing a key to be watched</param> |
| | | 315 | | /// <param name="methods">Methods to which minimal watch events data should be passed on</param> |
| | | 316 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 317 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 318 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 319 | | public Task WatchAsync(WatchRequest request, Action<WatchEvent[]>[] methods, Metadata headers = null, |
| | | 320 | | DateTime? deadline = null, |
| | | 321 | | CancellationToken cancellationToken = default) |
| | 1 | 322 | | { |
| | | 323 | | // Create a wrapper that converts WatchResponse to WatchEvent[] and calls all methods |
| | 1 | 324 | | Action<WatchResponse> wrapper = response => |
| | 2 | 325 | | { |
| | 2 | 326 | | WatchEvent[] events = response.Events.Select(i => |
| | 1 | 327 | | { |
| | 1 | 328 | | return new WatchEvent |
| | 1 | 329 | | { |
| | 1 | 330 | | Key = i.Kv.Key.ToStringUtf8(), Value = i.Kv.Value.ToStringUtf8(), Type = i.Type |
| | 1 | 331 | | }; |
| | 3 | 332 | | }).ToArray(); |
| | 1 | 333 | | |
| | 14 | 334 | | foreach (Action<WatchEvent[]> method in methods) |
| | 4 | 335 | | { |
| | 4 | 336 | | method(events); |
| | 4 | 337 | | } |
| | 3 | 338 | | }; |
| | | 339 | | |
| | 1 | 340 | | return _watchManager.WatchAsync(request, wrapper, headers, deadline, cancellationToken); |
| | 1 | 341 | | } |
| | | 342 | | |
| | | 343 | | /// <summary> |
| | | 344 | | /// Watches the specified key and passes the watch response to the method provided. |
| | | 345 | | /// </summary> |
| | | 346 | | /// <param name="request">Watch Request</param> |
| | | 347 | | /// <param name="method">Method to which a watch response should be passed on</param> |
| | | 348 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 349 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 350 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 351 | | public void Watch(WatchRequest request, Action<WatchResponse> method, Metadata headers = null, |
| | | 352 | | DateTime? deadline = null, |
| | | 353 | | CancellationToken cancellationToken = default) => |
| | 1 | 354 | | _watchManager.Watch(request, method, headers, deadline, cancellationToken); |
| | | 355 | | |
| | | 356 | | /// <summary> |
| | | 357 | | /// Watches a key according to the specified watch request and |
| | | 358 | | /// passes the watch response to the methods provided. |
| | | 359 | | /// </summary> |
| | | 360 | | /// <param name="request">Watch Request containing a key to be watched</param> |
| | | 361 | | /// <param name="methods">Methods to which a watch response should be passed on</param> |
| | | 362 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 363 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 364 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 365 | | public void Watch(WatchRequest request, Action<WatchResponse>[] methods, |
| | | 366 | | Metadata headers = null, DateTime? deadline = null, |
| | 1 | 367 | | CancellationToken cancellationToken = default) => Watch([request], methods, headers, |
| | 1 | 368 | | deadline, cancellationToken); |
| | | 369 | | |
| | | 370 | | |
| | | 371 | | /// <summary> |
| | | 372 | | /// Watches the specified keys and passes the watch response to the method provided. |
| | | 373 | | /// </summary> |
| | | 374 | | /// <param name="key">Key to watch</param> |
| | | 375 | | /// <param name="method">Method to which a watch response should be passed on</param> |
| | | 376 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 377 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 378 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 379 | | public void Watch(string key, Action<WatchResponse> method, Metadata headers = null, |
| | | 380 | | DateTime? deadline = null, |
| | 11 | 381 | | CancellationToken cancellationToken = default) => Watch([key], |
| | 11 | 382 | | [method], headers, deadline, cancellationToken); |
| | | 383 | | |
| | | 384 | | /// <summary> |
| | | 385 | | /// Watches the specified key and passes the watch response to the methods provided. |
| | | 386 | | /// </summary> |
| | | 387 | | /// <param name="key">Key to be watched</param> |
| | | 388 | | /// <param name="methods">Methods to which a watch response should be passed on</param> |
| | | 389 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 390 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 391 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 392 | | public void Watch(string key, Action<WatchResponse>[] methods, Metadata headers = null, |
| | | 393 | | DateTime? deadline = null, |
| | | 394 | | CancellationToken cancellationToken = default) => |
| | 1 | 395 | | Watch([key], methods, headers, deadline, cancellationToken); |
| | | 396 | | |
| | | 397 | | |
| | | 398 | | /// <summary> |
| | | 399 | | /// Watches the specified keys and passes the watch response to the method provided. |
| | | 400 | | /// </summary> |
| | | 401 | | /// <param name="keys">Keys to be watched</param> |
| | | 402 | | /// <param name="method">Method to which a watch response should be passed on</param> |
| | | 403 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 404 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 405 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 406 | | public void Watch(string[] keys, Action<WatchResponse> method, Metadata headers = null, |
| | | 407 | | DateTime? deadline = null, |
| | 1 | 408 | | CancellationToken cancellationToken = default) => Watch(keys, [method], headers, |
| | 1 | 409 | | deadline, cancellationToken); |
| | | 410 | | |
| | | 411 | | /// <summary> |
| | | 412 | | /// Watches the specified keys and passes the watch response to the method provided. |
| | | 413 | | /// </summary> |
| | | 414 | | /// <param name="keys">Keys to be watched</param> |
| | | 415 | | /// <param name="methods">Methods to which a watch response should be passed on</param> |
| | | 416 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 417 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 418 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 419 | | public void Watch(string[] keys, Action<WatchResponse>[] methods, Metadata headers = null, |
| | | 420 | | DateTime? deadline = null, |
| | | 421 | | CancellationToken cancellationToken = default) |
| | 13 | 422 | | { |
| | 13 | 423 | | List<WatchRequest> requests = []; |
| | | 424 | | |
| | 67 | 425 | | foreach (string key in keys) |
| | 14 | 426 | | { |
| | 14 | 427 | | WatchRequest request = new() |
| | 14 | 428 | | { |
| | 14 | 429 | | CreateRequest = new WatchCreateRequest |
| | 14 | 430 | | { |
| | 14 | 431 | | Key = ByteString.CopyFromUtf8(key), ProgressNotify = true, PrevKv = true |
| | 14 | 432 | | } |
| | 14 | 433 | | }; |
| | 14 | 434 | | requests.Add(request); |
| | 14 | 435 | | } |
| | | 436 | | |
| | 13 | 437 | | Watch(requests.ToArray(), methods, headers, deadline, cancellationToken); |
| | 13 | 438 | | } |
| | | 439 | | |
| | | 440 | | |
| | | 441 | | /// <summary> |
| | | 442 | | /// Watches the specified key and passes the minimal watch events data to the method provided. |
| | | 443 | | /// </summary> |
| | | 444 | | /// <param name="key">Key to be watched</param> |
| | | 445 | | /// <param name="method">Method to which minimal watch events data should be passed on</param> |
| | | 446 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 447 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 448 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 449 | | public void Watch(string key, Action<WatchEvent[]> method, Metadata headers = null, |
| | | 450 | | DateTime? deadline = null, |
| | 1 | 451 | | CancellationToken cancellationToken = default) => Watch([key], |
| | 1 | 452 | | [method], headers, deadline, cancellationToken); |
| | | 453 | | |
| | | 454 | | /// <summary> |
| | | 455 | | /// Watches the specified key and passes the minimal watch events data to the methods provided. |
| | | 456 | | /// </summary> |
| | | 457 | | /// <param name="key">Key to be watched</param> |
| | | 458 | | /// <param name="methods">Methods to which minimal watch events data should be passed on</param> |
| | | 459 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 460 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 461 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 462 | | public void Watch(string key, Action<WatchEvent[]>[] methods, Metadata headers = null, |
| | | 463 | | DateTime? deadline = null, |
| | | 464 | | CancellationToken cancellationToken = default) => |
| | 1 | 465 | | Watch([key], methods, headers, deadline, cancellationToken); |
| | | 466 | | |
| | | 467 | | /// <summary> |
| | | 468 | | /// Watches the specified keys and passes the minimal watch events data to the method provided. |
| | | 469 | | /// </summary> |
| | | 470 | | /// <param name="keys">Keys to be watched</param> |
| | | 471 | | /// <param name="method">Method to which minimal watch events data should be passed on</param> |
| | | 472 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 473 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 474 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 475 | | public void Watch(string[] keys, Action<WatchEvent[]> method, Metadata headers = null, |
| | | 476 | | DateTime? deadline = null, |
| | 1 | 477 | | CancellationToken cancellationToken = default) => Watch(keys, [method], headers, |
| | 1 | 478 | | deadline, cancellationToken); |
| | | 479 | | |
| | | 480 | | /// <summary> |
| | | 481 | | /// Watches the specified keys and passes the minimal watch events data to the method provided. |
| | | 482 | | /// </summary> |
| | | 483 | | /// <param name="keys">Keys to be watched</param> |
| | | 484 | | /// <param name="methods">Methods to which minimal watch events data should be passed on</param> |
| | | 485 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 486 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 487 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 488 | | public void Watch(string[] keys, Action<WatchEvent[]>[] methods, Metadata headers = null, |
| | | 489 | | DateTime? deadline = null, |
| | | 490 | | CancellationToken cancellationToken = default) |
| | 3 | 491 | | { |
| | 3 | 492 | | List<WatchRequest> requests = []; |
| | | 493 | | |
| | 17 | 494 | | foreach (string key in keys) |
| | 4 | 495 | | { |
| | 4 | 496 | | WatchRequest request = new() |
| | 4 | 497 | | { |
| | 4 | 498 | | CreateRequest = new WatchCreateRequest { Key = ByteString.CopyFromUtf8(key) } |
| | 4 | 499 | | }; |
| | 4 | 500 | | requests.Add(request); |
| | 4 | 501 | | } |
| | | 502 | | |
| | 3 | 503 | | Watch(requests.ToArray(), methods, headers, deadline, cancellationToken); |
| | 3 | 504 | | } |
| | | 505 | | |
| | | 506 | | |
| | | 507 | | /// <summary> |
| | | 508 | | /// Watches the specified key and passes the watch response to the method provided. |
| | | 509 | | /// </summary> |
| | | 510 | | /// <param name="key">Key to be watched</param> |
| | | 511 | | /// <param name="method">Method to which a watch response should be passed on</param> |
| | | 512 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 513 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 514 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 515 | | public Task WatchAsync(string key, Action<WatchResponse> method, Metadata headers = null, |
| | | 516 | | DateTime? deadline = null, |
| | 1 | 517 | | CancellationToken cancellationToken = default) => WatchAsync([key], |
| | 1 | 518 | | [method], headers, deadline, cancellationToken); |
| | | 519 | | |
| | | 520 | | /// <summary> |
| | | 521 | | /// Watches the specified key and passes the watch response to the methods provided. |
| | | 522 | | /// </summary> |
| | | 523 | | /// <param name="key">Key to be watched</param> |
| | | 524 | | /// <param name="methods">Methods to which a watch response should be passed on</param> |
| | | 525 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 526 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 527 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 528 | | public Task WatchAsync(string key, Action<WatchResponse>[] methods, Metadata headers = null, |
| | | 529 | | DateTime? deadline = null, |
| | | 530 | | CancellationToken cancellationToken = default) => |
| | 1 | 531 | | WatchAsync([key], methods, headers, deadline, cancellationToken); |
| | | 532 | | |
| | | 533 | | |
| | | 534 | | /// <summary> |
| | | 535 | | /// Watches the specified keys and passes the watch response to the method provided. |
| | | 536 | | /// </summary> |
| | | 537 | | /// <param name="keys">Keys to be watched</param> |
| | | 538 | | /// <param name="method">Method to which a watch response should be passed on</param> |
| | | 539 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 540 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 541 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 542 | | public Task WatchAsync(string[] keys, Action<WatchResponse> method, Metadata headers = null, |
| | | 543 | | DateTime? deadline = null, |
| | 1 | 544 | | CancellationToken cancellationToken = default) => WatchAsync(keys, [method], |
| | 1 | 545 | | headers, deadline, cancellationToken); |
| | | 546 | | |
| | | 547 | | /// <summary> |
| | | 548 | | /// Watches the specified keys and passes the watch response to the method provided. |
| | | 549 | | /// </summary> |
| | | 550 | | /// <param name="keys">Keys to be watched</param> |
| | | 551 | | /// <param name="methods">Methods to which a watch response should be passed on</param> |
| | | 552 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 553 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 554 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 555 | | public Task WatchAsync(string[] keys, Action<WatchResponse>[] methods, Metadata headers = null, |
| | | 556 | | DateTime? deadline = null, |
| | | 557 | | CancellationToken cancellationToken = default) |
| | 3 | 558 | | { |
| | 3 | 559 | | List<WatchRequest> requests = []; |
| | | 560 | | |
| | 17 | 561 | | foreach (string key in keys) |
| | 4 | 562 | | { |
| | 4 | 563 | | WatchRequest request = new() |
| | 4 | 564 | | { |
| | 4 | 565 | | CreateRequest = new WatchCreateRequest |
| | 4 | 566 | | { |
| | 4 | 567 | | Key = ByteString.CopyFromUtf8(key), ProgressNotify = true, PrevKv = true |
| | 4 | 568 | | } |
| | 4 | 569 | | }; |
| | 4 | 570 | | requests.Add(request); |
| | 4 | 571 | | } |
| | | 572 | | |
| | 3 | 573 | | return WatchAsync(requests.ToArray(), methods, headers, deadline, cancellationToken); |
| | 3 | 574 | | } |
| | | 575 | | |
| | | 576 | | /// <summary> |
| | | 577 | | /// Watches the specified key and passes the minimal watch events data to the method provided. |
| | | 578 | | /// </summary> |
| | | 579 | | /// <param name="key">Key to be watched</param> |
| | | 580 | | /// <param name="method">Method to which minimal watch events data should be passed on</param> |
| | | 581 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 582 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 583 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 584 | | public Task WatchAsync(string key, Action<WatchEvent[]> method, Metadata headers = null, |
| | | 585 | | DateTime? deadline = null, |
| | 1 | 586 | | CancellationToken cancellationToken = default) => WatchAsync([key], |
| | 1 | 587 | | [method], headers, deadline, cancellationToken); |
| | | 588 | | |
| | | 589 | | /// <summary> |
| | | 590 | | /// Watches the specified key and passes the minimal watch events data to the methods provided. |
| | | 591 | | /// </summary> |
| | | 592 | | /// <param name="key">Key to be watched</param> |
| | | 593 | | /// <param name="methods">Methods to which minimal watch events data should be passed on</param> |
| | | 594 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 595 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 596 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 597 | | public Task WatchAsync(string key, Action<WatchEvent[]>[] methods, Metadata headers = null, |
| | | 598 | | DateTime? deadline = null, |
| | | 599 | | CancellationToken cancellationToken = default) => |
| | 1 | 600 | | WatchAsync([key], methods, headers, deadline, cancellationToken); |
| | | 601 | | |
| | | 602 | | /// <summary> |
| | | 603 | | /// Watches the specified keys and passes the minimal watch events data to the method provided. |
| | | 604 | | /// </summary> |
| | | 605 | | /// <param name="keys">Keys to be watched</param> |
| | | 606 | | /// <param name="method">Method to which minimal watch events data should be passed on</param> |
| | | 607 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 608 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 609 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 610 | | public Task WatchAsync(string[] keys, Action<WatchEvent[]> method, Metadata headers = null, |
| | | 611 | | DateTime? deadline = null, |
| | 1 | 612 | | CancellationToken cancellationToken = default) => WatchAsync(keys, [method], |
| | 1 | 613 | | headers, deadline, cancellationToken); |
| | | 614 | | |
| | | 615 | | /// <summary> |
| | | 616 | | /// Watches the specified keys and passes the minimal watch events data to the method provided. |
| | | 617 | | /// </summary> |
| | | 618 | | /// <param name="keys">Keys to be watched</param> |
| | | 619 | | /// <param name="methods">Methods to which minimal watch events data should be passed on</param> |
| | | 620 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 621 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 622 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 623 | | public Task WatchAsync(string[] keys, Action<WatchEvent[]>[] methods, Metadata headers = null, |
| | | 624 | | DateTime? deadline = null, |
| | | 625 | | CancellationToken cancellationToken = default) |
| | 3 | 626 | | { |
| | 3 | 627 | | List<WatchRequest> requests = []; |
| | | 628 | | |
| | 17 | 629 | | foreach (string key in keys) |
| | 4 | 630 | | { |
| | 4 | 631 | | WatchRequest request = new() |
| | 4 | 632 | | { |
| | 4 | 633 | | CreateRequest = new WatchCreateRequest |
| | 4 | 634 | | { |
| | 4 | 635 | | Key = ByteString.CopyFromUtf8(key), ProgressNotify = true, PrevKv = true |
| | 4 | 636 | | } |
| | 4 | 637 | | }; |
| | 4 | 638 | | requests.Add(request); |
| | 4 | 639 | | } |
| | | 640 | | |
| | 3 | 641 | | return WatchAsync(requests.ToArray(), methods, headers, deadline, cancellationToken); |
| | 3 | 642 | | } |
| | | 643 | | |
| | | 644 | | #endregion |
| | | 645 | | |
| | | 646 | | #region Watch Range of keys |
| | | 647 | | |
| | | 648 | | /// <summary> |
| | | 649 | | /// Watches the specified key range and passes the watch response to the method provided. |
| | | 650 | | /// </summary> |
| | | 651 | | /// <param name="path">Path to be watched</param> |
| | | 652 | | /// <param name="method">Method to which a watch response should be passed on</param> |
| | | 653 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 654 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 655 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 656 | | /// <returns>A watch ID that can be used to cancel the watch</returns> |
| | | 657 | | public long WatchRange(string path, Action<WatchResponse> method, Metadata headers = null, |
| | | 658 | | DateTime? deadline = null, |
| | | 659 | | CancellationToken cancellationToken = default) |
| | 7 | 660 | | { |
| | 7 | 661 | | WatchRequest request = new() |
| | 7 | 662 | | { |
| | 7 | 663 | | CreateRequest = new WatchCreateRequest |
| | 7 | 664 | | { |
| | 7 | 665 | | Key = GetStringByteForRangeRequests(path), |
| | 7 | 666 | | RangeEnd = ByteString.CopyFromUtf8(GetRangeEnd(path)), |
| | 7 | 667 | | ProgressNotify = true, |
| | 7 | 668 | | PrevKv = true |
| | 7 | 669 | | } |
| | 7 | 670 | | }; |
| | | 671 | | |
| | 7 | 672 | | return _watchManager.Watch(request, method, headers, deadline, cancellationToken); |
| | 7 | 673 | | } |
| | | 674 | | |
| | | 675 | | /// <summary> |
| | | 676 | | /// Watches the specified key range and passes the watch response to the methods provided. |
| | | 677 | | /// </summary> |
| | | 678 | | /// <param name="path">Path to be watched</param> |
| | | 679 | | /// <param name="methods">Methods to which a watch response should be passed on</param> |
| | | 680 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 681 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 682 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 683 | | /// <returns>A watch ID that can be used to cancel the watch</returns> |
| | | 684 | | public long WatchRange(string path, Action<WatchResponse>[] methods, Metadata headers = null, |
| | | 685 | | DateTime? deadline = null, |
| | | 686 | | CancellationToken cancellationToken = default) |
| | 1 | 687 | | { |
| | | 688 | | // Create a wrapper method that calls all the methods |
| | 1 | 689 | | Action<WatchResponse> wrapper = response => |
| | 2 | 690 | | { |
| | 14 | 691 | | foreach (Action<WatchResponse> method in methods) |
| | 4 | 692 | | { |
| | 4 | 693 | | method(response); |
| | 4 | 694 | | } |
| | 3 | 695 | | }; |
| | | 696 | | |
| | 1 | 697 | | return WatchRange(path, wrapper, headers, deadline, cancellationToken); |
| | 1 | 698 | | } |
| | | 699 | | |
| | | 700 | | /// <summary> |
| | | 701 | | /// Watches the specified key range and passes the watch response to the method provided. |
| | | 702 | | /// </summary> |
| | | 703 | | /// <param name="paths">Paths to be watched</param> |
| | | 704 | | /// <param name="method">Method to which a watch response should be passed on</param> |
| | | 705 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 706 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 707 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 708 | | /// <returns>An array of watch IDs that can be used to cancel the watches</returns> |
| | | 709 | | public long[] WatchRange(string[] paths, Action<WatchResponse> method, Metadata headers = null, |
| | | 710 | | DateTime? deadline = null, |
| | | 711 | | CancellationToken cancellationToken = default) |
| | 1 | 712 | | { |
| | 1 | 713 | | long[] watchIds = new long[paths.Length]; |
| | | 714 | | |
| | 6 | 715 | | for (int i = 0; i < paths.Length; i++) |
| | 2 | 716 | | { |
| | 2 | 717 | | watchIds[i] = WatchRange(paths[i], method, headers, deadline, cancellationToken); |
| | 2 | 718 | | } |
| | | 719 | | |
| | 1 | 720 | | return watchIds; |
| | 1 | 721 | | } |
| | | 722 | | |
| | | 723 | | /// <summary> |
| | | 724 | | /// Watches the specified key range and passes the watch response to the method provided. |
| | | 725 | | /// </summary> |
| | | 726 | | /// <param name="paths">Paths to be watched</param> |
| | | 727 | | /// <param name="methods">Methods to which a watch response should be passed on</param> |
| | | 728 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 729 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 730 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 731 | | /// <returns>An array of watch IDs that can be used to cancel the watches</returns> |
| | | 732 | | public long[] WatchRange(string[] paths, Action<WatchResponse>[] methods, Metadata headers = null, |
| | | 733 | | DateTime? deadline = null, |
| | | 734 | | CancellationToken cancellationToken = default) |
| | 2 | 735 | | { |
| | 2 | 736 | | if (paths.Length != methods.Length) |
| | 1 | 737 | | { |
| | 1 | 738 | | throw new ArgumentException("The number of paths must match the number of methods"); |
| | | 739 | | } |
| | | 740 | | |
| | 1 | 741 | | long[] watchIds = new long[paths.Length]; |
| | | 742 | | |
| | 6 | 743 | | for (int i = 0; i < paths.Length; i++) |
| | 2 | 744 | | { |
| | 2 | 745 | | watchIds[i] = WatchRange(paths[i], methods[i], headers, deadline, cancellationToken); |
| | 2 | 746 | | } |
| | | 747 | | |
| | 1 | 748 | | return watchIds; |
| | 1 | 749 | | } |
| | | 750 | | |
| | | 751 | | /// <summary> |
| | | 752 | | /// Watches the specified key range and passes the watch response to the method provided asynchronously. |
| | | 753 | | /// </summary> |
| | | 754 | | /// <param name="path">Path to be watched</param> |
| | | 755 | | /// <param name="method">Method to which a watch response should be passed on</param> |
| | | 756 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 757 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 758 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 759 | | /// <returns>A task that completes with the watch ID</returns> |
| | | 760 | | public Task<long> WatchRangeAsync(string path, Action<WatchResponse> method, Metadata headers = null, |
| | | 761 | | DateTime? deadline = null, |
| | | 762 | | CancellationToken cancellationToken = default) |
| | 6 | 763 | | { |
| | 6 | 764 | | WatchRequest request = new() |
| | 6 | 765 | | { |
| | 6 | 766 | | CreateRequest = new WatchCreateRequest |
| | 6 | 767 | | { |
| | 6 | 768 | | Key = GetStringByteForRangeRequests(path), |
| | 6 | 769 | | RangeEnd = ByteString.CopyFromUtf8(GetRangeEnd(path)), |
| | 6 | 770 | | ProgressNotify = true, |
| | 6 | 771 | | PrevKv = true |
| | 6 | 772 | | } |
| | 6 | 773 | | }; |
| | | 774 | | |
| | 6 | 775 | | return _watchManager.WatchAsync(request, method, headers, deadline, cancellationToken); |
| | 6 | 776 | | } |
| | | 777 | | |
| | | 778 | | /// <summary> |
| | | 779 | | /// Watches the specified key range and passes the watch response to the methods provided asynchronously. |
| | | 780 | | /// </summary> |
| | | 781 | | /// <param name="path">Path to be watched</param> |
| | | 782 | | /// <param name="methods">Methods to which a watch response should be passed on</param> |
| | | 783 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 784 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 785 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 786 | | /// <returns>A task that completes with the watch ID</returns> |
| | | 787 | | public Task<long> WatchRangeAsync(string path, Action<WatchResponse>[] methods, Metadata headers = null, |
| | | 788 | | DateTime? deadline = null, |
| | | 789 | | CancellationToken cancellationToken = default) |
| | 1 | 790 | | { |
| | | 791 | | // Create a wrapper method that calls all the methods |
| | 1 | 792 | | Action<WatchResponse> wrapper = response => |
| | 2 | 793 | | { |
| | 14 | 794 | | foreach (Action<WatchResponse> method in methods) |
| | 4 | 795 | | { |
| | 4 | 796 | | method(response); |
| | 4 | 797 | | } |
| | 3 | 798 | | }; |
| | | 799 | | |
| | 1 | 800 | | return WatchRangeAsync(path, wrapper, headers, deadline, cancellationToken); |
| | 1 | 801 | | } |
| | | 802 | | |
| | | 803 | | /// <summary> |
| | | 804 | | /// Watches the specified key range and passes the watch response to the method provided asynchronously. |
| | | 805 | | /// </summary> |
| | | 806 | | /// <param name="paths">Paths to be watched</param> |
| | | 807 | | /// <param name="method">Method to which a watch response should be passed on</param> |
| | | 808 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 809 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 810 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 811 | | /// <returns>A task that completes with an array of watch IDs</returns> |
| | | 812 | | public async Task<long[]> WatchRangeAsync(string[] paths, Action<WatchResponse> method, Metadata headers = null, |
| | | 813 | | DateTime? deadline = null, |
| | | 814 | | CancellationToken cancellationToken = default) |
| | 1 | 815 | | { |
| | 1 | 816 | | long[] watchIds = new long[paths.Length]; |
| | | 817 | | |
| | 6 | 818 | | for (int i = 0; i < paths.Length; i++) |
| | 2 | 819 | | { |
| | 2 | 820 | | watchIds[i] = await WatchRangeAsync(paths[i], method, headers, deadline, cancellationToken) |
| | 2 | 821 | | .ConfigureAwait(false); |
| | 2 | 822 | | } |
| | | 823 | | |
| | 1 | 824 | | return watchIds; |
| | 1 | 825 | | } |
| | | 826 | | |
| | | 827 | | /// <summary> |
| | | 828 | | /// Watches the specified key range and passes the watch response to the method provided asynchronously. |
| | | 829 | | /// </summary> |
| | | 830 | | /// <param name="paths">Paths to be watched</param> |
| | | 831 | | /// <param name="methods">Methods to which a watch response should be passed on</param> |
| | | 832 | | /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param> |
| | | 833 | | /// <param name="deadline">An optional deadline for the call. The call will be canceled if the deadline is hit.</par |
| | | 834 | | /// <param name="cancellationToken">An optional token for canceling the call.</param> |
| | | 835 | | /// <returns>A task that completes with an array of watch IDs</returns> |
| | | 836 | | public async Task<long[]> WatchRangeAsync(string[] paths, Action<WatchResponse>[] methods, Metadata headers = null, |
| | | 837 | | DateTime? deadline = null, |
| | | 838 | | CancellationToken cancellationToken = default) |
| | 2 | 839 | | { |
| | 2 | 840 | | if (paths.Length != methods.Length) |
| | 1 | 841 | | { |
| | 1 | 842 | | throw new ArgumentException("The number of paths must match the number of methods"); |
| | | 843 | | } |
| | | 844 | | |
| | 1 | 845 | | long[] watchIds = new long[paths.Length]; |
| | | 846 | | |
| | 6 | 847 | | for (int i = 0; i < paths.Length; i++) |
| | 2 | 848 | | { |
| | 2 | 849 | | watchIds[i] = await WatchRangeAsync(paths[i], methods[i], headers, deadline, cancellationToken) |
| | 2 | 850 | | .ConfigureAwait(false); |
| | 2 | 851 | | } |
| | | 852 | | |
| | 1 | 853 | | return watchIds; |
| | 1 | 854 | | } |
| | | 855 | | |
| | | 856 | | #endregion |
| | | 857 | | } |