mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-18 09:46:24 +01:00
s3: answer ClientDisconnected for a GET the client aborted before the first byte (#12707)
* s3: answer ClientDisconnected for a GET the client aborted before the first byte Fixes #12706 * s3: tell a gone client on TLS and HTTP/2 too, and not on a connection the server closed
This commit is contained in:
1 parent
c91da3d1f8
commit
91eaabf371
4 files changed
+117
-9
No files matched your search
@@ -1,8 +1,12 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"os"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/glog"
|
||||
@@ -52,6 +56,8 @@ type Conn struct {
|
||||
mu sync.Mutex
|
||||
externalReadDeadline bool
|
||||
externalWriteDeadline bool
|
||||
|
||||
readEndedByServer atomic.Bool
|
||||
}
|
||||
|
||||
func (c *Conn) SetDeadline(t time.Time) error {
|
||||
@@ -107,13 +113,12 @@ func (c *Conn) extendDeadline() error {
|
||||
|
||||
func (c *Conn) Read(b []byte) (count int, e error) {
|
||||
// Extend deadline before reading - any activity keeps connection alive
|
||||
if err := c.extendDeadline(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
count, e = c.Conn.Read(b)
|
||||
if e == nil {
|
||||
stats.BytesIn(int64(count))
|
||||
if e = c.extendDeadline(); e == nil {
|
||||
if count, e = c.Conn.Read(b); e == nil {
|
||||
stats.BytesIn(int64(count))
|
||||
}
|
||||
}
|
||||
c.readEndedByServer.Store(errors.Is(e, os.ErrDeadlineExceeded) || errors.Is(e, net.ErrClosed))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -140,6 +145,29 @@ func (c *Conn) Close() error {
|
||||
return err
|
||||
}
|
||||
|
||||
type connContextKey struct{}
|
||||
|
||||
// ConnContext is an http.Server.ConnContext hook that lets ClientGone see the
|
||||
// connection serving a request.
|
||||
func ConnContext(ctx context.Context, c net.Conn) context.Context {
|
||||
if tlsConn, ok := c.(interface{ NetConn() net.Conn }); ok {
|
||||
c = tlsConn.NetConn()
|
||||
}
|
||||
if tc, ok := c.(*Conn); ok {
|
||||
return context.WithValue(ctx, connContextKey{}, tc)
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
// ClientGone reports whether the client is why the request context ctx is
|
||||
// canceled. net/http cancels it when a read of the connection fails or the
|
||||
// HTTP/2 stream is reset; a read the server ended itself, by the idle timeout
|
||||
// or by closing the connection, is not the client's doing.
|
||||
func ClientGone(ctx context.Context) bool {
|
||||
tc, _ := ctx.Value(connContextKey{}).(*Conn)
|
||||
return tc != nil && ctx.Err() == context.Canceled && !tc.readEndedByServer.Load()
|
||||
}
|
||||
|
||||
func NewListener(addr string, timeout time.Duration) (ipListener net.Listener, err error) {
|
||||
listener, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user