Skip to content

Commit

Permalink
Let the reader and writer shutdown cleanly before closing the server …
Browse files Browse the repository at this point in the history
…connection
  • Loading branch information
viciious committed Jul 31, 2024
1 parent b201032 commit 1e2d61f
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type IprotoServer struct {
firstError error
perf PerfCount
schemaID uint64
wg sync.WaitGroup
}

type IprotoServerOptions struct {
Expand Down Expand Up @@ -131,7 +132,10 @@ func (s *IprotoServer) Shutdown() error {
if s.onShutdown != nil {
s.onShutdown(err)
}
s.conn.Close()
go func() {
s.wg.Wait()
s.conn.Close()
}()
})

return err
Expand Down Expand Up @@ -166,15 +170,25 @@ func (s *IprotoServer) greet() (err error) {
}

func (s *IprotoServer) loop() {
go s.read()
go s.write()
s.wg.Add(2)

go func() {
defer s.wg.Done()
s.read()
}()

go func() {
defer s.wg.Done()
s.write()
}()
}

func (s *IprotoServer) read() {
var err error
var pp *BinaryPacket

r := s.reader
var wg sync.WaitGroup

READER_LOOP:
for {
Expand All @@ -193,8 +207,10 @@ READER_LOOP:
s.perf.NetPacketsIn.Add(1)
}

wg.Add(1)
go func(pp *BinaryPacket) {
packet := &pp.packet
defer wg.Done()

err := packet.UnmarshalBinary(pp.body)

Expand Down Expand Up @@ -244,6 +260,7 @@ READER_LOOP:
if err != nil {
s.setError(err)
}
wg.Wait()
s.Shutdown()

CLEANUP_LOOP:
Expand Down

0 comments on commit 1e2d61f

Please sign in to comment.