|
| 1 | +namespace StackExchange.Redis; |
| 2 | + |
| 3 | +public readonly partial struct GcraRateLimitResult |
| 4 | +{ |
| 5 | + internal static readonly ResultProcessor<GcraRateLimitResult> Processor = new GcraRateLimitResultProcessor(); |
| 6 | + |
| 7 | + private sealed class GcraRateLimitResultProcessor : ResultProcessor<GcraRateLimitResult> |
| 8 | + { |
| 9 | + protected override bool SetResultCore(PhysicalConnection connection, Message message, in RawResult result) |
| 10 | + { |
| 11 | + // GCRA returns an array with 5 elements: |
| 12 | + // 1) <limited> # 0 or 1 |
| 13 | + // 2) <max-req-num> # max number of request. Always equal to max_burst+1 |
| 14 | + // 3) <num-avail-req> # number of requests available immediately |
| 15 | + // 4) <reply-after> # number of seconds after which caller should retry. Always returns -1 if request isn't limited. |
| 16 | + // 5) <full-burst-after> # number of seconds after which a full burst will be allowed |
| 17 | + if (result.Resp2TypeArray == ResultType.Array && result.ItemsCount >= 5) |
| 18 | + { |
| 19 | + var items = result.GetItems(); |
| 20 | + bool limited = items[0].GetBoolean(); |
| 21 | + if (items[1].TryGetInt64(out long maxRequests) |
| 22 | + && items[2].TryGetInt64(out long availableRequests) |
| 23 | + && items[3].TryGetInt64(out long retryAfterSeconds) |
| 24 | + && items[4].TryGetInt64(out long fullBurstAfterSeconds)) |
| 25 | + { |
| 26 | + var grca = new GcraRateLimitResult( |
| 27 | + limited: limited, |
| 28 | + maxRequests: (int)maxRequests, |
| 29 | + availableRequests: (int)availableRequests, |
| 30 | + retryAfterSeconds: (int)retryAfterSeconds, |
| 31 | + fullBurstAfterSeconds: (int)fullBurstAfterSeconds); |
| 32 | + SetResult(message, grca); |
| 33 | + return true; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + /* for v3, already done (due to branch choice) |
| 41 | + protected override bool SetResultCore(PhysicalConnection connection, Message message, ref RespReader reader) |
| 42 | + { |
| 43 | + // GCRA returns an array with 5 elements: |
| 44 | + // 1) <limited> # 0 or 1 |
| 45 | + // 2) <max-req-num> # max number of request. Always equal to max_burst+1 |
| 46 | + // 3) <num-avail-req> # number of requests available immediately |
| 47 | + // 4) <reply-after> # number of seconds after which caller should retry. Always returns -1 if request isn't limited. |
| 48 | + // 5) <full-burst-after> # number of seconds after which a full burst will be allowed |
| 49 | + if (reader.IsAggregate |
| 50 | + && reader.TryMoveNext() && reader.IsScalar && reader.TryReadBoolean(out bool limited) |
| 51 | + && reader.TryMoveNext() && reader.IsScalar && reader.TryReadInt64(out long maxRequests) |
| 52 | + && reader.TryMoveNext() && reader.IsScalar && reader.TryReadInt64(out long availableRequests) |
| 53 | + && reader.TryMoveNext() && reader.IsScalar && reader.TryReadInt64(out long retryAfterSeconds) |
| 54 | + && reader.TryMoveNext() && reader.IsScalar && reader.TryReadInt64(out long fullBurstAfterSeconds)) |
| 55 | + { |
| 56 | + var result = new GcraRateLimitResult( |
| 57 | + limited: limited, |
| 58 | + maxRequests: (int)maxRequests, |
| 59 | + availableRequests: (int)availableRequests, |
| 60 | + retryAfterSeconds: (int)retryAfterSeconds, |
| 61 | + fullBurstAfterSeconds: (int)fullBurstAfterSeconds); |
| 62 | + SetResult(message, result); |
| 63 | + return true; |
| 64 | + } |
| 65 | +
|
| 66 | + return false; |
| 67 | + } |
| 68 | + */ |
| 69 | + } |
| 70 | +} |
0 commit comments