Skip to content

Commit

Permalink
Merge pull request #2 from AudriusButkevicius/small-reads
Browse files Browse the repository at this point in the history
Handle small reads
  • Loading branch information
AudriusButkevicius authored Jun 27, 2019
2 parents 730b0de + ff3c123 commit c55ef61
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 6 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@ func (r *FilteredConn) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
case <-timeout:
return 0, nil, errTimeout
case pkt := <-r.recvBuffer:
copy(b[:pkt.n], pkt.buf)
n := pkt.n
if l := len(b); l < n {
n = l
}
copy(b, pkt.buf[:n])
bufPool.Put(pkt.buf[:maxPacketSize])
return pkt.n, pkt.addr, pkt.err
return n, pkt.addr, pkt.err
case <-r.closed:
return 0, nil, errClosed
}
Expand Down
3 changes: 2 additions & 1 deletion filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,13 @@ next:
select {
case conn.recvBuffer <- pkt:
default:
bufPool.Put(pkt.buf[:maxPacketSize])
atomic.AddUint64(&d.overflow, 1)
}
goto next
}
}

bufPool.Put(pkt.buf[:maxPacketSize])
atomic.AddUint64(&d.dropped, 1)
}
}

0 comments on commit c55ef61

Please sign in to comment.