@cloudflare/sandbox 0.0.0-d81d2a5 → 0.0.0-d951819
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/CHANGELOG.md +129 -0
- package/Dockerfile +34 -27
- package/README.md +127 -12
- package/container_src/bun.lock +31 -77
- package/container_src/circuit-breaker.ts +121 -0
- package/container_src/control-process.ts +784 -0
- package/container_src/handler/exec.ts +99 -254
- package/container_src/handler/file.ts +253 -640
- package/container_src/handler/git.ts +28 -80
- package/container_src/handler/process.ts +443 -515
- package/container_src/handler/session.ts +92 -0
- package/container_src/index.ts +289 -219
- package/container_src/interpreter-service.ts +276 -0
- package/container_src/isolation.ts +1213 -0
- package/container_src/mime-processor.ts +1 -1
- package/container_src/package.json +4 -4
- package/container_src/runtime/executors/javascript/node_executor.ts +123 -0
- package/container_src/runtime/executors/python/ipython_executor.py +338 -0
- package/container_src/runtime/executors/typescript/ts_executor.ts +138 -0
- package/container_src/runtime/process-pool.ts +464 -0
- package/container_src/shell-escape.ts +42 -0
- package/container_src/startup.sh +6 -47
- package/container_src/types.ts +35 -12
- package/package.json +2 -2
- package/src/client.ts +214 -187
- package/src/errors.ts +219 -0
- package/src/file-stream.ts +162 -0
- package/src/index.ts +66 -14
- package/src/interpreter-client.ts +352 -0
- package/src/interpreter-types.ts +102 -95
- package/src/interpreter.ts +8 -8
- package/src/sandbox.ts +315 -337
- package/src/types.ts +194 -24
- package/container_src/jupyter-server.ts +0 -336
- package/src/jupyter-client.ts +0 -266
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Circuit Breaker implementation to prevent cascading failures
|
|
3
|
+
*/
|
|
4
|
+
export class CircuitBreaker {
|
|
5
|
+
private failures = 0;
|
|
6
|
+
private lastFailure: number = 0;
|
|
7
|
+
private successCount = 0;
|
|
8
|
+
private state: "closed" | "open" | "half-open" = "closed";
|
|
9
|
+
|
|
10
|
+
// Configuration
|
|
11
|
+
private readonly threshold: number;
|
|
12
|
+
private readonly timeout: number;
|
|
13
|
+
private readonly halfOpenSuccessThreshold: number;
|
|
14
|
+
private readonly name: string;
|
|
15
|
+
|
|
16
|
+
constructor(options: {
|
|
17
|
+
name: string;
|
|
18
|
+
threshold?: number;
|
|
19
|
+
timeout?: number;
|
|
20
|
+
halfOpenSuccessThreshold?: number;
|
|
21
|
+
}) {
|
|
22
|
+
this.name = options.name;
|
|
23
|
+
this.threshold = options.threshold || 5;
|
|
24
|
+
this.timeout = options.timeout || 30000; // 30 seconds
|
|
25
|
+
this.halfOpenSuccessThreshold = options.halfOpenSuccessThreshold || 3;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Execute an operation with circuit breaker protection
|
|
30
|
+
*/
|
|
31
|
+
async execute<T>(operation: () => Promise<T>): Promise<T> {
|
|
32
|
+
// Check circuit state
|
|
33
|
+
if (this.state === "open") {
|
|
34
|
+
if (Date.now() - this.lastFailure > this.timeout) {
|
|
35
|
+
console.log(
|
|
36
|
+
`[CircuitBreaker ${this.name}] Transitioning from open to half-open`
|
|
37
|
+
);
|
|
38
|
+
this.state = "half-open";
|
|
39
|
+
this.successCount = 0;
|
|
40
|
+
} else {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`Circuit breaker is open for ${this.name}. Retry after ${
|
|
43
|
+
this.timeout - (Date.now() - this.lastFailure)
|
|
44
|
+
}ms`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
const result = await operation();
|
|
51
|
+
|
|
52
|
+
// Record success
|
|
53
|
+
if (this.state === "half-open") {
|
|
54
|
+
this.successCount++;
|
|
55
|
+
if (this.successCount >= this.halfOpenSuccessThreshold) {
|
|
56
|
+
console.log(
|
|
57
|
+
`[CircuitBreaker ${this.name}] Transitioning from half-open to closed`
|
|
58
|
+
);
|
|
59
|
+
this.state = "closed";
|
|
60
|
+
this.failures = 0;
|
|
61
|
+
}
|
|
62
|
+
} else if (this.state === "closed") {
|
|
63
|
+
// Reset failure count on success
|
|
64
|
+
this.failures = 0;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return result;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
this.recordFailure();
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Record a failure and update circuit state
|
|
76
|
+
*/
|
|
77
|
+
private recordFailure() {
|
|
78
|
+
this.failures++;
|
|
79
|
+
this.lastFailure = Date.now();
|
|
80
|
+
|
|
81
|
+
if (this.state === "half-open") {
|
|
82
|
+
console.log(
|
|
83
|
+
`[CircuitBreaker ${this.name}] Failure in half-open state, transitioning to open`
|
|
84
|
+
);
|
|
85
|
+
this.state = "open";
|
|
86
|
+
} else if (this.failures >= this.threshold) {
|
|
87
|
+
console.log(
|
|
88
|
+
`[CircuitBreaker ${this.name}] Threshold reached (${this.failures}/${this.threshold}), transitioning to open`
|
|
89
|
+
);
|
|
90
|
+
this.state = "open";
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Get current circuit breaker state
|
|
96
|
+
*/
|
|
97
|
+
getState(): {
|
|
98
|
+
state: string;
|
|
99
|
+
failures: number;
|
|
100
|
+
lastFailure: number;
|
|
101
|
+
isOpen: boolean;
|
|
102
|
+
} {
|
|
103
|
+
return {
|
|
104
|
+
state: this.state,
|
|
105
|
+
failures: this.failures,
|
|
106
|
+
lastFailure: this.lastFailure,
|
|
107
|
+
isOpen: this.state === "open",
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Reset the circuit breaker
|
|
113
|
+
*/
|
|
114
|
+
reset() {
|
|
115
|
+
this.state = "closed";
|
|
116
|
+
this.failures = 0;
|
|
117
|
+
this.successCount = 0;
|
|
118
|
+
this.lastFailure = 0;
|
|
119
|
+
console.log(`[CircuitBreaker ${this.name}] Reset to closed state`);
|
|
120
|
+
}
|
|
121
|
+
}
|