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

blog: update the context blog to not use deprecated method #29075

Closed
gua-pian opened this issue Dec 3, 2018 · 1 comment
Closed

blog: update the context blog to not use deprecated method #29075

gua-pian opened this issue Dec 3, 2018 · 1 comment
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.

Comments

@gua-pian
Copy link

gua-pian commented Dec 3, 2018

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

$ go version
go version go1.10 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

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/hao.dong/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/hao.dong/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
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/qf/md348s5d0yq2m339m49gjrwh0000gp/T/go-build713354838=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

func httpDo(ctx context.Context, req *http.Request, f func(*http.Response, error) error) error {
    // Run the HTTP request in a goroutine and pass the response to f.
    tr := &http.Transport{}
    client := &http.Client{Transport: tr}
    c := make(chan error, 1)
    go func() { c <- f(client.Do(req)) }()
    select {
    case <-ctx.Done():
        tr.CancelRequest(req)
        <-c // Wait for f to return.
        return ctx.Err()
    case err := <-c:
        return err
    }
}

What did you expect to see?

The CancelRequest() method of type *http.Transport is deprecated now. As https://golang.org/pkg/net/http/#Transport.CancelRequest suggested Use Request.WithContext to create a request with a cancelable context instead. Could we just update the httpDo like this:

func httpDo(ctx context.Context, req *http.Request, f func(*http.Response, error) error) error {
	client := http.Client{}
	c := make(chan error)
	subCtx, subCancel := context.WithCancel(ctx)
	newReq := req.WithContext(subCtx)
	go func() { c <- f(client.Do(newReq)) }()
	select {
	case <-ctx.Done():
                subCancel()
		<-c
		return ctx.Err()
	case err := <-c:
		return err
	}
	return nil
}

What did you see instead?

@bradfitz bradfitz added NeedsFix The path to resolution is known, but the work has not been done. help wanted labels Dec 5, 2018
@bradfitz bradfitz changed the title Blog: update the context blog, not to use deprecated method. blog: update the context blog to not use deprecated method Dec 5, 2018
@gopherbot
Copy link

Change https://golang.org/cl/153957 mentions this issue: content/context: use req.WithContext

@golang golang locked and limited conversation to collaborators Dec 21, 2019
gopherbot pushed a commit to golang/website that referenced this issue May 26, 2021
(*Transport).CancelRequest is now deprecated. Use the new
(*Request).WithContext method which inherits the parent context.

Fixes golang/go#29075

Change-Id: I4a275a396c6ed64eef73b688a049e7bc372f07d4
Reviewed-on: https://go-review.googlesource.com/c/153957
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
X-Blog-Commit: 7ed0a50146b4d79f8b1027b56aa37a966162f30e
passionSeven added a commit to passionSeven/website that referenced this issue Oct 18, 2022
(*Transport).CancelRequest is now deprecated. Use the new
(*Request).WithContext method which inherits the parent context.

Fixes golang/go#29075

Change-Id: I4a275a396c6ed64eef73b688a049e7bc372f07d4
Reviewed-on: https://go-review.googlesource.com/c/153957
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
X-Blog-Commit: 7ed0a50146b4d79f8b1027b56aa37a966162f30e
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants