Skip to content

Commit

Permalink
Compatibility Adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
dliocode committed May 26, 2021
1 parent 5bc328f commit 25f5997
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/Horse.RateLimit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,24 @@ TRateLimitOptions = record

var
LStoreConfig: TStoreConfig<TRateLimitConfig>;
LConfigStore: TRateLimitConfig;
LConfig: TRateLimitConfig;
begin
LConfig := AConfig;

CriticalSection.Enter;
try
LStoreConfig := TStoreConfig<TRateLimitConfig>.New(AConfig.Id, AConfig);
LStoreConfig := TStoreConfig<TRateLimitConfig>.New(LConfig.Id, LConfig);
finally
CriticalSection.Leave;
end;

if not(Assigned(LStoreConfig.Config.Store)) then
begin
LConfig := LStoreConfig.Config;
LConfig.Store := TMemoryStore.New();
LConfigStore := LStoreConfig.Config;
LConfigStore.Store := TMemoryStore.New();

LStoreConfig.Config := LConfig;
LStoreConfig.Config := LConfigStore;
end;

LStoreConfig.Config.Store.SetTimeout(LStoreConfig.Config.Timeout);
Expand All @@ -84,7 +87,7 @@ TRateLimitOptions = record
begin
CriticalSection.Enter;
try
LManagerConfig := TStoreConfig<TRateLimitConfig>.New(AConfig.Id, AConfig);
LManagerConfig := TStoreConfig<TRateLimitConfig>.New(LConfig.Id, LConfig);
finally
CriticalSection.Leave;
end;
Expand All @@ -105,15 +108,15 @@ TRateLimitOptions = record

if (FOptions.Headers) then
begin
LWebResponse := THorseHackResponse(Res).GetWebResponse;
LWebResponse := Res.RawWebResponse;
LWebResponse.SetCustomHeader('X-RateLimit-Limit', FOptions.Limit.ToString);
LWebResponse.SetCustomHeader('X-RateLimit-Remaining', FOptions.Remaining.ToString);
LWebResponse.SetCustomHeader('X-RateLimit-Reset', IntToStr(MillisecondOfTheDay(FOptions.ResetTime)));
end;

if (FOptions.Current > FOptions.Limit) then
begin
THorseHackResponse(Res).GetWebResponse.SetCustomHeader('Retry-After', IntToStr(FOptions.Timeout * 1000));
Res.RawWebResponse.SetCustomHeader('Retry-After', IntToStr(FOptions.Timeout * 1000));

LMessage := 'Too many requests, please try again later.';
LMessage := Ifthen(FOptions.Message.Trim.IsEmpty, LMessage, FOptions.Message);
Expand All @@ -132,10 +135,10 @@ TRateLimitOptions = record
raise;
end;

if (FOptions.SkipFailedRequest) and (THorseHackResponse(Req).GetWebResponse.StatusCode >= 400) then
if (FOptions.SkipFailedRequest) and (Res.RawWebResponse.StatusCode >= 400) then
LManagerConfig.Config.Store.Decrement(LKey);

if (FOptions.SkipSuccessRequest) and (THorseHackResponse(Req).GetWebResponse.StatusCode < 400) then
if (FOptions.SkipSuccessRequest) and (Res.RawWebResponse.StatusCode < 400) then
LManagerConfig.Config.Store.Decrement(LKey);
finally
LManagerConfig.Save(LManagerConfig.Config.Id);
Expand Down Expand Up @@ -167,4 +170,4 @@ finalization

FreeAndNil(THorseRateLimit.CriticalSection);

end.
end.

0 comments on commit 25f5997

Please sign in to comment.