Skip to content

how to close tcp keep-alive for http.ListenAndServer #37462

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

Closed
orange-jacky opened this issue Feb 26, 2020 · 1 comment
Closed

how to close tcp keep-alive for http.ListenAndServer #37462

orange-jacky opened this issue Feb 26, 2020 · 1 comment

Comments

@orange-jacky
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.14 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/Users/xx/Documents/develop/go/workspace/bin"
GOCACHE="/Users/xx/Library/Caches/go-build"
GOENV="/Users/xx/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/xx/Documents/develop/go/workspace"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/local/go"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/7r/fpcvbc6d32j2lfls0lllcs240000gn/T/go-build785679791=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOROOT/bin/go version: go version go1.14 darwin/amd64
GOROOT/bin/go tool compile -V: compile version go1.14
uname -v: Darwin Kernel Version 19.3.0: Thu Jan  9 20:58:23 PST 2020; root:xnu-6153.81.5~1/RELEASE_X86_64
ProductName:	Mac OS X
ProductVersion:	10.15.3
BuildVersion:	19D76
lldb --version: lldb-1100.0.30.12
Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15)

What did you do?

edit main.go

`package main

import (
"log"
"net/http"
)

func main() {
addr := "127.0.0.1:8080"
http.HandleFunc("/agent", agentHandler)

srv := http.Server{
	Addr: addr,
}
srv.SetKeepAlivesEnabled(false)

err := srv.ListenAndServe()
log.Fatal(err)

}

func agentHandler(w http.ResponseWriter, r *http.Request) {
}
`

in terminal1
go run main.go

in terminal2
telnet 127.0.0.1 8080

What did you expect to see?

http server not actively send a tcp keep-alive to client
or i can change time of send tcp keep-alive

What did you see instead?

every 15s, http server actively send a tcp keep-alive to client

image

@ianlancetaylor
Copy link
Contributor

We generally do not use the issue tracker for questions. You will get better and faster answers on a forum. See https://golang.org/wiki/Questions. Thanks.

The srv.SetKeepAlivesEnabled method applies to HTTP keep-alives. What you are seeing is a different thing: TCP keep-alives. TCP keep-alives are controlled by methods like
https://golang.org/pkg/net/#TCPConn.SetKeepAlive. I'm not sure but I think you could probably set this by using ConnState in your http.Server. Asking on a forum may get better advice.

@golang golang locked and limited conversation to collaborators Feb 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants