[go] http: Add BaseContext field to Server

116 views
Skip to first unread message

Ralph Corderoy (Gerrit)

unread,
Jun 30, 2016, 9:46:16 AM6/30/16
to Gregory Man, golang-co...@googlegroups.com
Ralph Corderoy has posted comments on this change.

http: Add BaseContext field to Server

Patch Set 1:

(2 comments)

https://go-review.googlesource.com/#/c/24660/1/src/net/http/server.go
File src/net/http/server.go:

PS1, Line 2132: // BaseContext specifies an optional context avalible in
all handlers
Spelling error "available". Missing full stop.


PS1, Line 2133: // If nil context.Background will be used
Either a comma after "nil" or insert "then". See ErrorLog above.


--
https://go-review.googlesource.com/24660
Gerrit-Reviewer: Ralph Corderoy <ra...@inputplus.co.uk>
Gerrit-HasComments: Yes

Gregory Man (Gerrit)

unread,
Jun 30, 2016, 10:50:47 AM6/30/16
to Ian Lance Taylor, golang-co...@googlegroups.com
Gregory Man uploaded a change:
https://go-review.googlesource.com/24660

http: Add BaseContext field to Server

The BaseContext field allow to specify context avalible in all handlers.

Fixes #16220

Change-Id: I1cbd4d84c1ae4af65fbb194b9f01bb09665a1cc8
---
M src/net/http/serve_test.go
M src/net/http/server.go
2 files changed, 35 insertions(+), 3 deletions(-)



diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go
index 139ce3e..bb1e019 100644
--- a/src/net/http/serve_test.go
+++ b/src/net/http/serve_test.go
@@ -4227,6 +4227,31 @@
res.Body.Close()
}

+func TestServerContext_ServerBaseContext(t *testing.T) {
+ defer afterTest(t)
+ const baseCtxTestKey = 0
+ var testString = "test123"
+
+ ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r
*Request) {
+ got := r.Context().Value(baseCtxTestKey)
+ if str, ok := got.(string); !ok {
+ t.Errorf("local baseCtxTestKey value = %T; want string", got)
+ } else if str != testString {
+ t.Errorf("local baseCtxTestKey = %q; want %q", got, testString)
+ }
+ }))
+
+ ts.Config.BaseContext = context.WithValue(context.Background(),
baseCtxTestKey, testString)
+ ts.Start()
+ defer ts.Close()
+ client := &Client{}
+ res, err := client.Get(ts.URL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ res.Body.Close()
+}
+
// https://golang.org/issue/15960
func TestHandlerSetTransferEncodingChunked(t *testing.T) {
defer afterTest(t)
diff --git a/src/net/http/server.go b/src/net/http/server.go
index 7c3237c..8f73c76 100644
--- a/src/net/http/server.go
+++ b/src/net/http/server.go
@@ -2129,6 +2129,10 @@
// standard logger.
ErrorLog *log.Logger

+ // BaseContext specifies an optional context avalible in all handlers
+ // If nil context.Background will be used
+ BaseContext context.Context
+
disableKeepAlives int32 // accessed atomically.
nextProtoOnce sync.Once // guards initialization of TLSNextProto in
Serve
nextProtoErr error
@@ -2267,9 +2271,12 @@
}
}

- // TODO: allow changing base context? can't imagine concrete
- // use cases yet.
- baseCtx := context.Background()
+ var baseCtx context.Context
+ if srv.BaseContext != nil {
+ baseCtx = srv.BaseContext
+ } else {
+ baseCtx = context.Background()
+ }
ctx := context.WithValue(baseCtx, ServerContextKey, srv)
ctx = context.WithValue(ctx, LocalAddrContextKey, l.Addr())
for {

--
https://go-review.googlesource.com/24660

Gregory Man (Gerrit)

unread,
Jun 30, 2016, 10:54:08 AM6/30/16
to Ralph Corderoy, golang-co...@googlegroups.com
Gregory Man uploaded a new patch set:
https://go-review.googlesource.com/24660

http: Add BaseContext field to Server

The BaseContext field allow to specify context avalible in all handlers.

Fixes #16220

Change-Id: I1cbd4d84c1ae4af65fbb194b9f01bb09665a1cc8
---
M src/net/http/serve_test.go
M src/net/http/server.go
2 files changed, 35 insertions(+), 3 deletions(-)


Brad Fitzpatrick (Gerrit)

unread,
Jun 30, 2016, 10:58:06 AM6/30/16
to Gregory Man, Ralph Corderoy, Brad Fitzpatrick, golang-co...@googlegroups.com
Brad Fitzpatrick has posted comments on this change.

http: Add BaseContext field to Server

Patch Set 2:

R=go1.8

Also, the subject should be "net/http: " and lowercase "add"

--
https://go-review.googlesource.com/24660
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Ralph Corderoy <ra...@inputplus.co.uk>
Gerrit-HasComments: No

Gregory Man (Gerrit)

unread,
Jun 30, 2016, 11:00:28 AM6/30/16
to Ralph Corderoy, Brad Fitzpatrick, golang-co...@googlegroups.com
Gregory Man uploaded a new patch set:
https://go-review.googlesource.com/24660

net/http: add BaseContext field to Server

The BaseContext field allow to specify context avalible in all handlers.

Fixes #16220

Change-Id: I1cbd4d84c1ae4af65fbb194b9f01bb09665a1cc8
---
M src/net/http/serve_test.go
M src/net/http/server.go
2 files changed, 35 insertions(+), 3 deletions(-)


Gregory Man (Gerrit)

unread,
Jun 30, 2016, 11:02:46 AM6/30/16
to Ralph Corderoy, Brad Fitzpatrick, golang-co...@googlegroups.com
Gregory Man uploaded a new patch set:
https://go-review.googlesource.com/24660

net/http: add BaseContext field to Server

The BaseContext field allow to specify context avalible in all handlers.

Fixes #16220

Change-Id: I1cbd4d84c1ae4af65fbb194b9f01bb09665a1cc8
---
M src/net/http/serve_test.go
M src/net/http/server.go
2 files changed, 35 insertions(+), 3 deletions(-)


Reply all
Reply to author
Forward
0 new messages