1
0
mirror of https://github.com/golang/go synced 2024-11-17 19:34:50 -07:00

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 <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>
This commit is contained in:
Ludi Rehak 2022-08-03 13:48:15 -07:00 committed by Than McIntosh
parent 0581d69dc6
commit adac8acf88

View File

@ -293,9 +293,9 @@ type conn struct {
// on this connection, if any. // on this connection, if any.
lastMethod string 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 guards hijackedv
mu sync.Mutex mu sync.Mutex
@ -749,7 +749,7 @@ func (cr *connReader) handleReadError(_ error) {
// may be called from multiple goroutines. // may be called from multiple goroutines.
func (cr *connReader) closeNotify() { func (cr *connReader) closeNotify() {
res, _ := cr.conn.curReq.Load().(*response) res := cr.conn.curReq.Load()
if res != nil && atomic.CompareAndSwapInt32(&res.didCloseNotify, 0, 1) { if res != nil && atomic.CompareAndSwapInt32(&res.didCloseNotify, 0, 1) {
res.closeNotifyCh <- true res.closeNotifyCh <- true
} }
@ -1787,7 +1787,7 @@ func (c *conn) setState(nc net.Conn, state ConnState, runHook bool) {
panic("internal error") panic("internal error")
} }
packedState := uint64(time.Now().Unix()<<8) | uint64(state) packedState := uint64(time.Now().Unix()<<8) | uint64(state)
atomic.StoreUint64(&c.curState.atomic, packedState) c.curState.Store(packedState)
if !runHook { if !runHook {
return return
} }
@ -1797,7 +1797,7 @@ func (c *conn) setState(nc net.Conn, state ConnState, runHook bool) {
} }
func (c *conn) getState() (state ConnState, unixSec int64) { 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) return ConnState(packedState & 0xff), int64(packedState >> 8)
} }
@ -2002,7 +2002,7 @@ func (c *conn) serve(ctx context.Context) {
return return
} }
c.setState(c.rwc, StateIdle, runHooks) c.setState(c.rwc, StateIdle, runHooks)
c.curReq.Store((*response)(nil)) c.curReq.Store(nil)
if !w.conn.server.doKeepAlives() { if !w.conn.server.doKeepAlives() {
// We're in shutdown mode. We might've replied // We're in shutdown mode. We might've replied