feat: use request context

This commit is contained in:
Kevin Franklin Kim 2024-07-10 10:58:02 +02:00
parent d88146a2e9
commit 3a414aba79
No known key found for this signature in database
2 changed files with 4 additions and 22 deletions

View File

@ -131,12 +131,12 @@ func (s *Subscriber) handle(l *zap.Logger, r *http.Request, payload *gtag.Payloa
msg := message.NewMessage(s.uuidFunc(), data)
l = l.With(zap.String("message_id", msg.UUID))
msg.SetContext(context.WithoutCancel(r.Context()))
if payload.EventName != nil {
msg.Metadata.Set(MetadataEventName, gtag.Get(payload.EventName).String())
}
// TODO filter headers?
for name, headers := range r.Header {
msg.Metadata.Set(name, strings.Join(headers, ","))
}
@ -145,11 +145,6 @@ func (s *Subscriber) handle(l *zap.Logger, r *http.Request, payload *gtag.Payloa
l = l.With(zap.String(k, v))
}
// TODO different context?
ctx, cancelCtx := context.WithCancel(r.Context())
msg.SetContext(ctx)
defer cancelCtx()
// send message
s.messages <- msg
@ -162,7 +157,7 @@ func (s *Subscriber) handle(l *zap.Logger, r *http.Request, payload *gtag.Payloa
l.Debug("message nacked")
return ErrMessageNacked
case <-r.Context().Done():
l.Debug("message cancled")
l.Debug("message canceled")
return ErrContextCanceled
}
}

View File

@ -110,29 +110,16 @@ func (s *Subscriber) handle(l *zap.Logger, r *http.Request, payload *mpv2.Payloa
msg := message.NewMessage(s.uuidFunc(), jsonPayload)
l = l.With(zap.String("message_id", msg.UUID))
msg.SetContext(context.WithoutCancel(r.Context()))
// TODO filter headers?
for name, headers := range r.Header {
msg.Metadata.Set(name, strings.Join(headers, ","))
}
// if cookies := r.Cookies(); len(cookies) > 0 {
// values := make([]string, len(cookies))
// for i, cookie := range r.Cookies() {
// values[i] = cookie.String()
// }
// msg.Metadata.Set("Cookie", strings.Join(values, "; "))
// }
for k, v := range msg.Metadata {
l = l.With(zap.String(k, v))
}
// TODO different context?
ctx, cancelCtx := context.WithCancel(r.Context())
msg.SetContext(ctx)
defer cancelCtx()
// send message
s.messages <- msg
@ -145,7 +132,7 @@ func (s *Subscriber) handle(l *zap.Logger, r *http.Request, payload *mpv2.Payloa
l.Debug("message nacked")
return ErrMessageNacked
case <-r.Context().Done():
l.Debug("message cancled")
l.Debug("message canceled")
return ErrContextCanceled
}
}