From adac8acf881ac48c558a4873b4cc7551c7e592b1 Mon Sep 17 00:00:00 2001 From: Ludi Rehak Date: Wed, 3 Aug 2022 13:48:15 -0700 Subject: [PATCH] net/http: change conn.curReq type to atomic.Pointer[response] Use the newly added atomic.Pointer[T] type for atomically loading and storing type *T pointers. This has the advantage of avoiding runtime type assertions required by its predecessor, atomic.Value. To fix build failures uncovered by TryBots (caused by "panic: unaligned 64-bit atomic operation"), also change conn.curState to type atomic.Uint64 so that it is 64-bit aligned. Change-Id: I6024d12cd581adfdccc01be7eb0faa7482036614 Reviewed-on: https://go-review.googlesource.com/c/go/+/420901 Reviewed-by: Damien Neil TryBot-Result: Gopher Robot Reviewed-by: Than McIntosh Run-TryBot: Ian Lance Taylor Reviewed-by: Brad Fitzpatrick --- src/net/http/server.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/net/http/server.go b/src/net/http/server.go index 87dd412984..960f7d6482 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -293,9 +293,9 @@ type conn struct { // on this connection, if any. lastMethod string - curReq atomic.Value // of *response (which has a Request in it) + curReq atomic.Pointer[response] // (which has a Request in it) - curState struct{ atomic uint64 } // packed (unixtime<<8|uint8(ConnState)) + curState atomic.Uint64 // packed (unixtime<<8|uint8(ConnState)) // mu guards hijackedv mu sync.Mutex @@ -749,7 +749,7 @@ func (cr *connReader) handleReadError(_ error) { // may be called from multiple goroutines. func (cr *connReader) closeNotify() { - res, _ := cr.conn.curReq.Load().(*response) + res := cr.conn.curReq.Load() if res != nil && atomic.CompareAndSwapInt32(&res.didCloseNotify, 0, 1) { res.closeNotifyCh <- true } @@ -1787,7 +1787,7 @@ func (c *conn) setState(nc net.Conn, state ConnState, runHook bool) { panic("internal error") } packedState := uint64(time.Now().Unix()<<8) | uint64(state) - atomic.StoreUint64(&c.curState.atomic, packedState) + c.curState.Store(packedState) if !runHook { return } @@ -1797,7 +1797,7 @@ func (c *conn) setState(nc net.Conn, state ConnState, runHook bool) { } func (c *conn) getState() (state ConnState, unixSec int64) { - packedState := atomic.LoadUint64(&c.curState.atomic) + packedState := c.curState.Load() return ConnState(packedState & 0xff), int64(packedState >> 8) } @@ -2002,7 +2002,7 @@ func (c *conn) serve(ctx context.Context) { return } c.setState(c.rwc, StateIdle, runHooks) - c.curReq.Store((*response)(nil)) + c.curReq.Store(nil) if !w.conn.server.doKeepAlives() { // We're in shutdown mode. We might've replied -- 2.50.0