Skip to content

Commit 218dfc9

Browse files
authored
fix(context): req记录context,防止close和finish事件清理后无法上报 (#557)
1 parent f062d44 commit 218dfc9

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

lib/core/runtime/create-server.hack.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import * as http from "http";
1010
import * as https from "https";
1111
import * as domain from "domain";
12-
import currentContext, { RequestLog } from "../context";
12+
import currentContext, { Context, RequestLog } from "../context";
1313
import { address } from "ip";
1414
import { AddressInfo, isIP } from "net";
1515
import { captureOutgoing } from "./capture/outgoing";
@@ -56,7 +56,7 @@ export const hack = <T extends typeof http.createServer>(
5656

5757
// Creating a domain and wrapping the execution.
5858
const d = domain.create();
59-
59+
const context = new Context();
6060
d.add(req);
6161
d.add(res);
6262

@@ -86,8 +86,6 @@ export const hack = <T extends typeof http.createServer>(
8686
): ReturnType<typeof res.writeHead> => {
8787
timestamps.onResponse = new Date().getTime();
8888

89-
const context = currentContext();
90-
9189
eventBus.emit(EVENT_LIST.RESPONSE_START, {
9290
req, res, context
9391
});
@@ -98,8 +96,6 @@ export const hack = <T extends typeof http.createServer>(
9896
})(res.writeHead);
9997

10098
res.once("finish", () => {
101-
const context = currentContext();
102-
10399
context.currentRequest = {
104100
SN: context.SN,
105101

@@ -159,9 +155,6 @@ export const hack = <T extends typeof http.createServer>(
159155

160156
res.once("close", () => {
161157
timestamps.responseClose = new Date().getTime();
162-
163-
const context = currentContext();
164-
165158
clearDomain();
166159

167160
eventBus.emit(EVENT_LIST.RESPONSE_CLOSE, {
@@ -170,7 +163,8 @@ export const hack = <T extends typeof http.createServer>(
170163
});
171164

172165
d.run(() => {
173-
const context = currentContext();
166+
process.domain.currentContext = context;
167+
174168
eventBus.emit(EVENT_LIST.REQUEST_START, {
175169
req, context
176170
});

0 commit comments

Comments
 (0)