Using HTTP/2 Cleartext for a server in Go 1.24

https://news.ycombinator.com/rss Hits: 16
Summary

In our application, we use long-lived server-sent event streams (SSE). These are set up to have a really long timeout and lifetime - 15 minutes in our setup. However, Google Cloud Run has a known issue in that client disconnects are not propagated to Cloud Run when using HTTP/1.1 to communicate with the backend service. Thus, I started looking into using HTTP/2 for services.Cloud Run terminates TLS at the frontend, but can forward traffic as either HTTP/1.1 or HTTP/2 cleartext (h2c) traffic. Normally, HTTP/2 always uses TLS, but HTTP clients and servers can often be configured to use a cleartext version of the protocol. Cloud Run also makes you select the protocol to be used. This is basically the “HTTP/2 with Prior Knowledge” setup noted in RFC 9113, section 3.3.Configuring a Go server for HTTP/2 cleartextOur first cut of h2c support predated the Go 1.24 changes I will go into more detail below. Most posts and guides on the internet will point you toward the old outdated approach, but I’ve included it below so it is easier to see the before and after, and migrate your own code if you need to.Before Go 1.24 (old approach)To use h2c, you had to use the golang.org/x/net/http package, and go through a convoluted setup.import ( "net/http" "golang.org/x/net/http2" "golang.org/x/net/http2/h2c" ) handler := ... h2s := &http2.Server{} handler = h2c.NewHandler(handler, h2s) srv := &http.Server{ Addr: fmt.Sprintf("%s:%d", "", 9888), Handler: handler, ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 10 * time.Second, WriteTimeout: 35 * time.Second, IdleTimeout: 620 * time.Second, } err = http2.ConfigureServer(srv, h2s) if err != nil { ... } Go 1.24+ (new approach)With Go 1.24, no x/net/http2/h2c wrapper is needed anymore, and the setup is a lot more readable. You can configure protocols directly on http.Server.Reference: Go 1.24 net/http release notes.handler := ... srv := &http.Server{ Addr: fmt.Sprintf("%s:%d", "", 9888), Handler: handler, ReadHeaderTimeout: 5 * time.Second,...

First seen: 2026-05-24 21:00

Last seen: 2026-05-25 12:15