Skip to content

Commit b2d6a88

Browse files
committed
refactor(backend, transport): add pooled packet
1 parent 2dffcda commit b2d6a88

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

backend/pkg/transport/packet/data/decoder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func (decoder *Decoder) Decode(id abstraction.PacketId, reader io.Reader) (abstr
3535
return nil, ErrUnexpectedId{Id: id}
3636
}
3737

38+
//packet := GetPacket(id)
3839
packet := NewPacket(id)
3940
for _, value := range descriptor {
4041
val, err := value.Decode(decoder.endianness, reader)

backend/pkg/transport/packet/data/packet.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package data
22

33
import (
4+
"sync"
45
"time"
56

67
"github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction"
@@ -27,6 +28,16 @@ func NewPacket(id abstraction.PacketId) *Packet {
2728
}
2829
}
2930

31+
var packetPool = sync.Pool{
32+
New: func() any {
33+
return &Packet{
34+
values: make(map[ValueName]Value),
35+
enabled: make(map[ValueName]bool),
36+
}
37+
},
38+
}
39+
40+
3041
// NewPacketWithValues creates a new data packet with the given values
3142
func NewPacketWithValues(id abstraction.PacketId, values map[ValueName]Value, enabled map[ValueName]bool) *Packet {
3243
return &Packet{
@@ -62,3 +73,35 @@ func (packet *Packet) SetTimestamp(timestamp time.Time) *Packet {
6273
packet.timestamp = timestamp
6374
return packet
6475
}
76+
77+
func (packet *Packet) Reset() {
78+
clear(packet.values)
79+
clear(packet.enabled)
80+
packet.id = 0
81+
packet.timestamp = time.Time{}
82+
}
83+
84+
func GetPacket(id abstraction.PacketId) *Packet {
85+
p := packetPool.Get().(*Packet)
86+
if p.values == nil {
87+
p.values = make(map[ValueName]Value)
88+
} else {
89+
clear(p.values)
90+
}
91+
if p.enabled == nil {
92+
p.enabled = make(map[ValueName]bool)
93+
} else {
94+
clear(p.enabled)
95+
}
96+
p.id = id
97+
p.timestamp = time.Now()
98+
return p
99+
}
100+
101+
func ReleasePacket(p *Packet) {
102+
if p == nil {
103+
return
104+
}
105+
p.Reset()
106+
packetPool.Put(p)
107+
}

backend/pkg/transport/transport.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ func (transport *Transport) readLoopTCPConn(conn net.Conn, logger zerolog.Logger
259259

260260
logger.Trace().Type("type", packet).Msg("packet")
261261
transport.api.Notification(NewPacketNotification(packet, from, to, time.Now()))
262+
263+
//if dataPacket, ok := packet.(*data.Packet); ok {
264+
// data.ReleasePacket(dataPacket)
265+
//}
262266
}
263267
}()
264268
}
@@ -464,6 +468,10 @@ func (transport *Transport) handleUDPPacket(udpPacket udp.Packet) {
464468

465469
// Send notification
466470
transport.api.Notification(NewPacketNotification(packet, srcAddr, dstAddr, udpPacket.Timestamp))
471+
472+
//if dataPacket, ok := packet.(*data.Packet); ok {
473+
// data.ReleasePacket(dataPacket)
474+
//s}
467475
}
468476

469477
// handleConversation is called when the sniffer detects a new conversation and handles its specific packets
@@ -488,6 +496,10 @@ func (transport *Transport) handleConversation(socket network.Socket, reader io.
488496

489497
// Send notification
490498
transport.api.Notification(NewPacketNotification(packet, srcAddr, dstAddr, time.Now()))
499+
500+
//if dataPacket, ok := packet.(*data.Packet); ok {
501+
// data.ReleasePacket(dataPacket)
502+
//}
491503
}
492504
}()
493505
}

0 commit comments

Comments
 (0)