Skip to content
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

Flush don't work #529

Closed
ashertz-dev opened this issue May 31, 2023 · 7 comments
Closed

Flush don't work #529

ashertz-dev opened this issue May 31, 2023 · 7 comments

Comments

@ashertz-dev
Copy link

When I try to convert Response.ResponseWriter to http.Flush, by using restful.Response.ResponseWriter.(http.Flusher), it return false. So, the func Flush faild too.

@ashertz-dev
Copy link
Author

I resolve it

@maratrixx
Copy link

I resolve it

How to solve the problem? I also encountered the same problem. thanks.

@ashertz-dev
Copy link
Author

I resolve it

How to solve the problem? I also encountered the same problem. thanks.

Sorry, it has been a long time, and I have already forgotten. I find some code block in my project. Maybe it's useful.

type WriterFlusher interface {
	io.Writer
	http.Flusher
}

// wrapperHTTP used to wrap http request for print
func wrapperHTTP(h http.Handler) http.Handler {
	slogger := slog.New(slog.NewTextHandler(os.Stdout))
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		m := httpsnoop.CaptureMetrics(h, w, r)
		defer func() {
			slogger.Info("",
				"method", r.Method,
				"url", r.URL,
				"proto", r.Proto,
				"user-agent", r.UserAgent(),
				"remote", r.RemoteAddr,
				"referer", r.Referer(),
				"status", m.Code,
				"size", m.Written,
				"lat-ms", m.Duration.Milliseconds(),
			)
		}()
	})
}

func ListenAndServe(addr string, handler http.Handler) error {
	return http.ListenAndServe(addr, wrapperHTTP(handler))
}

@maratrixx
Copy link

I resolve it

How to solve the problem? I also encountered the same problem. thanks.

Sorry, it has been a long time, and I have already forgotten. I find some code block in my project. Maybe it's useful.

type WriterFlusher interface {
	io.Writer
	http.Flusher
}

// wrapperHTTP used to wrap http request for print
func wrapperHTTP(h http.Handler) http.Handler {
	slogger := slog.New(slog.NewTextHandler(os.Stdout))
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		m := httpsnoop.CaptureMetrics(h, w, r)
		defer func() {
			slogger.Info("",
				"method", r.Method,
				"url", r.URL,
				"proto", r.Proto,
				"user-agent", r.UserAgent(),
				"remote", r.RemoteAddr,
				"referer", r.Referer(),
				"status", m.Code,
				"size", m.Written,
				"lat-ms", m.Duration.Milliseconds(),
			)
		}()
	})
}

func ListenAndServe(addr string, handler http.Handler) error {
	return http.ListenAndServe(addr, wrapperHTTP(handler))
}

Thanks your reply. But this does not appear to use the flush method.

@nullbus
Copy link
Contributor

nullbus commented Dec 22, 2023

I suggest to figure out the actual type of ResponseWriter the server is using (using debbuger or logging). I think your ResponseWriter is not being from Go's default http package.

In this package, there is some path that it can wrap default http.ResponseWriter:

go-restful/container.go

Lines 263 to 272 in c691c18

if contentEncodingEnabled {
doCompress, encoding := wantsCompressedResponse(httpRequest, httpWriter)
if doCompress {
var err error
writer, err = NewCompressingResponseWriter(httpWriter, encoding)
if err != nil {
log.Print("unable to install compressor: ", err)
httpWriter.WriteHeader(http.StatusInternalServerError)
return
}

It replaces writer to CompressingResponseWriter if something compression is needed and CompressingResponseWriter is not implementing Flush method for now. If your case is it, there are some options:

  • disable compression
  • implement Flush method to CompressingResponseWriter
  • use Close() instead of Flush()

@emicklei
Copy link
Owner

I think adding a Flush method is an easy step to take

@emicklei
Copy link
Owner

#538

emicklei added a commit that referenced this issue Mar 11, 2024
* add Flush method

* fix doc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants