Browse Source

Refactor handleOutgoing to eliminate duplicate error handling code

Extract error handling and cleanup logic into helper functions to avoid duplication
in nested select statements. This improves maintainability and reduces the risk of
inconsistencies when updating error handling logic.
pull/7838/head
Chris Lu 2 months ago
parent
commit
33e400eb88
  1. 34
      weed/worker/client.go

34
weed/worker/client.go

@ -356,35 +356,41 @@ func handleOutgoing(
close(errCh) close(errCh)
}() }()
// Helper function to handle stream errors and cleanup
handleStreamError := func(err error) {
if err != nil {
glog.Errorf("Failed to send message to admin: %v", err)
cmds <- grpcCommand{action: ActionStreamError, data: err}
}
}
// Helper function to cleanup resources
cleanup := func() {
close(msgCh)
<-errCh
}
for { for {
select { select {
case <-streamExit: case <-streamExit:
close(msgCh)
<-errCh
cleanup()
return return
case err := <-errCh: case err := <-errCh:
if err != nil {
glog.Errorf("Failed to send message to admin: %v", err)
cmds <- grpcCommand{action: ActionStreamError, data: err}
}
handleStreamError(err)
return return
case msg, ok := <-outgoing: case msg, ok := <-outgoing:
if !ok { if !ok {
close(msgCh)
<-errCh
cleanup()
return return
} }
select { select {
case msgCh <- msg: case msgCh <- msg:
// Message queued successfully
case <-streamExit: case <-streamExit:
close(msgCh)
<-errCh
cleanup()
return return
case err := <-errCh: case err := <-errCh:
if err != nil {
glog.Errorf("Failed to send message to admin: %v", err)
cmds <- grpcCommand{action: ActionStreamError, data: err}
}
handleStreamError(err)
return return
} }
} }

Loading…
Cancel
Save