Skip to content

Commit

Permalink
ccl/sqlproxyccl: replace errPanicWriter with a writer that returns an…
Browse files Browse the repository at this point in the history
… error

Previously, we had an errPanicWriter struct that panics whenever a Write
call was made. This is used because Receive on the pgproto3 backend and
frontend instances must not call Write. However, panics are not ideal, so
this commit replaces that with a regular writer that just returns an error
when Write is called.

Release note: None
  • Loading branch information
jaylim-crl committed Feb 15, 2022
1 parent f374873 commit 179cbe3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pkg/ccl/sqlproxyccl/interceptor/backend_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func (bi *BackendInterceptor) ReadMsg() (msg pgproto3.FrontendMessage, err error
if err != nil {
return nil, err
}
// errPanicWriter is used here because Receive must not Write.
return pgproto3.NewBackend(newChunkReader(msgBytes), &errPanicWriter{}).Receive()
// errWriter is used here because Receive must not Write.
return pgproto3.NewBackend(newChunkReader(msgBytes), &errWriter{}).Receive()
}

// WriteMsg writes the given bytes to the writer dst. This is just a helper
Expand Down
12 changes: 7 additions & 5 deletions pkg/ccl/sqlproxyccl/interceptor/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,14 @@ func (p *pgInterceptor) ensureNextNBytes(n int) error {
return err
}

var _ io.Writer = &errPanicWriter{}
var _ io.Writer = &errWriter{}

// errPanicWriter is an io.Writer that panics whenever a Write call is made.
type errPanicWriter struct{}
// errWriter is an io.Writer that fails whenever a Write call is made. This is
// used within ReadMsg for both BackendInterceptor and FrontendInterceptor.
// Since it's just a Read, Write calls should not be made.
type errWriter struct{}

// Write implements the io.Writer interface.
func (w *errPanicWriter) Write(p []byte) (int, error) {
panic("unexpected Write call")
func (w *errWriter) Write(p []byte) (int, error) {
return 0, errors.AssertionFailedf("unexpected Write call")
}
4 changes: 2 additions & 2 deletions pkg/ccl/sqlproxyccl/interceptor/frontend_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (fi *FrontendInterceptor) ReadMsg() (msg pgproto3.BackendMessage, err error
if err != nil {
return nil, err
}
// errPanicWriter is used here because Receive must not Write.
return pgproto3.NewFrontend(newChunkReader(msgBytes), &errPanicWriter{}).Receive()
// errWriter is used here because Receive must not Write.
return pgproto3.NewFrontend(newChunkReader(msgBytes), &errWriter{}).Receive()
}

// WriteMsg writes the given bytes to the writer dst. This is just a helper
Expand Down

0 comments on commit 179cbe3

Please sign in to comment.