Skip to content

Algoritma: Sıralama & SeqNo

I₃ invariant'ı: Her topic için gönderim sırası korunur; client tarafında sıra kontrolü ve gap durumunda replay.


Sunucu: SeqNo Atama

  • Her topic için gateway'te Map<topic, number> (veya Redis INCR) ile monotonik artan seqNo.
  • Mesaj omurgaya { ...payload, seqNo } olarak yazılır.

İstemci: Sıra Kontrolü


Pseudocode: İstemci Sıra Kontrolü

FUNCTION onMessage(topic, seqNo, data):
  last = lastSeqPerTopic.get(topic) ?? 0

  IF seqNo == last + 1 THEN
    lastSeqPerTopic.set(topic, seqNo)
    deliverToApp(topic, data)
    sendAck(messageId)
    RETURN
  END IF

  IF seqNo > last + 1 THEN
    requestReplay(topic, fromSeq: last + 1)
    RETURN
  END IF

  // seqNo <= last → duplicate veya eski
  sendAck(messageId)  // yine de ACK gönder
  RETURN

Replay İsteği (Gap Doldurma)

Client, gap gördüğünde REPLAY(topic, fromSeq) gönderir; gateway omurgadan (veya buffer'dan) fromSeq ile gelen mesajları tekrar gönderir.


Node.js: Client lastSeq ve Kontrol

javascript
const lastSeqPerTopic = new Map();

function onMessage(msg) {
  const { topic, seqNo, data, messageId } = msg;
  const last = lastSeqPerTopic.get(topic) ?? 0;

  if (seqNo === last + 1) {
    lastSeqPerTopic.set(topic, seqNo);
    emitToApp(topic, data);
    sendAck(messageId);
    return;
  }
  if (seqNo > last + 1) {
    sendReplayRequest(topic, last + 1);
    return;
  }
  sendAck(messageId); // duplicate
}

Tam örnek: İstemci SDK, Gateway: Sub-Socket & Yayın.

Star the repo on GitHub if this documentation is useful — link in the navbar above.