<p>I have a Google Kubernetes Engine cluster, inside several pods with <code>NodePort</code>s, and all is exposed via an <code>Ingress</code>, which creates an HTTP load balancer (LB). I am using custom domain with Google managed SSL certificate for the LB.</p>

<p>My backend is an HTTP server written in Go, using its <code>"net/http"</code> package. It is using self-signed certificate for mTLS with LB (Google's HTTP LB accepts any certificate for mTLS).</p>

<p>Everything works fine, except for one case, and that is when a client creates an HTTP 1.1 connection with the LB and then cancels the request. This cancels the connection between the client and the LB, but LB holds an open connection with my backend until server's timeout.</p>

<p>My use-case requires requests to be opened even for hours, so my server has huge timeout values. Business logic inside the request is correctly using the request's <code>Context</code> and takes into account if the request is canceled by the client.</p>

<p>Everything works as expected if the client makes an HTTP2 request and cancels it i.e. the whole connection down to my backend is canceled.</p>

<p>Here is an example Go handler that simulates a cancelable long-running task:</p>

<pre><code>func handleLongRunningTask(w http.ResponseWriter, r *http.Request) {
    ctx := r.Context()
    t := time.Now()

    select {
    case &lt;-ctx.Done():
        log.Println("request canceled")
    case &lt;-time.After(30 * time.Second):
        log.Println("request finished")
    }
    log.Printf("here after: %v\n", time.Since(t))

    w.WriteHeader(http.StatusOK)
}
</code></pre>

<p>The <code>case &lt;-ctx.Done():</code> is never called for canceled HTTP 1.1 requests.</p>

<p>For easy testing I am using curl and <code>Ctrl+C</code>; this works:</p>

<pre><code>curl -v --http2 'https://example.com/long_running_task'
</code></pre>

<p>and this does not:</p>

<pre><code>curl -v --http1.1 'https://example.com/long_running_task'
</code></pre>

<p>It does not matter if the <code>NodePort</code> is HTTPS or HTTP2, the LB has exactly the same behaviour regarding requests canceled by clients.</p>

<p>I tried compiling the server with Go 1.14.4 and 1.13.12 and the results are the same.</p>

<p>Is this a bug in Kubernetes, Ingress, Google Kubernetes Engine, Google's HTTP Load Balancer, Go's HTTP server? Or is it something with HTTP 1.1 that I am missing? What can be wrong and how can I fix this?</p>

<p>...it is not possible to know the HTTP version in the backend, so I could reject all HTTP 1.1 requests. LB is always using the same HTTP version when communicating with its backends, no matter the client's HTTP version.</p>

shelll
<p>From your description it looks like the issue might be between the <a href="https://cloud.google.com/solutions/about-capacity-optimization-with-global-lb#addressing_these_challenges_with_global_load_balancing" rel="nofollow noreferrer">GFE</a> and the backends, since <a href="https://medium.com/google-cloud/ingress-load-balancing-issues-on-googles-gke-f54c7e194dd5" rel="nofollow noreferrer">GFE might hold the connections for reuse</a>.</p>

<p>My take is that you're seeing this variation between protocol version because how both handle connection persistence.</p>

<p>For HTTP2, the connection will be open <a href="https://stackoverflow.com/a/48110818/10892354">until one of the parties send a termination signal and the earliest takes preference</a>. But for HTTP1.1, <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html" rel="nofollow noreferrer">it might be prolonged until an explicit connection header is sent</a>, specifying the termination:</p>

<blockquote>
  <p>An HTTP/1.1 server MAY assume that a HTTP/1.1 client intends to maintain a persistent connection unless a Connection header including the connection-token "close" was sent in the request. If the server chooses to close the connection immediately after sending the response, it SHOULD send a Connection header including the connection-token close.</p>
</blockquote>

<p>This might explain why HTTP1.1 follows the <a href="https://cloud.google.com/load-balancing/docs/https#timeouts_and_retries" rel="nofollow noreferrer">same timeout configuration as the LB</a> and HTTP2 doesn't.</p>

<p>I'd recommend trying to actively send termination headers whenever you want to terminate a connection. An <a href="https://gist.github.com/freehan/ac4a5016315efe7908c2795129f918e1#file-server-hostname-go-L56" rel="nofollow noreferrer">example taken from Github</a>:</p>

<pre><code>func (m *MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    log.Printf("HTTP request from %s", r.RemoteAddr)
    // Add this header to force to close the connection after serving the request.
    w.Header().Add("Connection", "close")
    fmt.Fprintf(w, "%s", m.hostname)
}
</code></pre>

<p>Additionally, there seem to be some success stories switching your cluster to be <a href="https://medium.com/google-cloud/vpc-native-clusters-on-google-kubernetes-engine-b7c022c07510" rel="nofollow noreferrer">VPC Native</a>, as it takes out of the equation the kube-proxy connection management.</p>

<p>Finally, it might be that you're in a very specific situation that is worth being evaluated separately. You might want to try to send some replication steps to the GKE team using <a href="https://issuetracker.google.com/issues/new?component=187077&amp;template=1162666" rel="nofollow noreferrer">Issue Tracker</a>.</p>

<p>I hope this helps.</p>
