]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: change conn.curReq type to atomic.Pointer[response]
authorLudi Rehak <ludi317@gmail.com>
Wed, 3 Aug 2022 20:48:15 +0000 (13:48 -0700)
committerThan McIntosh <thanm@google.com>
Mon, 8 Aug 2022 16:01:12 +0000 (16:01 +0000)
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 <dneil@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/http/server.go

index 87dd4129846f21c8ffce5553952703e039bed142..960f7d64828278dd84641c5c74b95a8b89b8f26f 100644 (file)
@@ -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