brass-runtime 1.14.0 → 1.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -3
- package/dist/agent/cli/main.cjs +44 -43
- package/dist/agent/cli/main.js +5 -4
- package/dist/agent/cli/main.mjs +5 -4
- package/dist/agent/index.cjs +4 -3
- package/dist/agent/index.d.ts +1 -1
- package/dist/agent/index.js +3 -2
- package/dist/agent/index.mjs +3 -2
- package/dist/{chunk-WJESVBWN.js → chunk-3QMOKAS5.js} +9 -7
- package/dist/{chunk-BMRF4FN6.js → chunk-4NHES7VK.mjs} +59 -237
- package/dist/chunk-AR22SXML.js +1043 -0
- package/dist/{chunk-4N2JEK4H.mjs → chunk-BDF4AMWX.mjs} +27 -151
- package/dist/chunk-BDYEENHT.js +224 -0
- package/dist/{chunk-JT7D6M5H.js → chunk-BMH5AV44.js} +27 -151
- package/dist/chunk-ELOOF35R.mjs +131 -0
- package/dist/chunk-JFPU5GQI.mjs +1043 -0
- package/dist/{chunk-MQF7HZ7Y.mjs → chunk-K6M7MDZ4.mjs} +9 -7
- package/dist/chunk-MS34J5LY.cjs +224 -0
- package/dist/{chunk-UWMMYKVK.mjs → chunk-PPUXIH5R.js} +59 -237
- package/dist/chunk-R3R2FVLG.cjs +131 -0
- package/dist/chunk-STVLQ3XD.cjs +489 -0
- package/dist/{chunk-BKBFSOGT.cjs → chunk-TGIFUAK4.cjs} +26 -150
- package/dist/chunk-TO7IKXYT.js +131 -0
- package/dist/chunk-UMAZLXAB.mjs +224 -0
- package/dist/{chunk-XTMZTVIT.cjs → chunk-VEZNF5GZ.cjs} +136 -134
- package/dist/chunk-XPZNXSVN.cjs +1043 -0
- package/dist/core/index.cjs +216 -0
- package/dist/core/index.d.ts +673 -0
- package/dist/core/index.js +216 -0
- package/dist/core/index.mjs +216 -0
- package/dist/{effect-DM56H743.d.ts → effect-CMOQKX8y.d.ts} +12 -11
- package/dist/http/index.cjs +2557 -235
- package/dist/http/index.d.ts +1514 -4
- package/dist/http/index.js +2549 -227
- package/dist/http/index.mjs +2549 -227
- package/dist/index.cjs +237 -1168
- package/dist/index.d.ts +7 -673
- package/dist/index.js +77 -1008
- package/dist/index.mjs +77 -1008
- package/dist/stream-FQm9h4Mg.d.ts +74 -0
- package/dist/tracing-DNT9jEbr.d.ts +106 -0
- package/package.json +11 -3
- package/dist/chunk-SKVY72E5.cjs +0 -667
- package/dist/stream-Oqe6WeLE.d.ts +0 -173
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
+
});
|
|
7
|
+
|
|
1
8
|
// src/core/types/asyncEffect.ts
|
|
2
9
|
var Async = {
|
|
3
10
|
succeed: (value) => ({ _tag: "Succeed", value }),
|
|
@@ -6,21 +13,18 @@ var Async = {
|
|
|
6
13
|
async: (register) => ({ _tag: "Async", register })
|
|
7
14
|
};
|
|
8
15
|
function asyncFold(fa, onFailure, onSuccess) {
|
|
9
|
-
return {
|
|
16
|
+
return {
|
|
17
|
+
_tag: "Fold",
|
|
18
|
+
first: fa,
|
|
19
|
+
onFailure,
|
|
20
|
+
onSuccess
|
|
21
|
+
};
|
|
10
22
|
}
|
|
11
23
|
function asyncCatchAll(fa, handler) {
|
|
12
|
-
return asyncFold(
|
|
13
|
-
fa,
|
|
14
|
-
(e) => handler(e),
|
|
15
|
-
(a) => asyncSucceed(a)
|
|
16
|
-
);
|
|
24
|
+
return asyncFold(fa, handler, asyncSucceed);
|
|
17
25
|
}
|
|
18
26
|
function asyncMapError(fa, f) {
|
|
19
|
-
return asyncFold(
|
|
20
|
-
fa,
|
|
21
|
-
(e) => asyncFail(f(e)),
|
|
22
|
-
(a) => asyncSucceed(a)
|
|
23
|
-
);
|
|
27
|
+
return asyncFold(fa, (e) => asyncFail(f(e)), asyncSucceed);
|
|
24
28
|
}
|
|
25
29
|
var SUCCEED_UNDEFINED = { _tag: "Succeed", value: void 0 };
|
|
26
30
|
var SUCCEED_TRUE = { _tag: "Succeed", value: true };
|
|
@@ -43,7 +47,7 @@ var asyncSync = (thunk) => ({
|
|
|
43
47
|
thunk
|
|
44
48
|
});
|
|
45
49
|
var asyncTotal = (thunk) => asyncSync(() => thunk());
|
|
46
|
-
var
|
|
50
|
+
var asyncEffect = (register) => ({
|
|
47
51
|
_tag: "Async",
|
|
48
52
|
register
|
|
49
53
|
});
|
|
@@ -64,7 +68,7 @@ function acquireRelease(acquire, release, scope) {
|
|
|
64
68
|
});
|
|
65
69
|
}
|
|
66
70
|
function asyncInterruptible(register) {
|
|
67
|
-
return
|
|
71
|
+
return asyncEffect(register);
|
|
68
72
|
}
|
|
69
73
|
var withAsyncPromise = (run) => (eff) => {
|
|
70
74
|
const anyEff = eff;
|
|
@@ -101,18 +105,14 @@ var succeed = (value) => asyncSucceed(value);
|
|
|
101
105
|
var fail = (error) => asyncFail(error);
|
|
102
106
|
var sync = (thunk) => asyncSync((env) => thunk(env));
|
|
103
107
|
var map = (fa, f) => asyncMap(fa, f);
|
|
104
|
-
var flatMap = (fa, f) => asyncFlatMap(fa,
|
|
108
|
+
var flatMap = (fa, f) => asyncFlatMap(fa, f);
|
|
105
109
|
var mapError = (fa, f) => asyncMapError(fa, f);
|
|
106
|
-
var catchAll = (fa, handler) => asyncFold(
|
|
107
|
-
fa,
|
|
108
|
-
(e) => handler(e),
|
|
109
|
-
(a) => asyncSucceed(a)
|
|
110
|
-
);
|
|
110
|
+
var catchAll = (fa, handler) => asyncFold(fa, handler, asyncSucceed);
|
|
111
111
|
function orElseOptional(fa, that) {
|
|
112
112
|
return asyncFold(
|
|
113
113
|
fa,
|
|
114
114
|
(opt) => opt._tag === "Some" ? asyncFail(opt) : that(),
|
|
115
|
-
|
|
115
|
+
asyncSucceed
|
|
116
116
|
);
|
|
117
117
|
}
|
|
118
118
|
var end = () => fail(none);
|
|
@@ -2797,7 +2797,7 @@ var Runtime = class _Runtime {
|
|
|
2797
2797
|
});
|
|
2798
2798
|
}
|
|
2799
2799
|
delay(ms, eff) {
|
|
2800
|
-
return
|
|
2800
|
+
return asyncEffect((_env, cb) => {
|
|
2801
2801
|
const handle = setTimeout(() => {
|
|
2802
2802
|
this.unsafeRunAsync(eff, cb);
|
|
2803
2803
|
}, ms);
|
|
@@ -3517,7 +3517,7 @@ var JsFiberEngine = class {
|
|
|
3517
3517
|
// src/core/runtime/scope.ts
|
|
3518
3518
|
var nextScopeId = 1;
|
|
3519
3519
|
function awaitAll(fibers) {
|
|
3520
|
-
return
|
|
3520
|
+
return asyncEffect((_env, cb) => {
|
|
3521
3521
|
let remaining = fibers.length;
|
|
3522
3522
|
if (remaining === 0) {
|
|
3523
3523
|
cb({ _tag: "Success", value: void 0 });
|
|
@@ -3628,7 +3628,7 @@ var Scope = class _Scope {
|
|
|
3628
3628
|
closeAsync(exit = { _tag: "Success", value: void 0 }, opts = { awaitChildren: true }) {
|
|
3629
3629
|
return asyncFlatMap(
|
|
3630
3630
|
unit(),
|
|
3631
|
-
() =>
|
|
3631
|
+
() => asyncEffect((env, cb) => {
|
|
3632
3632
|
if (this.closed) {
|
|
3633
3633
|
cb({ _tag: "Success", value: void 0 });
|
|
3634
3634
|
return;
|
|
@@ -3663,7 +3663,7 @@ var Scope = class _Scope {
|
|
|
3663
3663
|
}
|
|
3664
3664
|
};
|
|
3665
3665
|
function withScopeAsync(runtime, f) {
|
|
3666
|
-
return
|
|
3666
|
+
return asyncEffect((_env, cb) => {
|
|
3667
3667
|
const scope = new Scope(runtime);
|
|
3668
3668
|
let done = false;
|
|
3669
3669
|
const completeAfterClose = (exit) => {
|
|
@@ -3690,128 +3690,8 @@ function withScope(runtime, f) {
|
|
|
3690
3690
|
});
|
|
3691
3691
|
}
|
|
3692
3692
|
|
|
3693
|
-
// src/core/stream/structuredConcurrency.ts
|
|
3694
|
-
function race(left, right, parentScope) {
|
|
3695
|
-
return async((env, cb) => {
|
|
3696
|
-
const scope = parentScope.subScope();
|
|
3697
|
-
let done = false;
|
|
3698
|
-
const onResult = (exit) => {
|
|
3699
|
-
if (done) return;
|
|
3700
|
-
done = true;
|
|
3701
|
-
scope.close(exit);
|
|
3702
|
-
cb(exit);
|
|
3703
|
-
};
|
|
3704
|
-
const fiberLeft = scope.fork(left);
|
|
3705
|
-
const fiberRight = scope.fork(right);
|
|
3706
|
-
fiberLeft.join(onResult);
|
|
3707
|
-
fiberRight.join(onResult);
|
|
3708
|
-
});
|
|
3709
|
-
}
|
|
3710
|
-
function zipPar(left, right, parentScope) {
|
|
3711
|
-
return async((env, cb) => {
|
|
3712
|
-
const scope = parentScope.subScope();
|
|
3713
|
-
let leftExit = null;
|
|
3714
|
-
let rightExit = null;
|
|
3715
|
-
let done = false;
|
|
3716
|
-
const checkDone = () => {
|
|
3717
|
-
if (!leftExit || !rightExit || done) return;
|
|
3718
|
-
done = true;
|
|
3719
|
-
if (leftExit._tag === "Success" && rightExit._tag === "Success") {
|
|
3720
|
-
scope.close({ _tag: "Success", value: void 0 });
|
|
3721
|
-
cb({
|
|
3722
|
-
_tag: "Success",
|
|
3723
|
-
value: [leftExit.value, rightExit.value]
|
|
3724
|
-
});
|
|
3725
|
-
return;
|
|
3726
|
-
}
|
|
3727
|
-
let cause;
|
|
3728
|
-
if (leftExit._tag === "Failure") {
|
|
3729
|
-
cause = leftExit.cause;
|
|
3730
|
-
} else if (rightExit._tag === "Failure") {
|
|
3731
|
-
cause = rightExit.cause;
|
|
3732
|
-
} else {
|
|
3733
|
-
throw new Error("zipPar: unreachable state (no Failure exit)");
|
|
3734
|
-
}
|
|
3735
|
-
const errExit = {
|
|
3736
|
-
_tag: "Failure",
|
|
3737
|
-
cause
|
|
3738
|
-
};
|
|
3739
|
-
scope.close(errExit);
|
|
3740
|
-
cb(errExit);
|
|
3741
|
-
};
|
|
3742
|
-
const f1 = scope.fork(left);
|
|
3743
|
-
const f2 = scope.fork(right);
|
|
3744
|
-
f1.join((exit) => {
|
|
3745
|
-
leftExit = exit;
|
|
3746
|
-
checkDone();
|
|
3747
|
-
});
|
|
3748
|
-
f2.join((exit) => {
|
|
3749
|
-
rightExit = exit;
|
|
3750
|
-
checkDone();
|
|
3751
|
-
});
|
|
3752
|
-
});
|
|
3753
|
-
}
|
|
3754
|
-
function collectAllPar(effects, parentScope) {
|
|
3755
|
-
return async((env, cb) => {
|
|
3756
|
-
const scope = parentScope.subScope();
|
|
3757
|
-
const results = new Array(effects.length);
|
|
3758
|
-
let completed = 0;
|
|
3759
|
-
let done = false;
|
|
3760
|
-
effects.forEach((eff, i) => {
|
|
3761
|
-
const f = scope.fork(eff);
|
|
3762
|
-
f.join((exit) => {
|
|
3763
|
-
if (done) return;
|
|
3764
|
-
if (exit._tag === "Failure") {
|
|
3765
|
-
done = true;
|
|
3766
|
-
const errExit = {
|
|
3767
|
-
_tag: "Failure",
|
|
3768
|
-
cause: exit.cause
|
|
3769
|
-
};
|
|
3770
|
-
scope.close(errExit);
|
|
3771
|
-
cb(errExit);
|
|
3772
|
-
return;
|
|
3773
|
-
}
|
|
3774
|
-
results[i] = exit.value;
|
|
3775
|
-
completed++;
|
|
3776
|
-
if (completed === effects.length) {
|
|
3777
|
-
done = true;
|
|
3778
|
-
const successExit = {
|
|
3779
|
-
_tag: "Success",
|
|
3780
|
-
value: results
|
|
3781
|
-
};
|
|
3782
|
-
scope.close({ _tag: "Success", value: void 0 });
|
|
3783
|
-
cb(successExit);
|
|
3784
|
-
}
|
|
3785
|
-
});
|
|
3786
|
-
});
|
|
3787
|
-
});
|
|
3788
|
-
}
|
|
3789
|
-
function raceWith(left, right, parentScope, onLeft, onRight) {
|
|
3790
|
-
return async((env, cb) => {
|
|
3791
|
-
const scope = parentScope.subScope();
|
|
3792
|
-
let done = false;
|
|
3793
|
-
const fiberLeft = scope.fork(left);
|
|
3794
|
-
const fiberRight = scope.fork(right);
|
|
3795
|
-
const finish = (next) => {
|
|
3796
|
-
scope.fork(next).join((exitNext) => {
|
|
3797
|
-
scope.close(exitNext);
|
|
3798
|
-
cb(exitNext);
|
|
3799
|
-
});
|
|
3800
|
-
};
|
|
3801
|
-
fiberLeft.join((exitL) => {
|
|
3802
|
-
if (done) return;
|
|
3803
|
-
done = true;
|
|
3804
|
-
finish(onLeft(exitL, fiberRight, scope));
|
|
3805
|
-
});
|
|
3806
|
-
fiberRight.join((exitR) => {
|
|
3807
|
-
if (done) return;
|
|
3808
|
-
done = true;
|
|
3809
|
-
finish(onRight(exitR, fiberLeft, scope));
|
|
3810
|
-
});
|
|
3811
|
-
});
|
|
3812
|
-
}
|
|
3813
|
-
|
|
3814
3693
|
export {
|
|
3694
|
+
__require,
|
|
3815
3695
|
Async,
|
|
3816
3696
|
asyncFold,
|
|
3817
3697
|
asyncCatchAll,
|
|
@@ -3821,7 +3701,7 @@ export {
|
|
|
3821
3701
|
asyncFail,
|
|
3822
3702
|
asyncSync,
|
|
3823
3703
|
asyncTotal,
|
|
3824
|
-
|
|
3704
|
+
asyncEffect,
|
|
3825
3705
|
asyncMap,
|
|
3826
3706
|
asyncFlatMap,
|
|
3827
3707
|
acquireRelease,
|
|
@@ -3889,9 +3769,5 @@ export {
|
|
|
3889
3769
|
withCurrentFiber,
|
|
3890
3770
|
Scope,
|
|
3891
3771
|
withScopeAsync,
|
|
3892
|
-
withScope
|
|
3893
|
-
race,
|
|
3894
|
-
zipPar,
|
|
3895
|
-
collectAllPar,
|
|
3896
|
-
raceWith
|
|
3772
|
+
withScope
|
|
3897
3773
|
};
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import {
|
|
2
|
+
asyncEffect,
|
|
3
|
+
asyncFail,
|
|
4
|
+
asyncFlatMap,
|
|
5
|
+
asyncFold,
|
|
6
|
+
asyncSucceed,
|
|
7
|
+
unsafeGetCurrentRuntime
|
|
8
|
+
} from "./chunk-BMH5AV44.js";
|
|
9
|
+
|
|
10
|
+
// src/core/runtime/combinators.ts
|
|
11
|
+
function sleep(ms) {
|
|
12
|
+
return asyncEffect((_env, cb) => {
|
|
13
|
+
const delay = Math.max(0, Math.floor(ms));
|
|
14
|
+
const id = setTimeout(() => cb({ _tag: "Success", value: void 0 }), delay);
|
|
15
|
+
return () => clearTimeout(id);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
function timeout(effect, ms) {
|
|
19
|
+
return asyncEffect((env, cb) => {
|
|
20
|
+
let done = false;
|
|
21
|
+
let timerId;
|
|
22
|
+
let effectRunning = true;
|
|
23
|
+
timerId = setTimeout(() => {
|
|
24
|
+
if (done) return;
|
|
25
|
+
done = true;
|
|
26
|
+
effectRunning = false;
|
|
27
|
+
cb({
|
|
28
|
+
_tag: "Failure",
|
|
29
|
+
cause: { _tag: "Fail", error: { _tag: "TimeoutError", ms } }
|
|
30
|
+
});
|
|
31
|
+
}, Math.max(0, Math.floor(ms)));
|
|
32
|
+
const runtime = unsafeGetCurrentRuntime();
|
|
33
|
+
if (runtime) {
|
|
34
|
+
const fiber = runtime.fork(effect);
|
|
35
|
+
fiber.join((exit) => {
|
|
36
|
+
if (done) return;
|
|
37
|
+
done = true;
|
|
38
|
+
clearTimeout(timerId);
|
|
39
|
+
cb(exit);
|
|
40
|
+
});
|
|
41
|
+
return () => {
|
|
42
|
+
if (done) return;
|
|
43
|
+
done = true;
|
|
44
|
+
clearTimeout(timerId);
|
|
45
|
+
fiber.interrupt();
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
return () => {
|
|
49
|
+
if (done) return;
|
|
50
|
+
done = true;
|
|
51
|
+
clearTimeout(timerId);
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
function retry(effect, policy) {
|
|
56
|
+
const shouldRetry = policy.shouldRetry ?? (() => true);
|
|
57
|
+
const jitter = policy.jitter ?? "full";
|
|
58
|
+
const maxElapsedMs = policy.maxElapsedMs;
|
|
59
|
+
const computeDelay = (attempt) => {
|
|
60
|
+
const exp = policy.baseDelayMs * Math.pow(2, attempt);
|
|
61
|
+
const capped = Math.min(exp, policy.maxDelayMs);
|
|
62
|
+
if (jitter === "none") return capped;
|
|
63
|
+
return Math.floor(Math.random() * capped);
|
|
64
|
+
};
|
|
65
|
+
const loop = (attempt, startedAt) => asyncFold(
|
|
66
|
+
effect,
|
|
67
|
+
(error) => {
|
|
68
|
+
if (attempt >= policy.maxRetries) return asyncFail(error);
|
|
69
|
+
if (!shouldRetry(error, attempt)) return asyncFail(error);
|
|
70
|
+
if (maxElapsedMs !== void 0) {
|
|
71
|
+
const elapsed = performance.now() - startedAt;
|
|
72
|
+
if (elapsed >= maxElapsedMs) return asyncFail(error);
|
|
73
|
+
}
|
|
74
|
+
const delay = computeDelay(attempt);
|
|
75
|
+
return asyncFlatMap(sleep(delay), () => loop(attempt + 1, startedAt));
|
|
76
|
+
},
|
|
77
|
+
(value) => asyncSucceed(value)
|
|
78
|
+
);
|
|
79
|
+
return asyncFlatMap(
|
|
80
|
+
{ _tag: "Sync", thunk: () => performance.now() },
|
|
81
|
+
(startedAt) => loop(0, startedAt)
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
function retryN(effect, n) {
|
|
85
|
+
return retry(effect, {
|
|
86
|
+
maxRetries: n,
|
|
87
|
+
baseDelayMs: 0,
|
|
88
|
+
maxDelayMs: 0,
|
|
89
|
+
jitter: "none"
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
function retryWithBackoff(effect, opts = {}) {
|
|
93
|
+
return retry(effect, {
|
|
94
|
+
maxRetries: opts.maxRetries ?? 3,
|
|
95
|
+
baseDelayMs: opts.baseDelayMs ?? 100,
|
|
96
|
+
maxDelayMs: opts.maxDelayMs ?? 1e4,
|
|
97
|
+
maxElapsedMs: opts.maxElapsedMs,
|
|
98
|
+
shouldRetry: opts.shouldRetry,
|
|
99
|
+
jitter: "full"
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/core/runtime/circuitBreaker.ts
|
|
104
|
+
function makeCircuitBreaker(config = {}) {
|
|
105
|
+
const failureThreshold = config.failureThreshold ?? 5;
|
|
106
|
+
const resetTimeoutMs = config.resetTimeoutMs ?? 3e4;
|
|
107
|
+
const successThreshold = config.successThreshold ?? 1;
|
|
108
|
+
const isFailure = config.isFailure ?? (() => true);
|
|
109
|
+
const onStateChange = config.onStateChange;
|
|
110
|
+
let currentState = "closed";
|
|
111
|
+
let consecutiveFailures = 0;
|
|
112
|
+
let consecutiveSuccesses = 0;
|
|
113
|
+
let openedAt = 0;
|
|
114
|
+
let totalRequests = 0;
|
|
115
|
+
let totalFailures = 0;
|
|
116
|
+
let totalSuccesses = 0;
|
|
117
|
+
let totalRejected = 0;
|
|
118
|
+
let lastFailureTime = null;
|
|
119
|
+
let lastSuccessTime = null;
|
|
120
|
+
const transition = (to) => {
|
|
121
|
+
if (currentState === to) return;
|
|
122
|
+
const from = currentState;
|
|
123
|
+
currentState = to;
|
|
124
|
+
onStateChange?.(from, to);
|
|
125
|
+
};
|
|
126
|
+
const onSuccess = () => {
|
|
127
|
+
totalSuccesses++;
|
|
128
|
+
lastSuccessTime = Date.now();
|
|
129
|
+
consecutiveFailures = 0;
|
|
130
|
+
if (currentState === "half-open") {
|
|
131
|
+
consecutiveSuccesses++;
|
|
132
|
+
if (consecutiveSuccesses >= successThreshold) {
|
|
133
|
+
consecutiveSuccesses = 0;
|
|
134
|
+
transition("closed");
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
const onFailure = (error) => {
|
|
139
|
+
if (!isFailure(error)) {
|
|
140
|
+
onSuccess();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
totalFailures++;
|
|
144
|
+
lastFailureTime = Date.now();
|
|
145
|
+
consecutiveSuccesses = 0;
|
|
146
|
+
consecutiveFailures++;
|
|
147
|
+
if (currentState === "half-open") {
|
|
148
|
+
openedAt = Date.now();
|
|
149
|
+
transition("open");
|
|
150
|
+
} else if (currentState === "closed" && consecutiveFailures >= failureThreshold) {
|
|
151
|
+
openedAt = Date.now();
|
|
152
|
+
transition("open");
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
const shouldAllow = () => {
|
|
156
|
+
switch (currentState) {
|
|
157
|
+
case "closed":
|
|
158
|
+
return true;
|
|
159
|
+
case "open": {
|
|
160
|
+
const elapsed = Date.now() - openedAt;
|
|
161
|
+
if (elapsed >= resetTimeoutMs) {
|
|
162
|
+
transition("half-open");
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
case "half-open":
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
const protect = (effect) => {
|
|
172
|
+
totalRequests++;
|
|
173
|
+
if (!shouldAllow()) {
|
|
174
|
+
totalRejected++;
|
|
175
|
+
return asyncFail({
|
|
176
|
+
_tag: "CircuitBreakerOpen",
|
|
177
|
+
openSince: openedAt,
|
|
178
|
+
failures: consecutiveFailures
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return asyncFold(
|
|
182
|
+
effect,
|
|
183
|
+
(error) => {
|
|
184
|
+
onFailure(error);
|
|
185
|
+
return asyncFail(error);
|
|
186
|
+
},
|
|
187
|
+
(value) => {
|
|
188
|
+
onSuccess();
|
|
189
|
+
return asyncSucceed(value);
|
|
190
|
+
}
|
|
191
|
+
);
|
|
192
|
+
};
|
|
193
|
+
const stats = () => ({
|
|
194
|
+
state: currentState,
|
|
195
|
+
failures: consecutiveFailures,
|
|
196
|
+
successes: consecutiveSuccesses,
|
|
197
|
+
totalRequests,
|
|
198
|
+
totalFailures,
|
|
199
|
+
totalSuccesses,
|
|
200
|
+
totalRejected,
|
|
201
|
+
lastFailureTime,
|
|
202
|
+
lastSuccessTime
|
|
203
|
+
});
|
|
204
|
+
const reset = () => {
|
|
205
|
+
consecutiveFailures = 0;
|
|
206
|
+
consecutiveSuccesses = 0;
|
|
207
|
+
transition("closed");
|
|
208
|
+
};
|
|
209
|
+
return {
|
|
210
|
+
state: () => currentState,
|
|
211
|
+
protect,
|
|
212
|
+
stats,
|
|
213
|
+
reset
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export {
|
|
218
|
+
sleep,
|
|
219
|
+
timeout,
|
|
220
|
+
retry,
|
|
221
|
+
retryN,
|
|
222
|
+
retryWithBackoff,
|
|
223
|
+
makeCircuitBreaker
|
|
224
|
+
};
|