]> Cypherpunks repositories - gostls13.git/commit
crypto/tls: make Conn.Read return (n, io.EOF) when EOF is next in buffer
authorBrad Fitzpatrick <bradfitz@golang.org>
Tue, 25 Mar 2014 17:58:35 +0000 (10:58 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 25 Mar 2014 17:58:35 +0000 (10:58 -0700)
commitf61f18d694028e5dd466dde11aa1c84bb3a434ed
tree865d6455523c2f287f61638c4106d301508d441c
parent8de04c78b7cd27ff7aad787cb016314bc31365ba
crypto/tls: make Conn.Read return (n, io.EOF) when EOF is next in buffer

Update #3514

An io.Reader is permitted to return either (n, nil)
or (n, io.EOF) on EOF or other error.

The tls package previously always returned (n, nil) for a read
of size n if n bytes were available, not surfacing errors at
the same time.

Amazon's HTTPS frontends like to hang up on clients without
sending the appropriate HTTP headers. (In their defense,
they're allowed to hang up any time, but generally a server
hangs up after a bit of inactivity, not immediately.) In any
case, the Go HTTP client tries to re-use connections by
looking at whether the response headers say to keep the
connection open, and because the connection looks okay, under
heavy load it's possible we'll reuse it immediately, writing
the next request, just as the Transport's always-reading
goroutine returns from tls.Conn.Read and sees (0, io.EOF).

But because Amazon does send an AlertCloseNotify record before
it hangs up on us, and the tls package does its own internal
buffering (up to 1024 bytes) of pending data, we have the
AlertCloseNotify in an unread buffer when our Conn.Read (to
the HTTP Transport code) reads its final bit of data in the
HTTP response body.

This change makes that final Read return (n, io.EOF) when
an AlertCloseNotify record is buffered right after, if we'd
otherwise return (n, nil).

A dependent change in the HTTP code then notes whether a
client connection has seen an io.EOF and uses that as an
additional signal to not reuse a HTTPS connection. With both
changes, the majority of Amazon request failures go
away. Without either one, 10-20 goroutines hitting the S3 API
leads to such an error rate that empirically up to 5 retries
are needed to complete an API call.

LGTM=agl, rsc
R=agl, rsc
CC=golang-codereviews
https://golang.org/cl/76400046
src/pkg/crypto/tls/conn.go
src/pkg/crypto/tls/tls_test.go