Skip to content

Commit

Permalink
Don't start USB low-level I/O operation, if context.Context already e…
Browse files Browse the repository at this point in the history
…xpired

This is a king of small optimization. There is no reason to start I/O
operation at this case, because it will be canceled immediately after
start.
  • Loading branch information
alexpevzner committed Dec 2, 2024
1 parent b4cac28 commit 591534a
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions usbio_libusb.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,11 @@ func (iface *UsbInterface) SoftReset() error {
func (iface *UsbInterface) Send(ctx context.Context,
data []byte) (n int, err error) {

// Don't even bother to send, if context already expired
if ctx.Err() != nil {
return 0, ctx.Err()
}

// Allocate a libusb_transfer.
xfer, doneChan, err := libusbTransferAlloc()
if err != nil {
Expand Down Expand Up @@ -806,6 +811,11 @@ func (iface *UsbInterface) Send(ctx context.Context,
func (iface *UsbInterface) Recv(ctx context.Context,
data []byte) (n int, err error) {

// Don't even bother to recv, if context already expired
if ctx.Err() != nil {
return 0, ctx.Err()
}

// Some versions of Linux kernel don't allow bulk transfers to
// be larger that 16kb per URB, and libusb uses some smart-ass
// mechanism to avoid this limitation.
Expand Down

0 comments on commit 591534a

Please sign in to comment.