Skip to content

Commit

Permalink
chore: pointer to wg
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenrinzema committed Sep 1, 2023
1 parent 1913429 commit 6bc7474
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (srv *Server) consumeCommands(ctx context.Context, conn net.Conn, reader *b
return err
}

if srv.closing.Load() {
return nil
}

// 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)
Expand Down
10 changes: 9 additions & 1 deletion wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net"
"sync"
"sync/atomic"

"log/slog"

Expand All @@ -31,6 +32,7 @@ func ListenAndServe(address string, handler ParseFn) error {
// NewServer constructs a new Postgres server using the given address and server options.
func NewServer(parse ParseFn, options ...OptionFn) (*Server, error) {
srv := &Server{
wg: &sync.WaitGroup{},
parse: parse,
logger: slog.Default(),
closer: make(chan struct{}),
Expand All @@ -52,7 +54,8 @@ func NewServer(parse ParseFn, options ...OptionFn) (*Server, error) {

// Server contains options for listening to an address.
type Server struct {
wg sync.WaitGroup
closing atomic.Bool
wg *sync.WaitGroup
logger *slog.Logger
types *pgtype.ConnInfo
Auth AuthStrategy
Expand Down Expand Up @@ -165,6 +168,11 @@ func (srv *Server) serve(ctx context.Context, conn net.Conn) error {

// Close gracefully closes the underlaying Postgres server.
func (srv *Server) Close() error {
if srv.closing.Load() {
return nil
}

srv.closing.Store(true)
close(srv.closer)
srv.wg.Wait()
return nil
Expand Down

0 comments on commit 6bc7474

Please sign in to comment.