aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-08-16 23:21:43 +0700
committerShulhan <ms@kilabit.info>2022-08-16 23:21:43 +0700
commitd769cfe466e2a5f96d396f91f6000c4fc604d836 (patch)
treedcea4cfcdb198cc46c97830f2eb17b4fde4f4d98
parent1ade2eebf51bd64c60b797c077b436b3221202bf (diff)
downloadhaminer-d769cfe466e2a5f96d396f91f6000c4fc604d836.tar.xz
all: use fixed []byte for consuming UDP packet
Instead of using struct UDPPacket to read UDP packet from HAproxy log, simplify it by using fixed, reusable size of []byte directly.
-rw-r--r--halog.go21
-rw-r--r--haminer.go28
-rw-r--r--udppacket.go35
3 files changed, 23 insertions, 61 deletions
diff --git a/halog.go b/halog.go
index b7ac16d..8d4f304 100644
--- a/halog.go
+++ b/halog.go
@@ -365,26 +365,25 @@ func (halog *Halog) Parse(in []byte, reqHeaders []string) (ok bool) {
//
// It will return nil and false if UDP packet is nil, have zero length, or
// cannot be parsed (rejected).
-func (halog *Halog) ParseUDPPacket(p *UDPPacket, reqHeaders []string) bool {
- if p == nil {
- return false
- }
- if len(p.Bytes) == 0 {
+func (halog *Halog) ParseUDPPacket(packet []byte, reqHeaders []string) bool {
+ if len(packet) == 0 {
return false
}
- var in []byte
+ var (
+ endIdx int
+ in []byte
+ )
- if p.Bytes[0] == '<' {
- endIdx := bytes.IndexByte(p.Bytes, '>')
+ if packet[0] == '<' {
+ endIdx = bytes.IndexByte(packet, '>')
if endIdx < 0 {
return false
}
- in = make([]byte, len(p.Bytes))
- copy(in, p.Bytes[endIdx+1:])
+ in = packet[endIdx+1:]
} else {
- in = p.Bytes
+ in = packet
}
return halog.Parse(in, reqHeaders)
diff --git a/haminer.go b/haminer.go
index 7a54e84..95dfe62 100644
--- a/haminer.go
+++ b/haminer.go
@@ -106,20 +106,23 @@ func (h *Haminer) filter(halog *Halog) bool {
func (h *Haminer) consume() {
var (
- err error
- ok bool
- p = NewUDPPacket(0)
+ packet = make([]byte, 4096)
+
+ halog *Halog
+ err error
+ n int
+ ok bool
)
for h.isRunning {
- _, err = h.udpConn.Read(p.Bytes)
+ n, err = h.udpConn.Read(packet)
if err != nil {
continue
}
- halog := &Halog{}
+ halog = &Halog{}
- ok = halog.ParseUDPPacket(p, h.cfg.RequestHeaders)
+ ok = halog.ParseUDPPacket(packet[:n], h.cfg.RequestHeaders)
if !ok {
continue
}
@@ -130,14 +133,6 @@ func (h *Haminer) consume() {
}
h.chHalog <- halog
-
- p.Reset()
- }
-}
-
-func (h *Haminer) forwards(halogs []*Halog) {
- for _, fwder := range h.ff {
- fwder.Forwards(halogs)
}
}
@@ -163,7 +158,10 @@ func (h *Haminer) produce() {
continue
}
- h.forwards(halogs)
+ for _, fwder := range h.ff {
+ fwder.Forwards(halogs)
+ }
+
halogs = halogs[:0]
}
}
diff --git a/udppacket.go b/udppacket.go
deleted file mode 100644
index fd52a61..0000000
--- a/udppacket.go
+++ /dev/null
@@ -1,35 +0,0 @@
-// Copyright 2018, M. Shulhan (ms@kilabit.info). All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-package haminer
-
-const (
- defSize = 1024
-)
-
-// UDPPacket wrap the slice of bytes for easy manipulation.
-type UDPPacket struct {
- Bytes []byte
-}
-
-// NewUDPPacket will create and initialize UDP packet.
-func NewUDPPacket(size uint32) (p *UDPPacket) {
- if size <= 0 {
- size = defSize
- }
- p = &UDPPacket{
- Bytes: make([]byte, size),
- }
-
- return
-}
-
-// Reset will set the content of packet data to zero, so it can be used
-// against on Read().
-func (p *UDPPacket) Reset() {
- p.Bytes[0] = 0
- for x := 1; x < len(p.Bytes); x *= 2 {
- copy(p.Bytes[x:], p.Bytes[:x])
- }
-}