Skip to content

Commit

Permalink
!TEMP! driver,protocol: Implement ResetSession
Browse files Browse the repository at this point in the history
Implementation stolen from the mysql go sql driver. Temporary draft to
run tests.
  • Loading branch information
Mathieu Borderé committed May 12, 2023
1 parent cec175e commit 25bae79
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
18 changes: 13 additions & 5 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ type Conn struct {
tracing client.LogLevel
}

func (c *Conn) ResetSession(ctx context.Context) error {
err := c.protocol.ConnCheck()
if err != nil {
return driver.ErrBadConn
}
return nil
}

// PrepareContext returns a prepared statement, bound to this connection.
// context is for the preparation of the statement, it must not store the
// context within the statement itself.
Expand All @@ -353,7 +361,7 @@ func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, e
if c.tracing != client.LogNone {
start = time.Now()
}
err := c.protocol.Call(ctx, &c.request, &c.response);
err := c.protocol.Call(ctx, &c.request, &c.response)
if c.tracing != client.LogNone {
c.log(c.tracing, "%.3fs request prepared: %q", time.Since(start).Seconds(), query)
}
Expand Down Expand Up @@ -392,7 +400,7 @@ func (c *Conn) ExecContext(ctx context.Context, query string, args []driver.Name
if c.tracing != client.LogNone {
start = time.Now()
}
err := c.protocol.Call(ctx, &c.request, &c.response);
err := c.protocol.Call(ctx, &c.request, &c.response)
if c.tracing != client.LogNone {
c.log(c.tracing, "%.3fs request exec: %q", time.Since(start).Seconds(), query)
}
Expand Down Expand Up @@ -428,7 +436,7 @@ func (c *Conn) QueryContext(ctx context.Context, query string, args []driver.Nam
if c.tracing != client.LogNone {
start = time.Now()
}
err := c.protocol.Call(ctx, &c.request, &c.response);
err := c.protocol.Call(ctx, &c.request, &c.response)
if c.tracing != client.LogNone {
c.log(c.tracing, "%.3fs request query: %q", time.Since(start).Seconds(), query)
}
Expand Down Expand Up @@ -588,7 +596,7 @@ func (s *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (drive
if s.tracing != client.LogNone {
start = time.Now()
}
err := s.protocol.Call(ctx, s.request, s.response);
err := s.protocol.Call(ctx, s.request, s.response)
if s.tracing != client.LogNone {
s.log(s.tracing, "%.3fs request prepared: %q", time.Since(start).Seconds(), s.sql)
}
Expand Down Expand Up @@ -627,7 +635,7 @@ func (s *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driv
if s.tracing != client.LogNone {
start = time.Now()
}
err := s.protocol.Call(ctx, s.request, s.response);
err := s.protocol.Call(ctx, s.request, s.response)
if s.tracing != client.LogNone {
s.log(s.tracing, "%.3fs request prepared: %q", time.Since(start).Seconds(), s.sql)
}
Expand Down
39 changes: 39 additions & 0 deletions internal/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net"
"sync"
"syscall"
"time"

"github.com/pkg/errors"
Expand All @@ -30,6 +31,44 @@ func newProtocol(version uint64, conn net.Conn) *Protocol {
return protocol
}

func connCheck(conn net.Conn) error {
var sysErr error

sysConn, ok := conn.(syscall.Conn)
if !ok {
return nil
}
rawConn, err := sysConn.SyscallConn()
if err != nil {
return err
}

err = rawConn.Read(func(fd uintptr) bool {
var buf [1]byte
n, err := syscall.Read(int(fd), buf[:])
switch {
case n == 0 && err == nil:
sysErr = io.EOF
case n > 0:
sysErr = syscall.EBADFD // TODO assign a sensible error here.
case err == syscall.EAGAIN || err == syscall.EWOULDBLOCK:
sysErr = nil
default:
sysErr = err
}
return true
})
if err != nil {
return err
}

return sysErr
}

func (p *Protocol) ConnCheck() error {
return connCheck(p.conn)
}

// Call invokes a dqlite RPC, sending a request message and receiving a
// response message.
func (p *Protocol) Call(ctx context.Context, request, response *Message) (err error) {
Expand Down

0 comments on commit 25bae79

Please sign in to comment.