1
0
mirror of https://github.com/golang/go synced 2024-11-19 02:44:44 -07:00

net/http, net/http/httputil: rename lk to mu

The conventional name for a sync.Mutex is "mu".

These "lk" names date back to a time before conventions.

Change-Id: Iee57f9f4423d04269e1125b5d82455c453aac26f
Reviewed-on: https://go-review.googlesource.com/21361
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Brad Fitzpatrick 2016-03-31 00:31:58 -07:00
parent 9f66636c93
commit e55896b9f4
3 changed files with 78 additions and 78 deletions

View File

@ -34,7 +34,7 @@ var errClosed = errors.New("i/o operation on closed connection")
// ServerConn is low-level and old. Applications should instead use Server // ServerConn is low-level and old. Applications should instead use Server
// in the net/http package. // in the net/http package.
type ServerConn struct { type ServerConn struct {
lk sync.Mutex // read-write protects the following fields mu sync.Mutex // read-write protects the following fields
c net.Conn c net.Conn
r *bufio.Reader r *bufio.Reader
re, we error // read/write errors re, we error // read/write errors
@ -62,8 +62,8 @@ func NewServerConn(c net.Conn, r *bufio.Reader) *ServerConn {
// called before Read has signaled the end of the keep-alive logic. The user // called before Read has signaled the end of the keep-alive logic. The user
// should not call Hijack while Read or Write is in progress. // should not call Hijack while Read or Write is in progress.
func (sc *ServerConn) Hijack() (c net.Conn, r *bufio.Reader) { func (sc *ServerConn) Hijack() (c net.Conn, r *bufio.Reader) {
sc.lk.Lock() sc.mu.Lock()
defer sc.lk.Unlock() defer sc.mu.Unlock()
c = sc.c c = sc.c
r = sc.r r = sc.r
sc.c = nil sc.c = nil
@ -96,29 +96,29 @@ func (sc *ServerConn) Read() (req *http.Request, err error) {
sc.pipe.EndResponse(id) sc.pipe.EndResponse(id)
} else { } else {
// Remember the pipeline id of this request // Remember the pipeline id of this request
sc.lk.Lock() sc.mu.Lock()
sc.pipereq[req] = id sc.pipereq[req] = id
sc.lk.Unlock() sc.mu.Unlock()
} }
}() }()
sc.lk.Lock() sc.mu.Lock()
if sc.we != nil { // no point receiving if write-side broken or closed if sc.we != nil { // no point receiving if write-side broken or closed
defer sc.lk.Unlock() defer sc.mu.Unlock()
return nil, sc.we return nil, sc.we
} }
if sc.re != nil { if sc.re != nil {
defer sc.lk.Unlock() defer sc.mu.Unlock()
return nil, sc.re return nil, sc.re
} }
if sc.r == nil { // connection closed by user in the meantime if sc.r == nil { // connection closed by user in the meantime
defer sc.lk.Unlock() defer sc.mu.Unlock()
return nil, errClosed return nil, errClosed
} }
r := sc.r r := sc.r
lastbody := sc.lastbody lastbody := sc.lastbody
sc.lastbody = nil sc.lastbody = nil
sc.lk.Unlock() sc.mu.Unlock()
// Make sure body is fully consumed, even if user does not call body.Close // Make sure body is fully consumed, even if user does not call body.Close
if lastbody != nil { if lastbody != nil {
@ -127,16 +127,16 @@ func (sc *ServerConn) Read() (req *http.Request, err error) {
// returned. // returned.
err = lastbody.Close() err = lastbody.Close()
if err != nil { if err != nil {
sc.lk.Lock() sc.mu.Lock()
defer sc.lk.Unlock() defer sc.mu.Unlock()
sc.re = err sc.re = err
return nil, err return nil, err
} }
} }
req, err = http.ReadRequest(r) req, err = http.ReadRequest(r)
sc.lk.Lock() sc.mu.Lock()
defer sc.lk.Unlock() defer sc.mu.Unlock()
if err != nil { if err != nil {
if err == io.ErrUnexpectedEOF { if err == io.ErrUnexpectedEOF {
// A close from the opposing client is treated as a // A close from the opposing client is treated as a
@ -161,8 +161,8 @@ func (sc *ServerConn) Read() (req *http.Request, err error) {
// Pending returns the number of unanswered requests // Pending returns the number of unanswered requests
// that have been received on the connection. // that have been received on the connection.
func (sc *ServerConn) Pending() int { func (sc *ServerConn) Pending() int {
sc.lk.Lock() sc.mu.Lock()
defer sc.lk.Unlock() defer sc.mu.Unlock()
return sc.nread - sc.nwritten return sc.nread - sc.nwritten
} }
@ -172,31 +172,31 @@ func (sc *ServerConn) Pending() int {
func (sc *ServerConn) Write(req *http.Request, resp *http.Response) error { func (sc *ServerConn) Write(req *http.Request, resp *http.Response) error {
// Retrieve the pipeline ID of this request/response pair // Retrieve the pipeline ID of this request/response pair
sc.lk.Lock() sc.mu.Lock()
id, ok := sc.pipereq[req] id, ok := sc.pipereq[req]
delete(sc.pipereq, req) delete(sc.pipereq, req)
if !ok { if !ok {
sc.lk.Unlock() sc.mu.Unlock()
return ErrPipeline return ErrPipeline
} }
sc.lk.Unlock() sc.mu.Unlock()
// Ensure pipeline order // Ensure pipeline order
sc.pipe.StartResponse(id) sc.pipe.StartResponse(id)
defer sc.pipe.EndResponse(id) defer sc.pipe.EndResponse(id)
sc.lk.Lock() sc.mu.Lock()
if sc.we != nil { if sc.we != nil {
defer sc.lk.Unlock() defer sc.mu.Unlock()
return sc.we return sc.we
} }
if sc.c == nil { // connection closed by user in the meantime if sc.c == nil { // connection closed by user in the meantime
defer sc.lk.Unlock() defer sc.mu.Unlock()
return ErrClosed return ErrClosed
} }
c := sc.c c := sc.c
if sc.nread <= sc.nwritten { if sc.nread <= sc.nwritten {
defer sc.lk.Unlock() defer sc.mu.Unlock()
return errors.New("persist server pipe count") return errors.New("persist server pipe count")
} }
if resp.Close { if resp.Close {
@ -205,11 +205,11 @@ func (sc *ServerConn) Write(req *http.Request, resp *http.Response) error {
// before signaling. // before signaling.
sc.re = ErrPersistEOF sc.re = ErrPersistEOF
} }
sc.lk.Unlock() sc.mu.Unlock()
err := resp.Write(c) err := resp.Write(c)
sc.lk.Lock() sc.mu.Lock()
defer sc.lk.Unlock() defer sc.mu.Unlock()
if err != nil { if err != nil {
sc.we = err sc.we = err
return err return err
@ -227,7 +227,7 @@ func (sc *ServerConn) Write(req *http.Request, resp *http.Response) error {
// ClientConn is low-level and old. Applications should instead use // ClientConn is low-level and old. Applications should instead use
// Client or Transport in the net/http package. // Client or Transport in the net/http package.
type ClientConn struct { type ClientConn struct {
lk sync.Mutex // read-write protects the following fields mu sync.Mutex // read-write protects the following fields
c net.Conn c net.Conn
r *bufio.Reader r *bufio.Reader
re, we error // read/write errors re, we error // read/write errors
@ -272,8 +272,8 @@ func NewProxyClientConn(c net.Conn, r *bufio.Reader) *ClientConn {
// called before the user or Read have signaled the end of the keep-alive // called before the user or Read have signaled the end of the keep-alive
// logic. The user should not call Hijack while Read or Write is in progress. // logic. The user should not call Hijack while Read or Write is in progress.
func (cc *ClientConn) Hijack() (c net.Conn, r *bufio.Reader) { func (cc *ClientConn) Hijack() (c net.Conn, r *bufio.Reader) {
cc.lk.Lock() cc.mu.Lock()
defer cc.lk.Unlock() defer cc.mu.Unlock()
c = cc.c c = cc.c
r = cc.r r = cc.r
cc.c = nil cc.c = nil
@ -307,23 +307,23 @@ func (cc *ClientConn) Write(req *http.Request) (err error) {
cc.pipe.EndResponse(id) cc.pipe.EndResponse(id)
} else { } else {
// Remember the pipeline id of this request // Remember the pipeline id of this request
cc.lk.Lock() cc.mu.Lock()
cc.pipereq[req] = id cc.pipereq[req] = id
cc.lk.Unlock() cc.mu.Unlock()
} }
}() }()
cc.lk.Lock() cc.mu.Lock()
if cc.re != nil { // no point sending if read-side closed or broken if cc.re != nil { // no point sending if read-side closed or broken
defer cc.lk.Unlock() defer cc.mu.Unlock()
return cc.re return cc.re
} }
if cc.we != nil { if cc.we != nil {
defer cc.lk.Unlock() defer cc.mu.Unlock()
return cc.we return cc.we
} }
if cc.c == nil { // connection closed by user in the meantime if cc.c == nil { // connection closed by user in the meantime
defer cc.lk.Unlock() defer cc.mu.Unlock()
return errClosed return errClosed
} }
c := cc.c c := cc.c
@ -332,11 +332,11 @@ func (cc *ClientConn) Write(req *http.Request) (err error) {
// still might be some pipelined reads // still might be some pipelined reads
cc.we = ErrPersistEOF cc.we = ErrPersistEOF
} }
cc.lk.Unlock() cc.mu.Unlock()
err = cc.writeReq(req, c) err = cc.writeReq(req, c)
cc.lk.Lock() cc.mu.Lock()
defer cc.lk.Unlock() defer cc.mu.Unlock()
if err != nil { if err != nil {
cc.we = err cc.we = err
return err return err
@ -349,8 +349,8 @@ func (cc *ClientConn) Write(req *http.Request) (err error) {
// Pending returns the number of unanswered requests // Pending returns the number of unanswered requests
// that have been sent on the connection. // that have been sent on the connection.
func (cc *ClientConn) Pending() int { func (cc *ClientConn) Pending() int {
cc.lk.Lock() cc.mu.Lock()
defer cc.lk.Unlock() defer cc.mu.Unlock()
return cc.nwritten - cc.nread return cc.nwritten - cc.nread
} }
@ -360,32 +360,32 @@ func (cc *ClientConn) Pending() int {
// concurrently with Write, but not with another Read. // concurrently with Write, but not with another Read.
func (cc *ClientConn) Read(req *http.Request) (resp *http.Response, err error) { func (cc *ClientConn) Read(req *http.Request) (resp *http.Response, err error) {
// Retrieve the pipeline ID of this request/response pair // Retrieve the pipeline ID of this request/response pair
cc.lk.Lock() cc.mu.Lock()
id, ok := cc.pipereq[req] id, ok := cc.pipereq[req]
delete(cc.pipereq, req) delete(cc.pipereq, req)
if !ok { if !ok {
cc.lk.Unlock() cc.mu.Unlock()
return nil, ErrPipeline return nil, ErrPipeline
} }
cc.lk.Unlock() cc.mu.Unlock()
// Ensure pipeline order // Ensure pipeline order
cc.pipe.StartResponse(id) cc.pipe.StartResponse(id)
defer cc.pipe.EndResponse(id) defer cc.pipe.EndResponse(id)
cc.lk.Lock() cc.mu.Lock()
if cc.re != nil { if cc.re != nil {
defer cc.lk.Unlock() defer cc.mu.Unlock()
return nil, cc.re return nil, cc.re
} }
if cc.r == nil { // connection closed by user in the meantime if cc.r == nil { // connection closed by user in the meantime
defer cc.lk.Unlock() defer cc.mu.Unlock()
return nil, errClosed return nil, errClosed
} }
r := cc.r r := cc.r
lastbody := cc.lastbody lastbody := cc.lastbody
cc.lastbody = nil cc.lastbody = nil
cc.lk.Unlock() cc.mu.Unlock()
// Make sure body is fully consumed, even if user does not call body.Close // Make sure body is fully consumed, even if user does not call body.Close
if lastbody != nil { if lastbody != nil {
@ -394,16 +394,16 @@ func (cc *ClientConn) Read(req *http.Request) (resp *http.Response, err error) {
// returned. // returned.
err = lastbody.Close() err = lastbody.Close()
if err != nil { if err != nil {
cc.lk.Lock() cc.mu.Lock()
defer cc.lk.Unlock() defer cc.mu.Unlock()
cc.re = err cc.re = err
return nil, err return nil, err
} }
} }
resp, err = http.ReadResponse(r, req) resp, err = http.ReadResponse(r, req)
cc.lk.Lock() cc.mu.Lock()
defer cc.lk.Unlock() defer cc.mu.Unlock()
if err != nil { if err != nil {
cc.re = err cc.re = err
return resp, err return resp, err

View File

@ -285,13 +285,13 @@ type maxLatencyWriter struct {
dst writeFlusher dst writeFlusher
latency time.Duration latency time.Duration
lk sync.Mutex // protects Write + Flush mu sync.Mutex // protects Write + Flush
done chan bool done chan bool
} }
func (m *maxLatencyWriter) Write(p []byte) (int, error) { func (m *maxLatencyWriter) Write(p []byte) (int, error) {
m.lk.Lock() m.mu.Lock()
defer m.lk.Unlock() defer m.mu.Unlock()
return m.dst.Write(p) return m.dst.Write(p)
} }
@ -306,9 +306,9 @@ func (m *maxLatencyWriter) flushLoop() {
} }
return return
case <-t.C: case <-t.C:
m.lk.Lock() m.mu.Lock()
m.dst.Flush() m.dst.Flush()
m.lk.Unlock() m.mu.Unlock()
} }
} }
} }

View File

@ -1015,7 +1015,7 @@ type persistConn struct {
// whether or not a connection can be reused. Issue 7569. // whether or not a connection can be reused. Issue 7569.
writeErrCh chan error writeErrCh chan error
lk sync.Mutex // guards following fields mu sync.Mutex // guards following fields
numExpectedResponses int numExpectedResponses int
closed error // set non-nil when conn is closed, before closech is closed closed error // set non-nil when conn is closed, before closech is closed
broken bool // an error has happened on this connection; marked broken so it's not reused. broken bool // an error has happened on this connection; marked broken so it's not reused.
@ -1029,30 +1029,30 @@ type persistConn struct {
// isBroken reports whether this connection is in a known broken state. // isBroken reports whether this connection is in a known broken state.
func (pc *persistConn) isBroken() bool { func (pc *persistConn) isBroken() bool {
pc.lk.Lock() pc.mu.Lock()
b := pc.broken b := pc.broken
pc.lk.Unlock() pc.mu.Unlock()
return b return b
} }
// isCanceled reports whether this connection was closed due to CancelRequest. // isCanceled reports whether this connection was closed due to CancelRequest.
func (pc *persistConn) isCanceled() bool { func (pc *persistConn) isCanceled() bool {
pc.lk.Lock() pc.mu.Lock()
defer pc.lk.Unlock() defer pc.mu.Unlock()
return pc.canceled return pc.canceled
} }
// isReused reports whether this connection is in a known broken state. // isReused reports whether this connection is in a known broken state.
func (pc *persistConn) isReused() bool { func (pc *persistConn) isReused() bool {
pc.lk.Lock() pc.mu.Lock()
r := pc.reused r := pc.reused
pc.lk.Unlock() pc.mu.Unlock()
return r return r
} }
func (pc *persistConn) cancelRequest() { func (pc *persistConn) cancelRequest() {
pc.lk.Lock() pc.mu.Lock()
defer pc.lk.Unlock() defer pc.mu.Unlock()
pc.canceled = true pc.canceled = true
pc.closeLocked(errRequestCanceled) pc.closeLocked(errRequestCanceled)
} }
@ -1087,13 +1087,13 @@ func (pc *persistConn) readLoop() {
err = beforeRespHeaderError{err} err = beforeRespHeaderError{err}
} }
pc.lk.Lock() pc.mu.Lock()
if pc.numExpectedResponses == 0 { if pc.numExpectedResponses == 0 {
pc.readLoopPeekFailLocked(err) pc.readLoopPeekFailLocked(err)
pc.lk.Unlock() pc.mu.Unlock()
return return
} }
pc.lk.Unlock() pc.mu.Unlock()
rc := <-pc.reqch rc := <-pc.reqch
@ -1121,9 +1121,9 @@ func (pc *persistConn) readLoop() {
return return
} }
pc.lk.Lock() pc.mu.Lock()
pc.numExpectedResponses-- pc.numExpectedResponses--
pc.lk.Unlock() pc.mu.Unlock()
hasBody := rc.req.Method != "HEAD" && resp.ContentLength != 0 hasBody := rc.req.Method != "HEAD" && resp.ContentLength != 0
@ -1412,10 +1412,10 @@ func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err err
pc.t.putOrCloseIdleConn(pc) pc.t.putOrCloseIdleConn(pc)
return nil, errRequestCanceled return nil, errRequestCanceled
} }
pc.lk.Lock() pc.mu.Lock()
pc.numExpectedResponses++ pc.numExpectedResponses++
headerFn := pc.mutateHeaderFunc headerFn := pc.mutateHeaderFunc
pc.lk.Unlock() pc.mu.Unlock()
if headerFn != nil { if headerFn != nil {
headerFn(req.extraHeaders()) headerFn(req.extraHeaders())
@ -1531,17 +1531,17 @@ WaitResponse:
// It differs from close in that it doesn't close the underlying // It differs from close in that it doesn't close the underlying
// connection for use when it's still being read. // connection for use when it's still being read.
func (pc *persistConn) markBroken() { func (pc *persistConn) markBroken() {
pc.lk.Lock() pc.mu.Lock()
defer pc.lk.Unlock() defer pc.mu.Unlock()
pc.broken = true pc.broken = true
} }
// markReused marks this connection as having been successfully used for a // markReused marks this connection as having been successfully used for a
// request and response. // request and response.
func (pc *persistConn) markReused() { func (pc *persistConn) markReused() {
pc.lk.Lock() pc.mu.Lock()
pc.reused = true pc.reused = true
pc.lk.Unlock() pc.mu.Unlock()
} }
// close closes the underlying TCP connection and closes // close closes the underlying TCP connection and closes
@ -1550,8 +1550,8 @@ func (pc *persistConn) markReused() {
// The provided err is only for testing and debugging; in normal // The provided err is only for testing and debugging; in normal
// circumstances it should never be seen by users. // circumstances it should never be seen by users.
func (pc *persistConn) close(err error) { func (pc *persistConn) close(err error) {
pc.lk.Lock() pc.mu.Lock()
defer pc.lk.Unlock() defer pc.mu.Unlock()
pc.closeLocked(err) pc.closeLocked(err)
} }