From 3f3b8bda70897461e91a9f839205b0acc5ccac1c Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Tue, 20 Jul 2021 15:52:18 +0100 Subject: [PATCH] permit underlying conn to implement batch interface directly --- conn_oob.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/conn_oob.go b/conn_oob.go index 143906c0..8f888a62 100644 --- a/conn_oob.go +++ b/conn_oob.go @@ -113,9 +113,20 @@ func newConn(c OOBCapablePacketConn) (*oobConn, error) { return nil, errors.New("activating packet info failed for both IPv4 and IPv6") } } + + // Allows callers to pass in a connection that already satisfies batchConn interface + // to make use of the optimisation. Otherwise, ipv4.NewPacketConn would unwrap the file descriptor + // via SyscallConn(), and read it that way, which might not be what the caller wants. + var bc batchConn + if ibc, ok := c.(batchConn); ok { + bc = ibc + } else { + bc = ipv4.NewPacketConn(c) + } + oobConn := &oobConn{ OOBCapablePacketConn: c, - batchConn: ipv4.NewPacketConn(c), + batchConn: bc, messages: make([]ipv4.Message, batchSize), readPos: batchSize, }