-
-
Notifications
You must be signed in to change notification settings - Fork 233
feat: unwrap resp for better deadline/flush SSE support #613
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 |
|---|---|---|
|
|
@@ -24,6 +24,14 @@ func deref(t reflect.Type) reflect.Type { | |
| return t | ||
| } | ||
|
|
||
| type unwrapper interface { | ||
| Unwrap() http.ResponseWriter | ||
| } | ||
|
|
||
| type writeDeadliner interface { | ||
| SetWriteDeadline(time.Time) error | ||
| } | ||
|
|
||
| // Message is a single SSE message. There is no `event` field as this is | ||
| // handled by the `eventTypeMap` when registering the operation. | ||
| type Message struct { | ||
|
|
@@ -119,9 +127,41 @@ func Register[I any](api huma.API, op huma.Operation, eventTypeMap map[string]an | |
| ctx.SetHeader("Content-Type", "text/event-stream") | ||
| bw := ctx.BodyWriter() | ||
| encoder := json.NewEncoder(bw) | ||
|
|
||
| // Get the flusher/deadliner from the response writer if possible. | ||
| var flusher http.Flusher | ||
| flushCheck := bw | ||
| for { | ||
| if f, ok := flushCheck.(http.Flusher); ok { | ||
| flusher = f | ||
| break | ||
| } | ||
| if u, ok := flushCheck.(unwrapper); ok { | ||
| flushCheck = u.Unwrap() | ||
| } else { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| var deadliner writeDeadliner | ||
| deadlineCheck := bw | ||
| for { | ||
| if d, ok := deadlineCheck.(writeDeadliner); ok { | ||
| deadliner = d | ||
| break | ||
| } | ||
| if u, ok := deadlineCheck.(unwrapper); ok { | ||
| deadlineCheck = u.Unwrap() | ||
| } else { | ||
| break | ||
| } | ||
| } | ||
|
Comment on lines
+132
to
+158
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Refactor duplicated unwrapping logic into a helper function The unwrapping logic to obtain You could create a generic helper function like this: // Helper function to unwrap the response writer until the desired interface is found.
func getInterface[T any](bw http.ResponseWriter) T {
check := bw
for {
if res, ok := check.(T); ok {
return res
}
if u, ok := check.(unwrapper); ok {
check = u.Unwrap()
} else {
var zero T
return zero
}
}
}Then replace the unwrapping loops with calls to this helper function: - var flusher http.Flusher
- flushCheck := bw
- for {
- if f, ok := flushCheck.(http.Flusher); ok {
- flusher = f
- break
- }
- if u, ok := flushCheck.(unwrapper); ok {
- flushCheck = u.Unwrap()
- } else {
- break
- }
- }
+ flusher := getInterface[http.Flusher](bw)And similarly for - var deadliner writeDeadliner
- deadlineCheck := bw
- for {
- if d, ok := deadlineCheck.(writeDeadliner); ok {
- deadliner = d
- break
- }
- if u, ok := deadlineCheck.(unwrapper); ok {
- deadlineCheck = u.Unwrap()
- } else {
- break
- }
- }
+ deadliner := getInterface[writeDeadliner](bw) |
||
|
|
||
| send := func(msg Message) error { | ||
| if d, ok := bw.(interface{ SetWriteDeadline(time.Time) error }); ok { | ||
| d.SetWriteDeadline(time.Now().Add(WriteTimeout)) | ||
| if deadliner != nil { | ||
| if err := deadliner.SetWriteDeadline(time.Now().Add(WriteTimeout)); err != nil { | ||
| fmt.Println("warning: unable to set write deadline: " + err.Error()) | ||
| } | ||
| } else { | ||
| fmt.Println("warning: unable to set write deadline") | ||
| } | ||
|
|
@@ -155,8 +195,8 @@ func Register[I any](api huma.API, op huma.Operation, eventTypeMap map[string]an | |
| return err | ||
| } | ||
| bw.Write([]byte("\n")) | ||
| if f, ok := bw.(http.Flusher); ok { | ||
| f.Flush() | ||
| if flusher != nil { | ||
| flusher.Flush() | ||
| } else { | ||
| fmt.Println("error: unable to flush") | ||
| return fmt.Errorf("unable to flush: %w", http.ErrNotSupported) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.