Skip to content

Commit

Permalink
fix(context): req记录context,防止close和finish事件清理后无法上报 (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinzZH committed Sep 14, 2023
1 parent f062d44 commit 218dfc9
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions lib/core/runtime/create-server.hack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import * as http from "http";
import * as https from "https";
import * as domain from "domain";
import currentContext, { RequestLog } from "../context";
import currentContext, { Context, RequestLog } from "../context";
import { address } from "ip";
import { AddressInfo, isIP } from "net";
import { captureOutgoing } from "./capture/outgoing";
Expand Down Expand Up @@ -56,7 +56,7 @@ export const hack = <T extends typeof http.createServer>(

// Creating a domain and wrapping the execution.
const d = domain.create();

const context = new Context();
d.add(req);
d.add(res);

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

const context = currentContext();

eventBus.emit(EVENT_LIST.RESPONSE_START, {
req, res, context
});
Expand All @@ -98,8 +96,6 @@ export const hack = <T extends typeof http.createServer>(
})(res.writeHead);

res.once("finish", () => {
const context = currentContext();

context.currentRequest = {
SN: context.SN,

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

res.once("close", () => {
timestamps.responseClose = new Date().getTime();

const context = currentContext();

clearDomain();

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

d.run(() => {
const context = currentContext();
process.domain.currentContext = context;

eventBus.emit(EVENT_LIST.REQUEST_START, {
req, context
});
Expand Down

0 comments on commit 218dfc9

Please sign in to comment.