Last active
December 14, 2020 18:59
-
-
Save chrisgoffinet/09ad40bd0890e7e5818c44d5bac3ebaa to your computer and use it in GitHub Desktop.
set custom tcp keepAlive timeout on http server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"net" | |
"net/http" | |
"time" | |
) | |
func greet(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello World! %s", time.Now()) | |
} | |
func main() { | |
addr := ":8080" | |
server := &http.Server{Addr: addr, Handler: nil} | |
// set listen config tcp keeyAlive | |
lc := net.ListenConfig{KeepAlive: 60 * time.Second} | |
ln, err := lc.Listen(context.Background(), "tcp", addr) | |
if err != nil { | |
panic(err) | |
} | |
defer ln.Close() | |
http.HandleFunc("/", greet) | |
log.Fatal(server.Serve(ln)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment