Skip to content

Commit

Permalink
chore: lock handler using a RW mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenrinzema committed Sep 1, 2023
1 parent c268d71 commit 82881d0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func (srv *Server) consumeCommands(ctx context.Context, conn net.Conn, reader *b

// NOTE: we increase the wait group by one in order to make sure that idle
// connections are not blocking a close.
srv.wg.Add(1)
srv.mu.RLock()
srv.logger.Debug("incoming command", slog.Int("length", length), slog.String("type", string(t)))
err = srv.handleCommand(ctx, conn, t, reader, writer)
srv.wg.Done()
srv.mu.RUnlock()
if errors.Is(err, io.EOF) {
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewServer(parse ParseFn, options ...OptionFn) (*Server, error) {

// Server contains options for listening to an address.
type Server struct {
wg sync.WaitGroup
mu sync.RWMutex
logger *slog.Logger
types *pgtype.ConnInfo
Auth AuthStrategy
Expand Down Expand Up @@ -91,11 +91,11 @@ func (srv *Server) Serve(listener net.Listener) error {

srv.logger.Info("serving incoming connections", slog.String("addr", listener.Addr().String()))

srv.wg.Add(1)
srv.mu.Add(1)

Check failure on line 94 in wire.go

View workflow job for this annotation

GitHub Actions / lint

srv.mu.Add undefined (type sync.RWMutex has no field or method Add)

// NOTE: handle graceful shutdowns
go func() {
defer srv.wg.Done()
defer srv.mu.Done()

Check failure on line 98 in wire.go

View workflow job for this annotation

GitHub Actions / lint

srv.mu.Done undefined (type sync.RWMutex has no field or method Done)
<-srv.closer

err := listener.Close()
Expand Down Expand Up @@ -166,6 +166,6 @@ func (srv *Server) serve(ctx context.Context, conn net.Conn) error {
// Close gracefully closes the underlaying Postgres server.
func (srv *Server) Close() error {
close(srv.closer)
srv.wg.Wait()
srv.mu.Lock()
return nil
}

0 comments on commit 82881d0

Please sign in to comment.