-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(wecom): use channel context instead of HTTP request context for async message processing #914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,9 +129,12 @@ func NewWeComAppChannel(cfg config.WeComAppConfig, messageBus *bus.MessageBus) ( | |
| channels.WithReasoningChannelID(cfg.ReasoningChannelID), | ||
| ) | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| return &WeComAppChannel{ | ||
| BaseChannel: base, | ||
| config: cfg, | ||
| ctx: ctx, | ||
| cancel: cancel, | ||
| processedMsgs: make(map[string]bool), | ||
| }, nil | ||
| } | ||
|
|
@@ -567,8 +570,9 @@ func (c *WeComAppChannel) handleMessageCallback(ctx context.Context, w http.Resp | |
| return | ||
| } | ||
|
|
||
| // Process the message with context | ||
| go c.processMessage(ctx, msg) | ||
| // Process the message with the channel's long-lived context (not the HTTP | ||
| // request context, which is canceled as soon as we return the response). | ||
| go c.processMessage(c.ctx, msg) | ||
|
Comment on lines
+573
to
+575
|
||
|
|
||
| // Return success response immediately | ||
| // WeCom App requires response within configured timeout (default 5 seconds) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -93,9 +93,12 @@ func NewWeComBotChannel(cfg config.WeComConfig, messageBus *bus.MessageBus) (*We | |||||||||||||
| channels.WithReasoningChannelID(cfg.ReasoningChannelID), | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| ctx, cancel := context.WithCancel(context.Background()) | ||||||||||||||
| return &WeComBotChannel{ | ||||||||||||||
| BaseChannel: base, | ||||||||||||||
| config: cfg, | ||||||||||||||
| ctx: ctx, | ||||||||||||||
| cancel: cancel, | ||||||||||||||
| processedMsgs: make(map[string]bool), | ||||||||||||||
| }, nil | ||||||||||||||
| } | ||||||||||||||
|
|
@@ -292,8 +295,9 @@ func (c *WeComBotChannel) handleMessageCallback(ctx context.Context, w http.Resp | |||||||||||||
| return | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Process the message asynchronously with context | ||||||||||||||
| go c.processMessage(ctx, msg) | ||||||||||||||
| // Process the message with the channel's long-lived context (not the HTTP | ||||||||||||||
| // request context, which is canceled as soon as we return the response). | ||||||||||||||
| go c.processMessage(c.ctx, msg) | ||||||||||||||
|
Comment on lines
+298
to
+300
|
||||||||||||||
| go c.processMessage(c.ctx, msg) | |
| procCtx := c.ctx | |
| if procCtx == nil { | |
| procCtx = context.Background() | |
| } | |
| go c.processMessage(procCtx, msg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c.ctxmay be nil here if the channel hasn't been started yet (e.g., current tests invokehandleMessageCallbackon a newly constructed channel without callingStart). A nil context will panic downstream inBaseChannel.HandleMessage/MessageBus.PublishInboundwhenctx.Err()is called. Ensurec.ctxis always non-nil (initialize it inNewWeComAppChannel, or add a safe fallback here whenc.ctx == nil).