avoid making copies of slices since we need to minimize the time spent in packet read in order not to drop packets

Signed-off-by: kullanici0606 <yakup.turgut@btk.gov.tr>
This commit is contained in:
kullanici0606 2023-09-20 10:05:50 +03:00
parent b0c6d983e1
commit aaaf26c074
2 changed files with 6 additions and 3 deletions

View file

@ -65,6 +65,7 @@ func benchmarkUDPListener(times int, b *testing.B) {
// there are more events than input lines, need bigger buffer
events := make(chan event.Events, len(bytesInput)*times*2)
udpChan := make(chan []byte, len(bytesInput)*times*2)
l := listener.StatsDUDPListener{
EventHandler: &event.UnbufferedEventHandler{C: events},
@ -74,6 +75,7 @@ func benchmarkUDPListener(times int, b *testing.B) {
LinesReceived: linesReceived,
SamplesReceived: samplesReceived,
TagsReceived: tagsReceived,
UdpPacketQueue: udpChan,
}
// resume benchmark timer

View file

@ -66,13 +66,14 @@ func (l *StatsDUDPListener) Listen() {
level.Error(l.Logger).Log("error", err)
return
}
l.EnqueueUdpPacket(buf[0:n])
// avoid making copies of slices since we need to minimize the time spent here in order not to drop packets
l.EnqueueUdpPacket(buf, n)
}
}
func (l *StatsDUDPListener) EnqueueUdpPacket(packet []byte) {
func (l *StatsDUDPListener) EnqueueUdpPacket(packet []byte, n int) {
l.UDPPackets.Inc()
packetCopy := make([]byte, len(packet))
packetCopy := make([]byte, n)
copy(packetCopy, packet)
l.UdpPacketQueue <- packetCopy
}