← 中文资源

简体中文版本

路径 MTU 发现

路径 MTU 发现沿路由探测最小链路容量,让端点发送能避免分片的最大包;现代方案改用包化层探测而非依赖 ICMP。

阅读英文原文

概念定义

每一跳都有最大传输单元,超过某跳容量的包无法完整通过。端点希望用“恰好放得下”的最大包以减少单位数据的包数。发现机制负责找到这个上限:尝试、观察、适应。

工作机制

经典 PMTUD 设置不分片位并信任路由器用 ICMP 报告丢弃;一旦这些消息被防火墙过滤,发送端永远学不会缩小包,连接就挂起。数据包包化层 PMTUD(DPLPMTUD)改发应用层探测包,从送达与否推断,完全不依赖 ICMP。

使用场景

隧道与叠加网络运维必须把封装头计入外层路径 MTU。QUIC 实现默认采用 DPLPMTUD。出现“小包通、大传输卡”的症状画像时应第一时间怀疑 MTU。

常见误区

MTU 不匹配会伪装成应用挂起、超时或文件传一半。放行正确的 ICMP 类型、用探测工具验证、在隧道上钳制 MSS 都是标准缓解。双向路径的 MTU 可能不同,两个方向都要测;全局假设 1500 会在巨型帧或封装环境里翻车。

代码示例

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.animate-fade {
  animation: fadeIn 0.3s ease-out forwards;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}
class CacheNode<K, V> {
  public previous: CacheNode<K, V> | null = null;
  public next: CacheNode<K, V> | null = null;

  public constructor(
    public readonly key: K,
    public value: V,
    public frequency: number,
  ) {}
}

class FrequencyList<K, V> {
  private head: CacheNode<K, V> | null = null;
  private tail: CacheNode<K, V> | null = null;
  public size = 0;

  public addFront(node: CacheNode<K, V>): void {
    node.previous = null;
    node.next = this.head;
    if (this.head !== null) this.head.previous = node;
    this.head = node;
    if (this.tail === null) this.tail = node;
    this.size += 1;
  }

  public remove(node: CacheNode<K, V>): void {
    if (node.previous !== null) node.previous.next = node.next;
    else this.head = node.next;
    if (node.next !== null) node.next.previous = node.previous;
    else this.tail = node.previous;
    node.previous = null;
    node.next = null;
    this.size -= 1;
  }

  public removeLast(): CacheNode<K, V> | null {
    if (this.tail === null) return null;
    const node = this.tail;
    this.remove(node);
    return node;
  }
}

export class LfuCache<K, V> {
  private readonly entries = new Map<K, CacheNode<K, V>>();
  private readonly frequencies = new Map<number, FrequencyList<K, V>>();
  private lowestFrequency = 0;

  public constructor(private readonly capacity: number) {
    if (!Number.isInteger(capacity) || capacity < 1) {
      throw new RangeError("capacity must be a positive integer");
    }
  }

  public get size(): number {
    return this.entries.size;
  }

  public get(key: K): V | undefined {
    const node = this.entries.get(key);
    if (node === undefined) return undefined;
    this.touch(node);
    return node.value;
  }

  public peek(key: K): V | undefined {
    return this.entries.get(key)?.value;
  }

  public put(key: K, value: V): this {
    const existing = this.entries.get(key);
    if (existing !== undefined) {
      existing.value = value;
      this.touch(existing);
      return this;
    }

    if (this.entries.size >= this.capacity) this.evict();
    const node = new CacheNode(key, value, 1);
    this.entries.set(key, node);
    this.listFor(1).addFront(node);
    this.lowestFrequency = 1;
    return this;
  }

  public delete(key: K): boolean {
    const node = this.entries.get(key);
    if (node === undefined) return false;
    this.removeNode(node);
    return true;
  }

  private touch(node: CacheNode<K, V>): void {
    const oldFrequency = node.frequency;
    const oldList = this.frequencies.get(oldFrequency);
    if (oldList === undefined) {
      throw new Error("cache invariant violated");
    }
    oldList.remove(node);
    if (oldList.size === 0) {
      this.frequencies.delete(oldFrequency);
      if (this.lowestFrequency === oldFrequency) {
        this.lowestFrequency = oldFrequency + 1;
      }
    }
    node.frequency = oldFrequency + 1;
    this.listFor(node.frequency).addFront(node);
  }

  private evict(): void {
    const list = this.frequencies.get(this.lowestFrequency);
    if (list === undefined) return;
    const node = list.removeLast();
    if (node === null) return;
    this.entries.delete(node.key);
    if (list.size === 0) this.frequencies.delete(this.lowestFrequency);
  }

  private removeNode(node: CacheNode<K, V>): void {
    const list = this.frequencies.get(node.frequency);
    if (list === undefined) {
      throw new Error("cache invariant violated");
    }
    list.remove(node);
    this.entries.delete(node.key);
    if (list.size === 0) this.frequencies.delete(node.frequency);
  }

  private listFor(frequency: number): FrequencyList<K, V> {
    let list = this.frequencies.get(frequency);
    if (list === undefined) {
      list = new FrequencyList<K, V>();
      this.frequencies.set(frequency, list);
    }
    return list;
  }
}

const cache = new LfuCache<string, number>(2);
cache.put("a", 1).put("b", 2);
console.log(cache.get("a"));
cache.put("c", 3);
console.log(cache.get("b"), cache.peek("a"), cache.get("c"));
cache.put("d", 4);
console.log(cache.get("a"), cache.get("c"), cache.get("d"));
console.log(cache.size);