[go] net/http: make Request.WithContext documentation less prescriptive

148 views
Skip to first unread message

Damien Neil (Gerrit)

unread,
Jun 16, 2022, 4:39:50 PM6/16/22
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Damien Neil has uploaded this change for review.

View Change

net/http: make Request.WithContext documentation less prescriptive

WithContext makes a shallow copy of a Request, and Clone makes a
deep copy. Both set the context of the new request. The distinction
between the two is clear, and it doesn't seem useful or necessary
to say that "it's rare to need WithContext".

Also update a couple locations that mention WithContext to mention
Clone as well.

Fixes #53413.

Change-Id: I89e6ddebd7d5ca6573e522fe48cd7f50cc645cdd
---
M src/net/http/request.go
1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/src/net/http/request.go b/src/net/http/request.go
index d091f3c..2180cac 100644
--- a/src/net/http/request.go
+++ b/src/net/http/request.go
@@ -317,14 +317,14 @@
Response *Response

// ctx is either the client or server context. It should only
- // be modified via copying the whole Request using WithContext.
+ // be modified via copying the whole Request using Clone or WithContext.
// It is unexported to prevent people from using Context wrong
// and mutating the contexts held by callers of the same request.
ctx context.Context
}

// Context returns the request's context. To change the context, use
-// WithContext.
+// Clone or WithContext.
//
// The returned context is always non-nil; it defaults to the
// background context.
@@ -347,11 +347,6 @@
// For outgoing client request, the context controls the entire
// lifetime of a request and its response: obtaining a connection,
// sending the request, and reading the response headers and body.
-//
-// To create a new request with a context, use NewRequestWithContext.
-// To change the context of a request, such as an incoming request you
-// want to modify before sending back out, use Request.Clone. Between
-// those two uses, it's rare to need WithContext.
func (r *Request) WithContext(ctx context.Context) *Request {
if ctx == nil {
panic("nil context")

To view, visit change 412778. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I89e6ddebd7d5ca6573e522fe48cd7f50cc645cdd
Gerrit-Change-Number: 412778
Gerrit-PatchSet: 1
Gerrit-Owner: Damien Neil <dn...@google.com>
Gerrit-MessageType: newchange

Damien Neil (Gerrit)

unread,
Jun 16, 2022, 4:40:41 PM6/16/22
to goph...@pubsubhelper.golang.org, Brad Fitzpatrick, golang-co...@googlegroups.com

Attention is currently required from: Brad Fitzpatrick.

Patch set 1:Run-TryBot +1

View Change

    To view, visit change 412778. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I89e6ddebd7d5ca6573e522fe48cd7f50cc645cdd
    Gerrit-Change-Number: 412778
    Gerrit-PatchSet: 1
    Gerrit-Owner: Damien Neil <dn...@google.com>
    Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
    Gerrit-Reviewer: Damien Neil <dn...@google.com>
    Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
    Gerrit-Comment-Date: Thu, 16 Jun 2022 20:40:38 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    David Ashby (Gerrit)

    unread,
    Aug 3, 2022, 2:37:47 PM8/3/22
    to Damien Neil, goph...@pubsubhelper.golang.org, Gopher Robot, Brad Fitzpatrick, golang-co...@googlegroups.com

    Attention is currently required from: Brad Fitzpatrick, Damien Neil.

    View Change

    1 comment:

    • Patchset:

      • Patch Set #1:

        Anything else this needs to get merged now that we're outside the 1.19 freeze window, or are we just waiting for a review?

    To view, visit change 412778. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I89e6ddebd7d5ca6573e522fe48cd7f50cc645cdd
    Gerrit-Change-Number: 412778
    Gerrit-PatchSet: 1
    Gerrit-Owner: Damien Neil <dn...@google.com>
    Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
    Gerrit-Reviewer: Damien Neil <dn...@google.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-CC: David Ashby <delta.m...@gmail.com>
    Gerrit-Attention: Damien Neil <dn...@google.com>
    Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
    Gerrit-Comment-Date: Wed, 03 Aug 2022 14:19:07 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Brad Fitzpatrick (Gerrit)

    unread,
    Aug 5, 2022, 12:52:38 PM8/5/22
    to Damien Neil, goph...@pubsubhelper.golang.org, David Ashby, Gopher Robot, Brad Fitzpatrick, golang-co...@googlegroups.com

    Attention is currently required from: Damien Neil.

    View Change

    1 comment:

    • File src/net/http/request.go:

      • Patch Set #1, Line 351: // To create a new request with a context, use NewRequestWithContext.

        I'd like to preserve this mention of NewRequestWithContext which is more efficient than using WithContext.

        Maybe just drop the last sentence?

    To view, visit change 412778. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I89e6ddebd7d5ca6573e522fe48cd7f50cc645cdd
    Gerrit-Change-Number: 412778
    Gerrit-PatchSet: 1
    Gerrit-Owner: Damien Neil <dn...@google.com>
    Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
    Gerrit-Reviewer: Damien Neil <dn...@google.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-CC: David Ashby <delta.m...@gmail.com>
    Gerrit-Attention: Damien Neil <dn...@google.com>
    Gerrit-Comment-Date: Fri, 05 Aug 2022 16:52:34 +0000

    Brad Fitzpatrick (Gerrit)

    unread,
    Aug 5, 2022, 12:52:53 PM8/5/22
    to Damien Neil, goph...@pubsubhelper.golang.org, Brad Fitzpatrick, David Ashby, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Damien Neil.

    Patch set 1:Code-Review +2

    View Change

      To view, visit change 412778. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I89e6ddebd7d5ca6573e522fe48cd7f50cc645cdd
      Gerrit-Change-Number: 412778
      Gerrit-PatchSet: 1
      Gerrit-Owner: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-Reviewer: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-CC: David Ashby <delta.m...@gmail.com>
      Gerrit-Attention: Damien Neil <dn...@google.com>
      Gerrit-Comment-Date: Fri, 05 Aug 2022 16:52:50 +0000

      Damien Neil (Gerrit)

      unread,
      Aug 5, 2022, 12:57:48 PM8/5/22
      to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

      Attention is currently required from: Damien Neil.

      Damien Neil uploaded patch set #2 to this change.

      View Change

      The following approvals got outdated and were removed: Run-TryBot+1 by Damien Neil, TryBot-Result+1 by Gopher Robot

      net/http: make Request.WithContext documentation less prescriptive

      WithContext makes a shallow copy of a Request, and Clone makes a
      deep copy. Both set the context of the new request. The distinction
      between the two is clear, and it doesn't seem useful or necessary
      to say that "it's rare to need WithContext".

      Also update a couple locations that mention WithContext to mention
      Clone as well.

      Fixes #53413.

      Change-Id: I89e6ddebd7d5ca6573e522fe48cd7f50cc645cdd
      ---
      M src/net/http/request.go
      1 file changed, 22 insertions(+), 5 deletions(-)

      To view, visit change 412778. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I89e6ddebd7d5ca6573e522fe48cd7f50cc645cdd
      Gerrit-Change-Number: 412778
      Gerrit-PatchSet: 2
      Gerrit-Owner: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-Reviewer: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-CC: David Ashby <delta.m...@gmail.com>
      Gerrit-Attention: Damien Neil <dn...@google.com>
      Gerrit-MessageType: newpatchset

      Damien Neil (Gerrit)

      unread,
      Aug 5, 2022, 12:58:42 PM8/5/22
      to goph...@pubsubhelper.golang.org, Brad Fitzpatrick, David Ashby, Gopher Robot, golang-co...@googlegroups.com

      Patch set 1:Run-TryBot +1

      View Change

      1 comment:

      • File src/net/http/request.go:

        • I'd like to preserve this mention of NewRequestWithContext which is more efficient than using WithCo […]

          Changed to preserve the reference to NewRequestWithContext, and to reference Request.Clone as an alternative that makes a deep copy.

      To view, visit change 412778. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I89e6ddebd7d5ca6573e522fe48cd7f50cc645cdd
      Gerrit-Change-Number: 412778
      Gerrit-PatchSet: 1
      Gerrit-Owner: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-Reviewer: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-CC: David Ashby <delta.m...@gmail.com>
      Gerrit-Comment-Date: Fri, 05 Aug 2022 16:58:38 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      Comment-In-Reply-To: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-MessageType: comment

      Damien Neil (Gerrit)

      unread,
      Aug 5, 2022, 12:58:48 PM8/5/22
      to goph...@pubsubhelper.golang.org, Brad Fitzpatrick, David Ashby, Gopher Robot, golang-co...@googlegroups.com

      Patch set 2:Run-TryBot +1

      View Change

        To view, visit change 412778. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: I89e6ddebd7d5ca6573e522fe48cd7f50cc645cdd
        Gerrit-Change-Number: 412778
        Gerrit-PatchSet: 2
        Gerrit-Owner: Damien Neil <dn...@google.com>
        Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
        Gerrit-Reviewer: Damien Neil <dn...@google.com>
        Gerrit-Reviewer: Gopher Robot <go...@golang.org>
        Gerrit-CC: David Ashby <delta.m...@gmail.com>
        Gerrit-Comment-Date: Fri, 05 Aug 2022 16:58:45 +0000

        Robert Findley (Gerrit)

        unread,
        Aug 15, 2022, 5:53:42 PM8/15/22
        to Damien Neil, goph...@pubsubhelper.golang.org, Gopher Robot, Brad Fitzpatrick, David Ashby, golang-co...@googlegroups.com

        Attention is currently required from: Damien Neil.

        Patch set 2:Code-Review +1

        View Change

          To view, visit change 412778. To unsubscribe, or for help writing mail filters, visit settings.

          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-Change-Id: I89e6ddebd7d5ca6573e522fe48cd7f50cc645cdd
          Gerrit-Change-Number: 412778
          Gerrit-PatchSet: 2
          Gerrit-Owner: Damien Neil <dn...@google.com>
          Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
          Gerrit-Reviewer: Damien Neil <dn...@google.com>
          Gerrit-Reviewer: Gopher Robot <go...@golang.org>
          Gerrit-Reviewer: Robert Findley <rfin...@google.com>
          Gerrit-CC: David Ashby <delta.m...@gmail.com>
          Gerrit-Attention: Damien Neil <dn...@google.com>
          Gerrit-Comment-Date: Mon, 15 Aug 2022 21:53:35 +0000

          Damien Neil (Gerrit)

          unread,
          Aug 15, 2022, 5:54:32 PM8/15/22
          to goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Robert Findley, Gopher Robot, Brad Fitzpatrick, David Ashby, golang-co...@googlegroups.com

          Damien Neil submitted this change.

          View Change



          1 is the latest approved patch-set.
          The change was submitted with unreviewed changes in the following files:

          ```
          The name of the file: src/net/http/request.go
          Insertions: 3, Deletions: 0.

          @@ -347,6 +347,9 @@

          // For outgoing client request, the context controls the entire
          // lifetime of a request and its response: obtaining a connection,
          // sending the request, and reading the response headers and body.
          +//
          +// To create a new request with a context, use NewRequestWithContext.
          +// To make a deep copy of a request with a new context, use Request.Clone.

          func (r *Request) WithContext(ctx context.Context) *Request {
          if ctx == nil {
          panic("nil context")
          ```

          Approvals: Robert Findley: Looks good to me, but someone else must approve Brad Fitzpatrick: Looks good to me, approved Damien Neil: Run TryBots Gopher Robot: TryBots succeeded
          net/http: make Request.WithContext documentation less prescriptive

          WithContext makes a shallow copy of a Request, and Clone makes a
          deep copy. Both set the context of the new request. The distinction
          between the two is clear, and it doesn't seem useful or necessary
          to say that "it's rare to need WithContext".

          Also update a couple locations that mention WithContext to mention
          Clone as well.

          Fixes #53413.

          Change-Id: I89e6ddebd7d5ca6573e522fe48cd7f50cc645cdd
          Reviewed-on: https://go-review.googlesource.com/c/go/+/412778
          Reviewed-by: Brad Fitzpatrick <brad...@golang.org>
          Reviewed-by: Robert Findley <rfin...@google.com>
          TryBot-Result: Gopher Robot <go...@golang.org>
          Run-TryBot: Damien Neil <dn...@google.com>
          ---
          M src/net/http/request.go
          1 file changed, 27 insertions(+), 5 deletions(-)

          diff --git a/src/net/http/request.go b/src/net/http/request.go
          index cead91d..5439cb3 100644

          --- a/src/net/http/request.go
          +++ b/src/net/http/request.go
          @@ -317,14 +317,14 @@
          Response *Response

          // ctx is either the client or server context. It should only
          - // be modified via copying the whole Request using WithContext.
          + // be modified via copying the whole Request using Clone or WithContext.
          // It is unexported to prevent people from using Context wrong
          // and mutating the contexts held by callers of the same request.
          ctx context.Context
          }

          // Context returns the request's context. To change the context, use
          -// WithContext.
          +// Clone or WithContext.
          //
          // The returned context is always non-nil; it defaults to the
          // background context.
          @@ -349,9 +349,7 @@

          // sending the request, and reading the response headers and body.
           //

          // To create a new request with a context, use NewRequestWithContext.
          -// To change the context of a request, such as an incoming request you
          -// want to modify before sending back out, use Request.Clone. Between
          -// those two uses, it's rare to need WithContext.
          +// To make a deep copy of a request with a new context, use Request.Clone.

          func (r *Request) WithContext(ctx context.Context) *Request {
          if ctx == nil {
          panic("nil context")

          To view, visit change 412778. To unsubscribe, or for help writing mail filters, visit settings.

          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-Change-Id: I89e6ddebd7d5ca6573e522fe48cd7f50cc645cdd
          Gerrit-Change-Number: 412778
          Gerrit-PatchSet: 3
          Gerrit-Owner: Damien Neil <dn...@google.com>
          Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
          Gerrit-Reviewer: Damien Neil <dn...@google.com>
          Gerrit-Reviewer: Gopher Robot <go...@golang.org>
          Gerrit-Reviewer: Robert Findley <rfin...@google.com>
          Gerrit-CC: David Ashby <delta.m...@gmail.com>
          Gerrit-MessageType: merged
          Reply all
          Reply to author
          Forward
          0 new messages