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 { 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() s.mu.Lock()
blackhole := s.blackhole4 blackhole := s.blackhole4
conn := s.ipv4 conn := s.ipv4

View file

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