Skip to content
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

net/http: How can I cancel a Dial in net/http/Transport DialContext or "how can http client use only tcp6." #28464

Closed
imfht opened this issue Oct 29, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@imfht
Copy link

commented Oct 29, 2018

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

go version go1.11 darwin/amd64

Does this issue reproduce with the latest release?

yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/jinxufang/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/jinxufang/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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/5_/2mhh8j2n4tdgndp9_9b0stzm0000gn/T/go-build163807452=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I create my own DialContext because want I to detect if a domain is ipv6-only. code below

package main
import (
	"errors"
	"fmt"
	"net"
	"net/http"
	"syscall"
	"time"
)
func ModifiedTransport() {
	var MyTransport = &http.Transport{
		DialContext: (&net.Dialer{
			Timeout:   30 * time.Second,
			KeepAlive: 30 * time.Second,
			DualStack: false,
			Control: func(network, address string, c syscall.RawConn) error {
				if network == "ipv4" {
					// cancel connection here because the site could connect via IPv6  and client.Get("http://myexample.com") return a non-nil err.
					return errors.New("you should not use ipv4")
				}
				return nil
			},
		}).DialContext,
		MaxIdleConns:          100,
		IdleConnTimeout:       90 * time.Second,
		TLSHandshakeTimeout:   10 * time.Second,
		ExpectContinueTimeout: 1 * time.Second,
	}
	var myClient = http.Client{Transport: MyTransport}
	resp, myerr := myClient.Get("http://www.github.com")
	if myerr != nil {
		fmt.Println("request error")
		return 
	}
	var buffer = make([]byte, 1000)
	resp.Body.Read(buffer)
	fmt.Println(string(buffer))
}
func main(){
	ModifiedTransport();
}

What did you expect to see?

I want to cancel the request if domain can not connect via IPv6.

What did you see instead?

I do not now how to close the request even when I can enter network == "ipv4".

addition

Python can solve the question via https://stackoverflow.com/questions/33046733/force-requests-to-use-ipv4-ipv6. I do not know how to do it in golang. Could someone one help me?
Thanks so much!

@katiehockman katiehockman changed the title How can I cancel a Dial in net/http/Transport DialContext or "how can http client use only tcp6." net/http: How can I cancel a Dial in net/http/Transport DialContext or "how can http client use only tcp6." Oct 29, 2018

@katiehockman

This comment has been minimized.

Copy link
Contributor

commented Oct 29, 2018

I'm closing this issue. Please use a forum, where there will be more people who can help you. See https://golang.org/wiki/Questions . Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.