fix: improve workbench kafka publish handling

This commit is contained in:
richjohnrambo
2026-09-15 09:46:26 +08:00
parent 989e365492
commit edb9a6b2bb
+13 -3
View File
@@ -6,6 +6,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@@ -22,6 +23,8 @@ type API struct {
events events.Publisher events events.Publisher
} }
const workbenchEventPublishTimeout = 15 * time.Second
func New(st *store.Store, authenticator *auth.Authenticator, publisher events.Publisher) *API { func New(st *store.Store, authenticator *auth.Authenticator, publisher events.Publisher) *API {
if publisher == nil { if publisher == nil {
publisher = events.NoopPublisher{} publisher = events.NoopPublisher{}
@@ -863,11 +866,18 @@ func (a *API) publishWorkbenchEvent(w http.ResponseWriter, r *http.Request, p *a
if a.events == nil { if a.events == nil {
return true return true
} }
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
event := events.NewWorkbenchEvent(p, requestID(r), module, action, resourceID, targets, title, summary, data) event := events.NewWorkbenchEvent(p, requestID(r), module, action, resourceID, targets, title, summary, data)
start := time.Now()
ctx, cancel := context.WithTimeout(r.Context(), workbenchEventPublishTimeout)
defer cancel()
if err := a.events.Publish(ctx, event); err != nil { if err := a.events.Publish(ctx, event); err != nil {
writeError(w, r, http.StatusInternalServerError, 90000, reasonInternal, "workbench event publish failed", map[string]any{"event": action}) log.Printf("workbench event publish failed: event_id=%s module=%s action=%s resource_id=%s trace_id=%s elapsed=%s err=%v",
event.EventID, module, action, resourceID, event.TraceID, time.Since(start).Round(time.Millisecond), err)
writeError(w, r, http.StatusInternalServerError, 90000, reasonInternal, "workbench event publish failed", map[string]any{
"event_id": event.EventID,
"module": module,
"event": action,
})
return false return false
} }
return true return true