// MTU管理器
type MTUManager struct {
mtuData map[string]int
mtuHistory map[string][]MTUNegotiationRecord
minMTU map[string]int
maxMTU map[string]int
hysteresis float64
mu sync.RWMutex
}
func NewMTUManager() *MTUManager {
return &MTUManager{
mtuData: make(map[string]int),
mtuHistory: make(map[string][]MTUNegotiationRecord),
minMTU: make(map[string]int),
maxMTU: make(map[string]int),
hysteresis: 0.1,
}
}
func (m *MTUManager) NegotiateMTU(deviceID string, rtt int64) {
m.mu.Lock()
defer m.mu.Unlock()
if _, exists := m.mtuData[deviceID]; !exists {
m.mtuData[deviceID] = 1500
m.minMTU[deviceID] = 64
m.maxMTU[deviceID] = 4096
m.mtuHistory[deviceID] = make([]MTUNegotiationRecord, 0)
}
// 简单的MTU协商逻辑
currentMTU := m.mtuData[deviceID]
minMTU := m.minMTU[deviceID]
maxMTU := m.maxMTU[deviceID]
// 根据RTT调整MTU
if rtt < 10000 { // RTT < 10ms
// 尝试增加MTU
newMTU := currentMTU * 2
if newMTU > maxMTU {
newMTU = maxMTU
}
if newMTU > currentMTU {
m.mtuData[deviceID] = newMTU
m.mtuHistory[deviceID] = append(m.mtuHistory[deviceID], MTUNegotiationRecord{
AttemptValue: newMTU,
ResponseTime: rtt,
RetryCount: 0,
Success: true,
Timestamp: time.Now(),
})
}
} else if rtt > 50000 { // RTT > 50ms
// 尝试减少MTU
newMTU := currentMTU / 2
if newMTU < minMTU {
newMTU = minMTU
}
if newMTU < currentMTU {
m.mtuData[deviceID] = newMTU
m.mtuHistory[deviceID] = append(m.mtuHistory[deviceID], MTUNegotiationRecord{
AttemptValue: newMTU,
ResponseTime: rtt,
RetryCount: 0,
Success: true,
Timestamp: time.Now(),
})
}
}
}
func (m *MTUManager) GetCurrentMTU(deviceID string) int {
m.mu.RLock()
defer m.mu.RUnlock()
if mtu, exists := m.mtuData[deviceID]; exists {
return mtu
}
return 1500
}