FIx batched InputPackets

This commit is contained in:
世界 2026-07-06 23:38:46 +08:00
parent 57baac9504
commit 2c27bbf4f9
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 23 additions and 11 deletions

View file

@ -408,6 +408,13 @@ func (e ErrUDPGSODisabled) Unwrap() error {
}
func (s *StdNetBind) Send(bufs [][]byte, endpoint Endpoint, offset int) error {
for len(bufs) > IdealBatchSize {
err := s.Send(bufs[:IdealBatchSize], endpoint, offset)
if err != nil {
return err
}
bufs = bufs[IdealBatchSize:]
}
s.mu.Lock()
blackhole := s.blackhole4
conn := s.ipv4

View file

@ -374,7 +374,7 @@ type InputPacketRef struct {
func (device *Device) InputPackets(packets []*InputPacketRef) []*InputPacketRef {
var unmatched []*InputPacketRef
elemsByPeer := make(map[*Peer]*QueueOutboundElementsContainer, len(packets))
elemsByPeer := make(map[*Peer][]*QueueOutboundElementsContainer, len(packets))
for _, packetRef := range packets {
peer := device.allowedips.Lookup(packetRef.Destination)
if peer == nil {
@ -401,18 +401,22 @@ func (device *Device) InputPackets(packets []*InputPacketRef) []*InputPacketRef
n += copy(packet[n:], packetSlice)
}
elem.packet = packet[:n]
elemsForPeer, ok := elemsByPeer[peer]
if !ok {
elemsForPeer = device.GetOutboundElementsContainer()
elemsByPeer[peer] = elemsForPeer
containers := elemsByPeer[peer]
if len(containers) == 0 || len(containers[len(containers)-1].elems) >= conn.IdealBatchSize {
containers = append(containers, device.GetOutboundElementsContainer())
elemsByPeer[peer] = containers
}
elemsForPeer := containers[len(containers)-1]
elemsForPeer.elems = append(elemsForPeer.elems, elem)
}
for peer, elemsForPeer := range elemsByPeer {
for peer, containers := range elemsByPeer {
if peer.isRunning.Load() {
for _, elemsForPeer := range containers {
peer.StagePackets(elemsForPeer)
}
peer.SendStagedPackets()
} else {
for _, elemsForPeer := range containers {
for _, elem := range elemsForPeer.elems {
device.PutOutboundBuffer(elem.buffer)
device.PutOutboundElement(elem)
@ -420,6 +424,7 @@ func (device *Device) InputPackets(packets []*InputPacketRef) []*InputPacketRef
device.PutOutboundElementsContainer(elemsForPeer)
}
}
}
return unmatched
}