Skip to content

Commit

Permalink
Added store config
Browse files Browse the repository at this point in the history
  • Loading branch information
dliocode committed Dec 24, 2020
1 parent e6658cd commit 30e0f94
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 137 deletions.
2 changes: 1 addition & 1 deletion HorseSlowDown.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<DCC_Namespace>System;Xml;Data;Datasnap;Web;Soap;$(DCC_Namespace)</DCC_Namespace>
<DCC_CBuilderOutput>All</DCC_CBuilderOutput>
<SanitizedProjectName>HorseSlowDown</SanitizedProjectName>
<DCC_UnitSearchPath>modules\.dcp;modules\.dcu;modules;modules\delphiredisclient\samples\02JobQueue;modules\delphiredisclient\samples\03PubSub;modules\delphiredisclient\samples\99FireMonkeySample;modules\delphiredisclient\samples\Commons;modules\delphiredisclient\sources;modules\delphiredisclient\tests;modules\horse\src;modules\horse-utils-clientip\src;modules\store\src;$(DCC_UnitSearchPath)</DCC_UnitSearchPath>
<DCC_UnitSearchPath>$(DCC_UnitSearchPath);modules\.dcp;modules\.dcu;modules;modules\delphiredisclient\samples\02JobQueue;modules\delphiredisclient\samples\03PubSub;modules\delphiredisclient\samples\99FireMonkeySample;modules\delphiredisclient\samples\Commons;modules\delphiredisclient\sources;modules\delphiredisclient\tests;modules\horse\src;modules\horse-utils-clientip\src;modules\store\src</DCC_UnitSearchPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Base_Win32)'!=''">
<DCC_Namespace>Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace)</DCC_Namespace>
Expand Down
8 changes: 4 additions & 4 deletions boss-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
},
"https://github.com/dliocode/horse-utils-clientip": {
"name": "horse-utils-clientip",
"version": "0.0.3",
"hash": "3e42ee357fc6d1b958c862b7a0918bac",
"version": "0.0.5",
"hash": "e70e774b0e0e7248d1e9028fc9c4542e",
"artifacts": {},
"failed": false,
"changed": false
},
"https://github.com/dliocode/store": {
"name": "store",
"version": "^2.0.1",
"hash": "ed6443c4fac6bb9f67215248b3a85f11",
"version": "2.0.2",
"hash": "e761bc2e0bf482271b9b8e9f8f133c35",
"artifacts": {},
"failed": false,
"changed": false
Expand Down
116 changes: 0 additions & 116 deletions src/Horse.SlowDown.Config.pas

This file was deleted.

37 changes: 21 additions & 16 deletions src/Horse.SlowDown.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
interface

uses
Horse,
Horse.SlowDown.Config, Horse.Utils.ClientIP,
Store.Intf, Store.Memory,
Horse, Horse.Utils.ClientIP,
Store.Intf, Store.Memory, Store.Config,
System.SysUtils, System.Math, System.SyncObjs, System.Classes,
Web.HTTPApp;

Expand All @@ -15,7 +14,14 @@ interface
DEFAULT_TIMEOUT = 60;

type
TSlowDownConfig = Horse.SlowDown.Config.TSlowDownConfig;
TSlowDownConfig = record
Id: string;
DelayAfter: Integer;
DelayMs: Integer;
MaxDelayMs: Integer;
Timeout: Integer;
Store: IStore;
end;

THorseSlowDown = class
private
Expand All @@ -31,38 +37,37 @@ implementation

class function THorseSlowDown.New(const AConfig: TSlowDownConfig): THorseCallback;
var
FManagerConfig: TSlowDownManager;
LStoreConfig: TStoreConfig<TSlowDownConfig>;
LConfig: TSlowDownConfig;
begin
CriticalSection.Enter;
try
FManagerConfig := TSlowDownManager.New(AConfig);
LStoreConfig := TStoreConfig<TSlowDownConfig>.New(AConfig.Id, AConfig);
finally
CriticalSection.Leave;
end;

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

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

FManagerConfig.Config.Store.SetTimeout(FManagerConfig.Config.Timeout);

FManagerConfig.Save;
LStoreConfig.Config.Store.SetTimeout(LStoreConfig.Config.Timeout);
LStoreConfig.Save(LStoreConfig.Config.Id);

Result := procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
var
LManagerConfig: TSlowDownManager;
LManagerConfig: TStoreConfig<TSlowDownConfig>;
LStoreCallback: TStoreCallback;
LKey: string;
LTimeSleep: Int64;
begin
CriticalSection.Enter;
try
LManagerConfig := TSlowDownManager.New(AConfig);
LManagerConfig := TStoreConfig<TSlowDownConfig>.New(AConfig.Id, AConfig);
finally
CriticalSection.Leave;
end;
Expand All @@ -85,7 +90,7 @@ class function THorseSlowDown.New(const AConfig: TSlowDownConfig): THorseCallbac
try
Next;
finally
LManagerConfig.Save;
LManagerConfig.Save(LManagerConfig.Config.Id);
end;
end;
end;
Expand All @@ -112,4 +117,4 @@ finalization

FreeAndNil(THorseSlowDown.CriticalSection);

end.
end.

0 comments on commit 30e0f94

Please sign in to comment.