Skip to content

Commit d5d284d

Browse files
committed
await stops + add timeout
Signed-off-by: LeTamanoir <[email protected]>
1 parent 0ef2a9f commit d5d284d

File tree

4 files changed

+46
-22
lines changed

4 files changed

+46
-22
lines changed

src/adapter/bun/index.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {
1717
isNotEmpty,
1818
isNumericString,
1919
randomId,
20-
supportPerMethodInlineHandler
20+
supportPerMethodInlineHandler,
21+
withTimeout
2122
} from '../../utils'
2223

2324
import {
@@ -381,9 +382,20 @@ export const BunAdapter: ElysiaAdapter = {
381382

382383
if (callback) callback(app.server!)
383384

384-
process.on('beforeExit', () => {
385+
process.on('beforeExit', async () => {
385386
if (app.server) {
386-
app.server.stop?.()
387+
if (typeof options === 'object') {
388+
await withTimeout(
389+
() =>
390+
app.server!.stop(
391+
options.closeActiveConnections
392+
),
393+
options.stopTimeout ?? -1
394+
)
395+
} else {
396+
await app.server!.stop()
397+
}
398+
387399
app.server = null
388400

389401
if (app.event.stop)
@@ -414,9 +426,12 @@ export const BunAdapter: ElysiaAdapter = {
414426
})
415427
}
416428
},
417-
async stop(app, closeActiveConnections) {
429+
async stop(app, { closeActiveConnections, timeout = -1 }) {
418430
if (app.server) {
419-
app.server.stop(closeActiveConnections)
431+
await withTimeout(
432+
() => app.server!.stop(closeActiveConnections),
433+
timeout
434+
)
420435
app.server = null
421436

422437
if (app.event.stop?.length)

src/adapter/types.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ import type { AnyWSLocalHook } from '../ws/types'
99

1010
export interface ElysiaAdapter {
1111
name: string
12-
listen(
13-
app: AnyElysia
14-
): (
15-
options: string | number | Partial<Serve>,
12+
listen(app: AnyElysia): (
13+
options:
14+
| string
15+
| number
16+
| Partial<
17+
Serve & {
18+
closeActiveConnections?: boolean
19+
stopTimeout?: number
20+
}
21+
>,
1622
callback?: ListenCallback
1723
) => void
1824
/**
@@ -28,15 +34,15 @@ export interface ElysiaAdapter {
2834
* ```typescript
2935
* app.stop({ closeActiveConnections: true }) // Abruptly stop any requests inflight
3036
* ```
31-
*
37+
*
3238
* @example
3339
* ```typescript
34-
* app.stop({ shutdownTimeout: 1000 }) // Wait for 1 second to stop the server
40+
* app.stop({ timeout: 1000 }) // Wait for 1 second to stop the server
3541
* ```
3642
*/
3743
stop?(
3844
app: AnyElysia,
39-
params: { closeActiveConnections?: boolean; shutdownTimeout?: number }
45+
params: { closeActiveConnections?: boolean; timeout?: number }
4046
): Promise<void>
4147
isWebStandard?: boolean
4248
handler: {

src/adapter/web-standard/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from './handler'
77

88
import type { ElysiaAdapter } from '../types'
9+
import { withTimeout } from '../../utils'
910

1011
export const WebStandardAdapter: ElysiaAdapter = {
1112
name: 'web-standard',
@@ -59,14 +60,17 @@ export const WebStandardAdapter: ElysiaAdapter = {
5960
}
6061
}
6162
},
62-
async stop(app, closeActiveConnections) {
63+
async stop(app, { closeActiveConnections, timeout = -1 }) {
6364
if (!app.server)
6465
throw new Error(
6566
"Elysia isn't running. Call `app.listen` to start the server."
6667
)
6768

6869
if (app.server) {
69-
app.server.stop(closeActiveConnections)
70+
await withTimeout(
71+
() => app.server!.stop(closeActiveConnections),
72+
timeout
73+
)
7074
app.server = null
7175

7276
if (app.event.stop?.length)

src/index.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ import fastDecodeURIComponent from 'fast-decode-uri-component'
1212
import type { Context, PreContext } from './context'
1313

1414
import { t } from './type-system'
15-
import {
16-
mergeInference,
17-
sucrose,
18-
type Sucrose
19-
} from './sucrose'
15+
import { mergeInference, sucrose, type Sucrose } from './sucrose'
2016

2117
import type { WSLocalHook } from './ws/types'
2218

@@ -8157,14 +8153,17 @@ export default class Elysia<
81578153
* app.stop(true) // Abruptly any requests inflight
81588154
* ```
81598155
*/
8160-
stop = async (closeActiveConnections?: boolean) => {
8161-
await this['~adapter'].stop?.(this, closeActiveConnections)
8156+
stop = async (options: {
8157+
closeActiveConnections?: boolean
8158+
shutdownTimeout?: number
8159+
}) => {
8160+
await this['~adapter'].stop?.(this, options)
81628161

81638162
return this
81648163
};
81658164

81668165
[Symbol.dispose] = () => {
8167-
if (this.server) this.stop()
8166+
if (this.server) this.stop({})
81688167
}
81698168

81708169
/**

0 commit comments

Comments
 (0)