diff --git a/device/channels.go b/device/channels.go index 1eaec56..45b2a76 100644 --- a/device/channels.go +++ b/device/channels.go @@ -83,10 +83,16 @@ func newAutodrainingInboundQueue(device *Device) *autodrainingInboundQueue { q := &autodrainingInboundQueue{ c: make(chan *QueueInboundElementsContainer, QueueInboundSize), } - runtime.SetFinalizer(q, device.flushInboundQueue) + if device.needsInboundQueueFinalizer() { + runtime.SetFinalizer(q, device.flushInboundQueue) + } return q } +func (device *Device) needsInboundQueueFinalizer() bool { + return device.pool.messageBuffers.hasAccounting() +} + func (device *Device) flushInboundQueue(q *autodrainingInboundQueue) { for { select { @@ -116,10 +122,16 @@ func newAutodrainingOutboundQueue(device *Device) *autodrainingOutboundQueue { q := &autodrainingOutboundQueue{ c: make(chan *QueueOutboundElementsContainer, QueueOutboundSize), } - runtime.SetFinalizer(q, device.flushOutboundQueue) + if device.needsOutboundQueueFinalizer() { + runtime.SetFinalizer(q, device.flushOutboundQueue) + } return q } +func (device *Device) needsOutboundQueueFinalizer() bool { + return device.pool.messageBuffers.hasAccounting() +} + func (device *Device) flushOutboundQueue(q *autodrainingOutboundQueue) { for { select { diff --git a/device/pools.go b/device/pools.go index b7536a3..173486e 100644 --- a/device/pools.go +++ b/device/pools.go @@ -25,6 +25,10 @@ func NewWaitPool(max uint32, new func() any) *WaitPool { return p } +func (p *WaitPool) hasAccounting() bool { + return p != nil && p.max != 0 +} + func (p *WaitPool) Get() any { if p.max != 0 { p.lock.Lock()