Use context timeout to cancel the log request

**What**
- Pass the writetimeout to the logs handler to set the context timeout
of the log stream.

Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
This commit is contained in:
Lucas Roesler
2019-06-22 21:47:02 +02:00
committed by Alex Ellis
parent db6628d1a5
commit 6df51a3516
2 changed files with 5 additions and 3 deletions

View File

@ -14,7 +14,7 @@ const crlf = "\r\n"
const upstreamLogsEndpoint = "/system/logs" const upstreamLogsEndpoint = "/system/logs"
// NewLogHandlerFunc creates and http HandlerFunc from the supplied log Requestor. // NewLogHandlerFunc creates and http HandlerFunc from the supplied log Requestor.
func NewLogHandlerFunc(logProvider url.URL) http.HandlerFunc { func NewLogHandlerFunc(logProvider url.URL, timeout time.Duration) http.HandlerFunc {
writeRequestURI := false writeRequestURI := false
if _, exists := os.LookupEnv("write_request_uri"); exists { if _, exists := os.LookupEnv("write_request_uri"); exists {
writeRequestURI = exists writeRequestURI = exists
@ -23,7 +23,9 @@ func NewLogHandlerFunc(logProvider url.URL) http.HandlerFunc {
upstreamLogProviderBase := strings.TrimSuffix(logProvider.String(), "/") upstreamLogProviderBase := strings.TrimSuffix(logProvider.String(), "/")
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx, cancelQuery := context.WithTimeout(r.Context(), timeout)
defer cancelQuery()
if r.Body != nil { if r.Body != nil {
defer r.Body.Close() defer r.Body.Close()
} }

View File

@ -113,7 +113,7 @@ func main() {
forwardingNotifiers, forwardingNotifiers,
) )
faasHandlers.LogProxyHandler = handlers.NewLogHandlerFunc(*config.LogsProviderURL) faasHandlers.LogProxyHandler = handlers.NewLogHandlerFunc(*config.LogsProviderURL, config.WriteTimeout)
if config.UseNATS() { if config.UseNATS() {
log.Println("Async enabled: Using NATS Streaming.") log.Println("Async enabled: Using NATS Streaming.")