| 1 | namespace Zipelo.Core; |
| 2 | |
| 3 | // 제한값은 호스트가 정함. 아래 값은 제품 기본값이나 성능 약속이 아님. |
| 4 | public sealed record JobLimits(long MaxEntries, long MaxProcessedBytes, long MaxOutputBytes, TimeSpan MaxDuration, int MaxPasswordAttempts); |
| 5 | public sealed record BudgetUsage(long Entries, long ProcessedBytes, long OutputBytes); |
| 6 | |
| 7 | public sealed class JobBudget |
| 8 | { |
| 9 | private readonly object gate = new(); |
| 10 | private readonly JobLimits limits; |
| 11 | private readonly TimeProvider time; |
| 12 | private readonly long started; |
| 13 | private long entries; |
| 14 | private long processedBytes; |
| 15 | private long outputBytes; |
| 16 | private int exhausted; |
| 17 | |
| 18 | public JobBudget(JobLimits limits, TimeProvider? timeProvider = null) |
| 19 | { |
| 20 | ArgumentNullException.ThrowIfNull(limits); |
| 21 | if (limits.MaxEntries <= 0 || limits.MaxProcessedBytes <= 0 || limits.MaxOutputBytes <= 0 || |
| 22 | limits.MaxDuration <= TimeSpan.Zero || limits.MaxPasswordAttempts <= 0) |
| 23 | { |
| 24 | throw new ArgumentOutOfRangeException(nameof(limits)); |
| 25 | } |
| 26 | |
| 27 | this.limits = limits; |
| 28 | time = timeProvider ?? TimeProvider.System; |
| 29 | started = time.GetTimestamp(); |
| 30 | } |
| 31 | |
| 32 | public BudgetUsage Usage |
| 33 | { |
| 34 | get { lock (gate) { return new(entries, processedBytes, outputBytes); } } |
| 35 | } |
| 36 | |
| 37 | public void Check(CancellationToken cancellationToken) |
| 38 | { |
| 39 | cancellationToken.ThrowIfCancellationRequested(); |
| 40 | if (Volatile.Read(ref exhausted) != 0 || time.GetElapsedTime(started) >= limits.MaxDuration) |
| 41 | { |
| 42 | Interlocked.Exchange(ref exhausted, 1); |
| 43 | throw new ArchiveException(ArchiveError.BudgetExceeded); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public void AddEntry(CancellationToken cancellationToken) => Charge(1, 0, 0, cancellationToken); |
| 48 | |
| 49 | // 중간 TAR, 솔리드 선행 처리, 복구 재시도도 같은 인스턴스에 누적함. |
| 50 | public void ChargeProcessed(long bytes, CancellationToken cancellationToken) => Charge(0, bytes, 0, cancellationToken); |
| 51 | public void ReserveOutput(long bytes, CancellationToken cancellationToken) => Charge(0, 0, bytes, cancellationToken); |
| 52 | |
| 53 | private void Charge(long entryCount, long processed, long output, CancellationToken cancellationToken) |
| 54 | { |
| 55 | if (entryCount < 0 || processed < 0 || output < 0) |
| 56 | { |
| 57 | throw new ArgumentOutOfRangeException(nameof(processed)); |
| 58 | } |
| 59 | |
| 60 | lock (gate) |
| 61 | { |
| 62 | Check(cancellationToken); |
| 63 | if (entryCount > limits.MaxEntries - entries || processed > limits.MaxProcessedBytes - processedBytes || |
| 64 | output > limits.MaxOutputBytes - outputBytes) |
| 65 | { |
| 66 | Interlocked.Exchange(ref exhausted, 1); |
| 67 | throw new ArchiveException(ArchiveError.BudgetExceeded); |
| 68 | } |
| 69 | |
| 70 | entries += entryCount; |
| 71 | processedBytes += processed; |
| 72 | outputBytes += output; |
| 73 | } |
| 74 | } |
| 75 | } |