Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hmaurer committed Nov 7, 2023
1 parent 6b8851d commit 615ca5e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 5 additions & 1 deletion go/mysql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,10 @@ func (l *Listener) Accept() {
// handle is called in a go routine for each client connection.
// FIXME(alainjobart) handle per-connection logs in a way that makes sense.
func (l *Listener) handle(conn net.Conn, connectionID uint32, acceptTime time.Time) {
var connWithTimeouts netutil.ConnWithTimeouts
if l.connReadTimeout != 0 || l.connWriteTimeout != 0 {
conn = netutil.NewConnWithTimeouts(conn, l.connReadTimeout, l.connWriteTimeout)
connWithTimeouts = netutil.NewConnWithTimeouts(conn, l.connReadTimeout, l.connWriteTimeout)
conn = connWithTimeouts
}
c := newServerConn(conn, l)
c.ConnectionID = connectionID
Expand Down Expand Up @@ -519,6 +521,8 @@ func (l *Listener) handle(conn net.Conn, connectionID uint32, acceptTime time.Ti
l.handler.ConnectionReady(c)

for {
waitTimeout := 10 * time.Second
(&connWithTimeouts).SetNextReadTimeout(waitTimeout)
kontinue := c.handleNextCommand(l.handler)
if !kontinue {
return
Expand Down
14 changes: 11 additions & 3 deletions go/netutil/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var _ net.Conn = (*ConnWithTimeouts)(nil)
// A ConnWithTimeouts is a wrapper to net.Comm that allows to set a read and write timeouts.
type ConnWithTimeouts struct {
net.Conn
nextReadTimeout time.Duration
readTimeout time.Duration
writeTimeout time.Duration
}
Expand All @@ -36,13 +37,16 @@ func NewConnWithTimeouts(conn net.Conn, readTimeout time.Duration, writeTimeout

// Read sets a read deadilne and delegates to conn.Read.
func (c ConnWithTimeouts) Read(b []byte) (int, error) {
if c.readTimeout == 0 {
if c.nextReadTimeout == 0 {
return c.Conn.Read(b)
}
if err := c.Conn.SetReadDeadline(time.Now().Add(c.readTimeout)); err != nil {
if err := c.Conn.SetReadDeadline(time.Now().Add(c.nextReadTimeout)); err != nil {
return 0, err
}
return c.Conn.Read(b)
n, err := c.Conn.Read(b)
// how do I reset the read timeout here without a pointer receiver?
c.nextReadTimeout = c.readTimeout
return n, err
}

// Write sets a write deadline and delegates to conn.Write
Expand All @@ -56,6 +60,10 @@ func (c ConnWithTimeouts) Write(b []byte) (int, error) {
return c.Conn.Write(b)
}

func (c *ConnWithTimeouts) SetNextReadTimeout(timeout time.Duration) {
c.nextReadTimeout = timeout
}

// SetDeadline implements the Conn SetDeadline method.
func (c ConnWithTimeouts) SetDeadline(t time.Time) error {
panic("can't call SetDeadline for ConnWithTimeouts")
Expand Down

0 comments on commit 615ca5e

Please sign in to comment.