Skip to content

Commit

Permalink
fix: make AniDB api calls more preservative
Browse files Browse the repository at this point in the history
after the initial burst of commands to hopefully prevent API bans.

The long delay for UDP has been changed from two times the short delay to three times (4s → 6s), and HTTP has been changed from two times to fifteen times (4s → 30s). To compensate, then the reset period is down from 3m to 2m.

If these numbers are too preservative then we can tweak them again.

Related to #1182
  • Loading branch information
revam committed Oct 30, 2024
1 parent c3cce2f commit 2e08d8c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
14 changes: 7 additions & 7 deletions Shoko.Server/Providers/AniDB/AniDBRateLimiter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.Threading;
using Microsoft.Extensions.Logging;
Expand All @@ -22,11 +22,11 @@ public abstract class AniDBRateLimiter
// An extended amount of time is not defined. Use common sense.
protected abstract int LongDelay { get; init; }

// Switch to longer delay after 1 hour
protected abstract long shortPeriod { get; init; }
// Switch to longer delay after a short period
protected abstract long ShortPeriod { get; init; }

// Switch to shorter delay after 30 minutes of inactivity
protected abstract long resetPeriod { get; init; }
// Switch to shorter delay after inactivity
protected abstract long ResetPeriod { get; init; }

protected AniDBRateLimiter(ILogger logger)
{
Expand All @@ -51,8 +51,8 @@ public T EnsureRate<T>(Func<T> action)
if (!entered) throw new SynchronizationLockException();

var delay = _requestWatch.ElapsedMilliseconds;
if (delay > resetPeriod) ResetRate();
var currentDelay = _activeTimeWatch.ElapsedMilliseconds > shortPeriod ? LongDelay : ShortDelay;
if (delay > ResetPeriod) ResetRate();
var currentDelay = _activeTimeWatch.ElapsedMilliseconds > ShortPeriod ? LongDelay : ShortDelay;

if (delay > currentDelay)
{
Expand Down
8 changes: 4 additions & 4 deletions Shoko.Server/Providers/AniDB/HTTP/HttpRateLimiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace Shoko.Server.Providers.AniDB.HTTP;

public class HttpRateLimiter : AniDBRateLimiter
{
protected override int ShortDelay { get; init; } = 2000;
protected override int LongDelay { get; init; } = 4000;
protected override long shortPeriod { get; init; } = 1000000;
protected override long resetPeriod { get; init; } = 1800000;
protected override int ShortDelay { get; init; } = 2_000;
protected override int LongDelay { get; init; } = 30_000;
protected override long ShortPeriod { get; init; } = 10_000;
protected override long ResetPeriod { get; init; } = 120_000;

public HttpRateLimiter(ILogger<HttpRateLimiter> logger) : base(logger)
{
Expand Down
8 changes: 4 additions & 4 deletions Shoko.Server/Providers/AniDB/UDP/UDPRateLimiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace Shoko.Server.Providers.AniDB.UDP;

public class UDPRateLimiter : AniDBRateLimiter
{
protected override int ShortDelay { get; init; } = 2000;
protected override int LongDelay { get; init; } = 4000;
protected override long shortPeriod { get; init; } = 3600000;
protected override long resetPeriod { get; init; } = 1800000;
protected override int ShortDelay { get; init; } = 2_000;
protected override int LongDelay { get; init; } = 6_000;
protected override long ShortPeriod { get; init; } = 10_000;
protected override long ResetPeriod { get; init; } = 120_000;

public UDPRateLimiter(ILogger<UDPRateLimiter> logger) : base(logger)
{
Expand Down

0 comments on commit 2e08d8c

Please sign in to comment.