Skip to content

Commit

Permalink
remove redundant code, Close() will now call new closing function and…
Browse files Browse the repository at this point in the history
… added return error
  • Loading branch information
laduchesneau committed Sep 30, 2024
1 parent 4d94530 commit b9118e4
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions transceivable.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,7 @@ func (t *transceivable) SystemID() string {

// Close transceiver and stop underlying daemons.
func (t *transceivable) Close() (err error) {
if atomic.CompareAndSwapInt32(&t.aliveState, Alive, Closed) {
t.cancel()

// closing input and output
_ = t.out.close(StoppingProcessOnly)
_ = t.in.close(StoppingProcessOnly)

// close underlying conn
err = t.conn.Close()

// notify transceiver closed
if t.settings.OnClosed != nil {
t.settings.OnClosed(ExplicitClosing)
}

t.wg.Wait()
}
return
return t.closing(ExplicitClosing)
}

// Submit a PDU.
Expand Down Expand Up @@ -164,7 +147,7 @@ func (t *transceivable) windowCleanup() {
_ = t.requestStore.Delete(ctx, request.GetSequenceNumber())
if t.settings.OnExpiredPduRequest != nil {
if t.settings.OnExpiredPduRequest(request.PDU) {
t.closing(ConnectionIssue)
_ = t.closing(ConnectionIssue)
}
}
}
Expand All @@ -174,20 +157,23 @@ func (t *transceivable) windowCleanup() {
}
}

func (t *transceivable) closing(state State) {
func (t *transceivable) closing(state State) (err error) {
if atomic.CompareAndSwapInt32(&t.aliveState, Alive, Closed) {
t.cancel()

t.in.closing(StoppingProcessOnly)
t.out.closing(StoppingProcessOnly)
// closing input and output
_ = t.out.close(StoppingProcessOnly)
_ = t.in.close(StoppingProcessOnly)

// close underlying conn
_ = t.conn.Close()
err = t.conn.Close()

// notify transceiver closed
if t.settings.OnClosed != nil {
t.settings.OnClosed(state)
}

t.wg.Wait()
}
return
}

0 comments on commit b9118e4

Please sign in to comment.