forked from exilon/QuickLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick.FaultControl.pas
226 lines (195 loc) · 6.52 KB
/
Quick.FaultControl.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
{ ***************************************************************************
Copyright (c) 2016-2019 Kike Pérez
Unit : Quick.FaultControl
Description : Thread workers retry and circuit break
Author : Kike Pérez
Version : 1.5
Created : 20/06/2019
Modified : 02/12/2019
This file is part of QuickLib: https://github.com/exilon/QuickLib
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.FaultControl;
{$i QuickLib.inc}
interface
uses
SysUtils,
SyncObjs,
Quick.Commons;
type
TRetryEvent = procedure(aRaisedException : Exception; var vStopRetries : Boolean) of object;
TMaxRetriesEvent = procedure of object;
TCircuitBreakEvent = procedure of object;
TFaultPolicy = class
private
fMaxRetries : Integer;
fWaitTimeBetweenRetries : Integer;
fWaitTimeMultiplierFactor : Double;
public
constructor Create;
property MaxRetries : Integer read fMaxRetries write fMaxRetries;
property WaitTimeBetweenRetries : Integer read fWaitTimeBetweenRetries write fWaitTimeBetweenRetries;
property WaitTimeMultiplierFactor : Double read fWaitTimeMultiplierFactor write fWaitTimeMultiplierFactor;
end;
TFaultControl = class
private
fMaxRetries : Integer;
fWaitTimeBetweenRetries : Integer;
fWaitTimeMultiplierFactor : Double;
fWaitTimeArray : TArray<Integer>;
fNumRetries : Integer;
fLastException : Exception;
fCircuitBreaked : Boolean;
fOnRetry : TRetryEvent;
fOnCircuitBreak : TCircuitBreakEvent;
fTaskFailed : Boolean;
procedure WaitBeforeRetry(aNumWaitMilliseconds : Integer);
procedure SetWaitTimeMultiplierFactor(const Value: Double);
public
constructor Create;
destructor Destroy; override;
property MaxRetries : Integer read fMaxRetries write fMaxRetries;
property WaitTimeBetweenRetriesMS : Integer read fWaitTimeBetweenRetries write fWaitTimeBetweenRetries;
property WaitTimeMultiplierFactor : Double read fWaitTimeMultiplierFactor write SetWaitTimeMultiplierFactor;
property WaitTimeMSArray : TArray<Integer> read fWaitTimeArray write fWaitTimeArray;
property OnRetry : TRetryEvent read fOnRetry write fOnRetry;
property OnCircuitBreak : TCircuitBreakEvent read fOnCircuitBreak write fOnCircuitBreak;
property TaskFailed : Boolean read fTaskFailed;
property CircuitBreaked : Boolean read fCircuitBreaked;
property LastException : Exception read fLastException;
property NumRetries : Integer read fNumRetries;
procedure FailedExecution(aException : Exception);
procedure SuccessExecution;
procedure Reset;
function NeedToRetry : Boolean;
end;
EFaultControlConfigError = class(Exception);
implementation
{ TFaultControl }
constructor TFaultControl.Create;
begin
fTaskFailed := False;
fLastException := nil;
fOnRetry := nil;
fOnCircuitBreak := nil;
fCircuitBreaked := False;
fNumRetries := 0;
fMaxRetries := 0;
fWaitTimeBetweenRetries := 0;
fWaitTimeMultiplierFactor := 1;
{$IFDEF DELPHIXE7_UP}
fWaitTimeArray := [];
{$ELSE}
fWaitTimeArray := nil;
{$ENDIF}
end;
function TFaultControl.NeedToRetry: Boolean;
var
waitretryMS : Integer;
begin
Result := False;
if fTaskFailed then
begin
if (fMaxRetries <> 0) and (not fCircuitBreaked) then
begin
if (fNumRetries < fMaxRetries) or (fMaxRetries = -1) then
begin
Inc(fNumRetries);
if Assigned(fOnRetry) then
begin
//can cancel next retries
fOnRetry(fLastException,fCircuitBreaked);
//if cancelled next retries, decrease current retries
if fCircuitBreaked then fNumRetries := fNumRetries - 1;
Result := not fCircuitBreaked;
end
else Result := True;
//wait between retries
if (Result) and (fMaxRetries <> 0) then
begin
if IsEmptyArray(fWaitTimeArray) then
begin
if fWaitTimeMultiplierFactor = 1 then waitretryMS := fWaitTimeBetweenRetries
else waitretryMS := fWaitTimeBetweenRetries * Round(fNumRetries * fWaitTimeMultiplierFactor);
end
else waitretryMS := fWaitTimeArray[fNumRetries - 1];
if waitretryMS > 0 then WaitBeforeRetry(waitretryMS);
end;
end
else fCircuitBreaked := True;
if fCircuitBreaked then
begin
if Assigned(fOnCircuitBreak) then fOnCircuitBreak;
if Assigned(fLastException) then raise fLastException;
end;
end;
end;
end;
procedure TFaultControl.Reset;
begin
fCircuitBreaked := False;
fTaskFailed := False;
fNumRetries := 0;
fLastException := nil;
end;
procedure TFaultControl.SetWaitTimeMultiplierFactor(const Value: Double);
begin
if Value = 0 then raise EFaultControlConfigError.Create('WaitTimeMultiplierFactor cannot be 0')
else fWaitTimeMultiplierFactor := Value;
end;
procedure TFaultControl.SuccessExecution;
begin
fTaskFailed := False;
end;
procedure TFaultControl.WaitBeforeRetry(aNumWaitMilliseconds: Integer);
var
fEvent : TSimpleEvent;
begin
//Sleep(aNumWaitMilliseconds);
{$IFDEF FPC}
fEvent := TSimpleEvent.Create;
{$ELSE}
fEvent := TSimpleEvent.Create(nil,True,False,'');
{$ENDIF}
try
fEvent.WaitFor(aNumWaitMilliseconds);
finally
fEvent.SetEvent;
fEvent.Free;
end;
end;
destructor TFaultControl.Destroy;
begin
//if Assigned(fLastException) then fLastException.Free;
inherited;
end;
procedure TFaultControl.FailedExecution(aException: Exception);
begin
fTaskFailed := True;
//free older exception
if Assigned(fLastException) then fLastException.Free;
fLastException := aException;
if fMaxRetries = 0 then raise aException
else if fNumRetries = fMaxRetries then
begin
aException.Message := Format('Max %d retries reached: %s',[fMaxRetries,aException.Message]);
raise fLastException;
end;
end;
{ TFaultPolicy }
constructor TFaultPolicy.Create;
begin
fMaxRetries := 0;
fWaitTimeBetweenRetries := 0;
fWaitTimeMultiplierFactor := 1;
end;
end.