Use plan TimeoutMultiplier for ratelimit costs

This commit is contained in:
Jetsparrow 2024-04-27 18:01:01 +03:00
parent db970fe87d
commit 51d05b8104
5 changed files with 15 additions and 6 deletions

View File

@ -2,14 +2,18 @@
public class Topic public class Topic
{ {
public uint TopicId { get; set; } public uint TopicId { get; private set; }
public uint CreatorId { get; set; } public uint CreatorId { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Description { get; set; } public string Description { get; set; }
public string ReadToken { get; set; } public string ReadToken { get; set; }
public string WriteToken { get; set; } public string WriteToken { get; set; }
public NamespacedId? Sub { get; set; }
// joined
public NamespacedId? Sub { get; private set; }
public string? Login { get; private set; }
public double TimeoutMultiplier { get; private set; } = 1;
public override string ToString() public override string ToString()
=> Name == Description ? Name : $"{Name}: {Description}"; => Name == Description ? Name : $"{Name}: {Description}";

View File

@ -82,7 +82,7 @@ public class HeartbeatController : ControllerBase
if (wasBeating == 0) if (wasBeating == 0)
await Herald.BroadcastMessageRaw(t.TopicId, $"!{t.Description}!:\nHeart \"{heart}\" has started beating at {DateTime.UtcNow:O}"); await Herald.BroadcastMessageRaw(t.TopicId, $"!{t.Description}!:\nHeart \"{heart}\" has started beating at {DateTime.UtcNow:O}");
Timeouts.ApplyCost(t.TopicId, Config.HeartbeatCost); Timeouts.ApplyCost(t.TopicId, Config.HeartbeatCost * t.TimeoutMultiplier);
return new OkResult(); return new OkResult();
} }

View File

@ -68,7 +68,7 @@ public class ReportController : ControllerBase
await Herald.BroadcastMessageRaw(t.TopicId, $"|{t.Description}|:\n{args.Message}"); await Herald.BroadcastMessageRaw(t.TopicId, $"|{t.Description}|:\n{args.Message}");
Timeouts.ApplyCost(t.TopicId, Config.ReportCost); Timeouts.ApplyCost(t.TopicId, Config.ReportCost * t.TimeoutMultiplier);
return new OkResult(); return new OkResult();
} }

View File

@ -101,7 +101,11 @@ public class DbContext : IDisposable
public Task<Topic> GetTopic(string name) public Task<Topic> GetTopic(string name)
=> Tran.QuerySingleOrDefaultAsync<Topic>( => Tran.QuerySingleOrDefaultAsync<Topic>(
"SELECT * FROM topic WHERE Name = @name", @"SELECT t.*, u.Login, p.TimeoutMultiplier
FROM `topic` t
JOIN `user` u ON t.CreatorId = u.UserId
JOIN `plan` p ON u.PlanId = p.PlanId
WHERE t.Name = @name;",
new { name }); new { name });
public Task<int> DeleteTopic(string name, uint userId) public Task<int> DeleteTopic(string name, uint userId)

View File

@ -21,8 +21,9 @@ public class LeakyBucket
return time > debtLimit; return time > debtLimit;
} }
public void ApplyCost(uint key, int cost) public void ApplyCost(uint key, double cost)
{ {
if (cost <= 0) return;
expiryDates.AddOrUpdate(key, expiryDates.AddOrUpdate(key,
key => DateTime.UtcNow.AddSeconds(cost), key => DateTime.UtcNow.AddSeconds(cost),
(key, oldDebt) => (key, oldDebt) =>