forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenCodeServerOwner.ts
More file actions
184 lines (169 loc) · 5.6 KB
/
Copy pathOpenCodeServerOwner.ts
File metadata and controls
184 lines (169 loc) · 5.6 KB
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
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Scope from "effect/Scope";
import * as Semaphore from "effect/Semaphore";
import * as OpenCodeRuntime from "./opencodeRuntime.ts";
export const OPENCODE_SERVER_IDLE_TTL = "30 seconds";
interface OpenCodeServerOwnerState {
server: OpenCodeRuntime.OpenCodeServerProcess | null;
serverScope: Scope.Closeable | null;
borrowers: number;
idleCloseFiber: Fiber.Fiber<void, never> | null;
}
export class OpenCodeServerOwner extends Context.Service<
OpenCodeServerOwner,
{
readonly withServer: <A, E, R>(
use: (server: OpenCodeRuntime.OpenCodeServerProcess) => Effect.Effect<A, E, R>,
) => Effect.Effect<A, E | OpenCodeRuntime.OpenCodeRuntimeError, R>;
}
>()("t3/provider/OpenCodeServerOwner") {}
/** Owns the lazy local OpenCode server shared by one provider instance. */
export const make = Effect.fn("OpenCodeServerOwner.make")(function* (input: {
readonly binaryPath: string;
readonly directory: string;
readonly serverPassword?: string;
readonly environment?: NodeJS.ProcessEnv;
}) {
const runtime = yield* OpenCodeRuntime.OpenCodeRuntime;
const ownerScope = yield* Effect.acquireRelease(Scope.make(), (scope) =>
Scope.close(scope, Exit.void),
);
const mutex = yield* Semaphore.make(1);
const state: OpenCodeServerOwnerState = {
server: null,
serverScope: null,
borrowers: 0,
idleCloseFiber: null,
};
const cancelIdleClose = Effect.fn("OpenCodeServerOwner.cancelIdleClose")(function* () {
const fiber = state.idleCloseFiber;
state.idleCloseFiber = null;
if (fiber !== null) {
yield* Fiber.interrupt(fiber).pipe(Effect.ignore);
}
});
const closeServer = Effect.fn("OpenCodeServerOwner.closeServer")(function* (
expected?: OpenCodeRuntime.OpenCodeServerProcess,
) {
if (expected !== undefined && state.server !== expected) {
return;
}
const scope = state.serverScope;
state.server = null;
state.serverScope = null;
if (scope !== null) {
yield* Scope.close(scope, Exit.void).pipe(Effect.ignore);
}
});
const watchServerExit = Effect.fn("OpenCodeServerOwner.watchServerExit")(function* (
server: OpenCodeRuntime.OpenCodeServerProcess,
) {
yield* server.exitCode;
yield* mutex.withPermit(
Effect.gen(function* () {
if (state.server !== server) {
return;
}
yield* cancelIdleClose();
yield* closeServer(server);
}),
);
});
const acquireServer = mutex.withPermit(
Effect.gen(function* () {
yield* cancelIdleClose();
if (state.server !== null) {
if (yield* state.server.isRunning) {
state.borrowers += 1;
return state.server;
}
yield* closeServer(state.server);
}
return yield* Effect.uninterruptibleMask((restore) =>
Effect.gen(function* () {
const serverScope = yield* Scope.make();
const started = yield* Effect.exit(
restore(
runtime
.startOpenCodeServerProcess({
binaryPath: input.binaryPath,
directory: input.directory,
...(input.serverPassword !== undefined
? { serverPassword: input.serverPassword }
: {}),
...(input.environment ? { environment: input.environment } : {}),
})
.pipe(Effect.provideService(Scope.Scope, serverScope)),
),
);
if (Exit.isFailure(started)) {
yield* Scope.close(serverScope, Exit.void).pipe(Effect.ignore);
return yield* Effect.failCause(started.cause);
}
const server = started.value;
state.server = server;
state.serverScope = serverScope;
state.borrowers = 1;
yield* watchServerExit(server).pipe(Effect.forkIn(ownerScope));
return server;
}),
);
}),
);
const releaseServer = (server: OpenCodeRuntime.OpenCodeServerProcess) =>
mutex.withPermit(
Effect.gen(function* () {
if (state.server !== server) {
return;
}
state.borrowers = Math.max(0, state.borrowers - 1);
if (state.borrowers > 0) {
return;
}
yield* cancelIdleClose();
state.idleCloseFiber = yield* Effect.sleep(OPENCODE_SERVER_IDLE_TTL).pipe(
Effect.andThen(
mutex.withPermit(
Effect.gen(function* () {
if (state.server !== server || state.borrowers > 0) {
return;
}
state.idleCloseFiber = null;
yield* closeServer(server);
}),
),
),
Effect.forkIn(ownerScope),
);
}),
);
yield* Effect.addFinalizer(() =>
mutex.withPermit(
Effect.gen(function* () {
yield* cancelIdleClose();
state.borrowers = 0;
yield* closeServer();
}),
),
);
return OpenCodeServerOwner.of({
withServer: (use) =>
Effect.uninterruptibleMask((restore) =>
restore(acquireServer).pipe(
Effect.flatMap((server) =>
restore(use(server)).pipe(Effect.ensuring(releaseServer(server))),
),
),
),
});
});
export const layer = (input: {
readonly binaryPath: string;
readonly directory: string;
readonly serverPassword?: string;
readonly environment?: NodeJS.ProcessEnv;
}) => Layer.effect(OpenCodeServerOwner, make(input));