mirror of
https://github.com/fosrl/olm.git
synced 2026-03-11 05:06:43 +00:00
Try to reduce cpu when idle
This commit is contained in:
@@ -599,12 +599,12 @@ func (p *DNSProxy) runTunnelPacketSender() {
|
|||||||
defer p.wg.Done()
|
defer p.wg.Done()
|
||||||
logger.Debug("DNS tunnel packet sender goroutine started")
|
logger.Debug("DNS tunnel packet sender goroutine started")
|
||||||
|
|
||||||
ticker := time.NewTicker(1 * time.Millisecond)
|
|
||||||
defer ticker.Stop()
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
// Use blocking ReadContext instead of polling - much more CPU efficient
|
||||||
case <-p.ctx.Done():
|
// This will block until a packet is available or context is cancelled
|
||||||
|
pkt := p.tunnelEp.ReadContext(p.ctx)
|
||||||
|
if pkt == nil {
|
||||||
|
// Context was cancelled or endpoint closed
|
||||||
logger.Debug("DNS tunnel packet sender exiting")
|
logger.Debug("DNS tunnel packet sender exiting")
|
||||||
// Drain any remaining packets
|
// Drain any remaining packets
|
||||||
for {
|
for {
|
||||||
@@ -615,12 +615,6 @@ func (p *DNSProxy) runTunnelPacketSender() {
|
|||||||
pkt.DecRef()
|
pkt.DecRef()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
case <-ticker.C:
|
|
||||||
// Try to read packets
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
pkt := p.tunnelEp.Read()
|
|
||||||
if pkt == nil {
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract packet data
|
// Extract packet data
|
||||||
@@ -644,8 +638,6 @@ func (p *DNSProxy) runTunnelPacketSender() {
|
|||||||
|
|
||||||
pkt.DecRef()
|
pkt.DecRef()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// runPacketSender sends packets from netstack back to TUN
|
// runPacketSender sends packets from netstack back to TUN
|
||||||
@@ -657,18 +649,12 @@ func (p *DNSProxy) runPacketSender() {
|
|||||||
const offset = 16
|
const offset = 16
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
// Use blocking ReadContext instead of polling - much more CPU efficient
|
||||||
case <-p.ctx.Done():
|
// This will block until a packet is available or context is cancelled
|
||||||
return
|
pkt := p.ep.ReadContext(p.ctx)
|
||||||
default:
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read packets from netstack endpoint
|
|
||||||
pkt := p.ep.Read()
|
|
||||||
if pkt == nil {
|
if pkt == nil {
|
||||||
// No packet available, small sleep to avoid busy loop
|
// Context was cancelled or endpoint closed
|
||||||
time.Sleep(1 * time.Millisecond)
|
return
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract packet data as slices
|
// Extract packet data as slices
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ type PeerMonitor struct {
|
|||||||
stack *stack.Stack
|
stack *stack.Stack
|
||||||
ep *channel.Endpoint
|
ep *channel.Endpoint
|
||||||
activePorts map[uint16]bool
|
activePorts map[uint16]bool
|
||||||
portsLock sync.Mutex
|
portsLock sync.RWMutex
|
||||||
nsCtx context.Context
|
nsCtx context.Context
|
||||||
nsCancel context.CancelFunc
|
nsCancel context.CancelFunc
|
||||||
nsWg sync.WaitGroup
|
nsWg sync.WaitGroup
|
||||||
@@ -809,9 +809,9 @@ func (pm *PeerMonitor) handlePacket(packet []byte) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if we are listening on this port
|
// Check if we are listening on this port
|
||||||
pm.portsLock.Lock()
|
pm.portsLock.RLock()
|
||||||
active := pm.activePorts[uint16(port)]
|
active := pm.activePorts[uint16(port)]
|
||||||
pm.portsLock.Unlock()
|
pm.portsLock.RUnlock()
|
||||||
|
|
||||||
if !active {
|
if !active {
|
||||||
return false
|
return false
|
||||||
@@ -842,13 +842,12 @@ func (pm *PeerMonitor) runPacketSender() {
|
|||||||
defer pm.nsWg.Done()
|
defer pm.nsWg.Done()
|
||||||
logger.Debug("PeerMonitor: Packet sender goroutine started")
|
logger.Debug("PeerMonitor: Packet sender goroutine started")
|
||||||
|
|
||||||
// Use a ticker to periodically check for packets without blocking indefinitely
|
|
||||||
ticker := time.NewTicker(10 * time.Millisecond)
|
|
||||||
defer ticker.Stop()
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
// Use blocking ReadContext instead of polling - much more CPU efficient
|
||||||
case <-pm.nsCtx.Done():
|
// This will block until a packet is available or context is cancelled
|
||||||
|
pkt := pm.ep.ReadContext(pm.nsCtx)
|
||||||
|
if pkt == nil {
|
||||||
|
// Context was cancelled or endpoint closed
|
||||||
logger.Debug("PeerMonitor: Packet sender context cancelled, draining packets")
|
logger.Debug("PeerMonitor: Packet sender context cancelled, draining packets")
|
||||||
// Drain any remaining packets before exiting
|
// Drain any remaining packets before exiting
|
||||||
for {
|
for {
|
||||||
@@ -860,12 +859,6 @@ func (pm *PeerMonitor) runPacketSender() {
|
|||||||
}
|
}
|
||||||
logger.Debug("PeerMonitor: Packet sender goroutine exiting")
|
logger.Debug("PeerMonitor: Packet sender goroutine exiting")
|
||||||
return
|
return
|
||||||
case <-ticker.C:
|
|
||||||
// Try to read packets in batches
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
pkt := pm.ep.Read()
|
|
||||||
if pkt == nil {
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract packet data
|
// Extract packet data
|
||||||
@@ -889,8 +882,6 @@ func (pm *PeerMonitor) runPacketSender() {
|
|||||||
|
|
||||||
pkt.DecRef()
|
pkt.DecRef()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// dial creates a UDP connection using the netstack
|
// dial creates a UDP connection using the netstack
|
||||||
|
|||||||
Reference in New Issue
Block a user