Skip to content

Commit 8b241fe

Browse files
committed
set write buffer to same size used for read buffer
1 parent 21c5315 commit 8b241fe

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
const (
11-
readBufSize = 4096
11+
bufSize = 4096 // Size of read and write buffers.
1212
)
1313

1414
// Common errors.

udp.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type udpConn interface {
1212
net.Conn
1313

1414
ReadFromUDP([]byte) (int, *net.UDPAddr, error)
15+
SetWriteBuffer(bytes int) error
1516
WriteTo([]byte, net.Addr) (int, error)
1617
}
1718

@@ -34,12 +35,13 @@ func DialUDPContext(ctx context.Context, network string, laddr, raddr *net.UDPAd
3435
if err != nil {
3536
return nil, err
3637
}
37-
return &UDPConn{
38+
uc := &UDPConn{
3839
udpConn: conn,
3940
closeChan: make(chan struct{}),
4041
ctx: ctx,
4142
errChan: make(chan error),
42-
}, nil
43+
}
44+
return uc.initialize()
4345
}
4446

4547
// ListenUDP creates a new UDP server.
@@ -53,12 +55,21 @@ func ListenUDPContext(ctx context.Context, network string, laddr *net.UDPAddr) (
5355
if err != nil {
5456
return nil, err
5557
}
56-
return &UDPConn{
58+
uc := &UDPConn{
5759
udpConn: conn,
5860
closeChan: make(chan struct{}),
5961
ctx: ctx,
6062
errChan: make(chan error),
61-
}, nil
63+
}
64+
return uc.initialize()
65+
}
66+
67+
// initialize initializes a UDP connection.
68+
func (conn *UDPConn) initialize() (*UDPConn, error) {
69+
if err := conn.udpConn.SetWriteBuffer(bufSize); err != nil {
70+
return nil, errors.Wrap(err, "setting write buffer size")
71+
}
72+
return conn, nil
6273
}
6374

6475
// Context returns the context associated with the conn.
@@ -114,7 +125,7 @@ func (conn *UDPConn) Serve(dispatcher Dispatcher) error {
114125

115126
// serve retrieves OSC packets.
116127
func (conn *UDPConn) serve(dispatcher Dispatcher, errChan chan error) {
117-
data := make([]byte, readBufSize)
128+
data := make([]byte, bufSize)
118129

119130
_, sender, err := conn.ReadFromUDP(data)
120131
if err != nil {

udp_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ func TestDialUDP(t *testing.T) {
3636
}
3737
}
3838

39+
func TestDialUDPSetWriteBufferError(t *testing.T) {
40+
uc := &UDPConn{udpConn: errConn{}}
41+
_, err := uc.initialize()
42+
if err == nil {
43+
t.Fatal("expected error, got nil")
44+
}
45+
if expected, got := `setting write buffer size: derp`, err.Error(); expected != got {
46+
t.Fatalf("expected %s, got %s", expected, got)
47+
}
48+
}
49+
3950
func TestListenUDP(t *testing.T) {
4051
if _, err := ListenUDP("asdfiauosweif", nil); err == nil {
4152
t.Fatal("expected error, got nil")
@@ -114,6 +125,7 @@ func TestUDPConnSend_OK(t *testing.T) {
114125
}
115126
}
116127

128+
// errConn is an implementation of the udpConn interface that returns errors from all it's methods.
117129
type errConn struct {
118130
udpConn
119131
}
@@ -122,6 +134,10 @@ func (e errConn) ReadFromUDP(b []byte) (int, *net.UDPAddr, error) {
122134
return 0, nil, errors.New("oops")
123135
}
124136

137+
func (e errConn) SetWriteBuffer(bytes int) error {
138+
return errors.New("derp")
139+
}
140+
125141
func TestUDPConnServe_ContextTimeout(t *testing.T) {
126142
laddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
127143
if err != nil {

0 commit comments

Comments
 (0)