-
Notifications
You must be signed in to change notification settings - Fork 376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stream-management: Implement requesting ACKs #1005
Changes from 4 commits
c4d4bb8
6fa3127
710b167
ce30e8b
7ef1d8d
d76e479
69d0752
0190cb6
b2bf99c
0f79b3b
efad5c1
8cb8dff
8c408ef
43a2d69
97e8d28
f076fd1
f6e15ba
2209036
c66696c
bb3998a
4736120
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import XMPPError from "@xmpp/error"; | ||
import { procedure } from "@xmpp/events"; | ||
import { EventEmitter, procedure } from "@xmpp/events"; | ||
import xml from "@xmpp/xml"; | ||
import { datetime } from "@xmpp/time"; | ||
|
||
// https://xmpp.org/extensions/xep-0198.html | ||
|
||
|
@@ -45,24 +46,50 @@ export default function streamManagement({ | |
bind2, | ||
sasl2, | ||
}) { | ||
const sm = { | ||
let timeoutTimeout = null; | ||
let requestAckTimeout = null; | ||
|
||
const sm = new EventEmitter(); | ||
Object.assign(sm, { | ||
allowResume: true, | ||
preferredMaximum: null, | ||
enabled: false, | ||
id: "", | ||
outbound_q: [], | ||
outbound: 0, | ||
inbound: 0, | ||
max: null, | ||
}; | ||
timeout: 60_000, | ||
_teardown: () => { | ||
clearTimeout(timeoutTimeout); | ||
clearTimeout(requestAckTimeout); | ||
}, | ||
}); | ||
|
||
function resumed() { | ||
async function resumed(resumed) { | ||
sm.enabled = true; | ||
const oldOutbound = sm.outbound; | ||
for (let i = 0; i < resumed.attrs.h - oldOutbound; i++) { | ||
let stanza = sm.outbound_q.shift(); | ||
sm.outbound++; | ||
sm.emit("ack", stanza); | ||
} | ||
let q = sm.outbound_q; | ||
sm.outbound_q = []; | ||
for (const item of q) { | ||
await entity.send(item); // This will trigger the middleware and re-add to the queue | ||
} | ||
sm.emit("resumed"); | ||
entity._ready(true); | ||
} | ||
|
||
function failed() { | ||
sm.enabled = false; | ||
sm.id = ""; | ||
let stanza; | ||
while ((stanza = sm.outbound_q.shift())) { | ||
sm.emit("fail", stanza); | ||
} | ||
sm.outbound = 0; | ||
} | ||
|
||
|
@@ -73,11 +100,18 @@ export default function streamManagement({ | |
} | ||
|
||
entity.on("online", () => { | ||
if (sm.outbound_q.length > 0) { | ||
throw "Stream Management assertion failure, queue should be empty during online"; | ||
sonnyp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
sm.outbound = 0; | ||
sm.inbound = 0; | ||
}); | ||
|
||
entity.on("offline", () => { | ||
let stanza; | ||
while ((stanza = sm.outbound_q.shift())) { | ||
sm.emit("fail", stanza); | ||
} | ||
sm.outbound = 0; | ||
sm.inbound = 0; | ||
sm.enabled = false; | ||
|
@@ -86,14 +120,20 @@ export default function streamManagement({ | |
|
||
middleware.use((context, next) => { | ||
const { stanza } = context; | ||
clearTimeout(timeoutTimeout); | ||
if (["presence", "message", "iq"].includes(stanza.name)) { | ||
sm.inbound += 1; | ||
} else if (stanza.is("r", NS)) { | ||
// > When an <r/> element ("request") is received, the recipient MUST acknowledge it by sending an <a/> element to the sender containing a value of 'h' that is equal to the number of stanzas handled by the recipient of the <r/> element. | ||
entity.send(xml("a", { xmlns: NS, h: sm.inbound })).catch(() => {}); | ||
} else if (stanza.is("a", NS)) { | ||
// > When a party receives an <a/> element, it SHOULD keep a record of the 'h' value returned as the sequence number of the last handled outbound stanza for the current stream (and discard the previous value). | ||
sm.outbound = stanza.attrs.h; | ||
const oldOutbound = sm.outbound; | ||
for (let i = 0; i < stanza.attrs.h - oldOutbound; i++) { | ||
let stanza = sm.outbound_q.shift(); | ||
sm.outbound++; | ||
sm.emit("ack", stanza); | ||
} | ||
} | ||
|
||
return next(); | ||
|
@@ -105,6 +145,42 @@ export default function streamManagement({ | |
if (sasl2) { | ||
setupSasl2({ sasl2, sm, failed, resumed }); | ||
} | ||
|
||
function requestAck() { | ||
clearTimeout(timeoutTimeout); | ||
if (sm.timeout) { | ||
timeoutTimeout = setTimeout(() => entity.disconnect(), sm.timeout); | ||
sonnyp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
entity.send(xml("r", { xmlns: NS })).catch(() => {}); | ||
// Periodically send r to check the connection | ||
// If a stanza goes out it will cancel this and set a sooner timer | ||
requestAckTimeout = setTimeout(requestAck, 300_000); | ||
sonnyp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
middleware.filter((context, next) => { | ||
if (!sm.enabled) return next(); | ||
const { stanza } = context; | ||
if (!["presence", "message", "iq"].includes(stanza.name)) return next(); | ||
|
||
let qStanza = stanza; | ||
if ( | ||
qStanza.name === "message" && | ||
!qStanza.getChild("delay", "urn:xmpp:delay") | ||
) { | ||
qStanza = xml.clone(qStanza); | ||
qStanza.c("delay", { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use |
||
xmlns: "urn:xmpp:delay", | ||
from: entity.jid.toString(), | ||
stamp: datetime(), | ||
}); | ||
} | ||
sm.outbound_q.push(qStanza); | ||
// Debounce requests so we send only one after a big run of stanza together | ||
clearTimeout(requestAckTimeout); | ||
requestAckTimeout = setTimeout(requestAck, 100); | ||
sonnyp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return next(); | ||
}); | ||
|
||
if (streamFeatures) { | ||
setupStreamFeature({ | ||
streamFeatures, | ||
|
@@ -133,8 +209,7 @@ function setupStreamFeature({ | |
// Resuming | ||
if (sm.id) { | ||
try { | ||
await resume(entity, sm); | ||
resumed(); | ||
await resumed(await resume(entity, sm)); | ||
sonnyp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
// If resumption fails, continue with session establishment | ||
} catch { | ||
|
@@ -150,6 +225,9 @@ function setupStreamFeature({ | |
const promiseEnable = enable(entity, sm); | ||
|
||
// > The counter for an entity's own sent stanzas is set to zero and started after sending either <enable/> or <enabled/>. | ||
if (sm.outbound_q.length > 0) { | ||
throw "Stream Management assertion failure, queue should be empty after enable"; | ||
sonnyp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
sm.outbound = 0; | ||
|
||
try { | ||
|
@@ -172,7 +250,7 @@ function setupSasl2({ sasl2, sm, failed, resumed }) { | |
}, | ||
(element) => { | ||
if (element.is("resumed")) { | ||
resumed(); | ||
resumed(element); | ||
} else if (element.is(failed)) { | ||
// const error = StreamError.fromElement(element) | ||
failed(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ test("enable - enabled", async () => { | |
); | ||
|
||
expect(entity.streamManagement.outbound).toBe(0); | ||
expect(entity.streamManagement.outbound_q).toBeEmpty(); | ||
expect(entity.streamManagement.enabled).toBe(false); | ||
expect(entity.streamManagement.id).toBe(""); | ||
|
||
|
@@ -73,6 +74,7 @@ test("enable - message - enabled", async () => { | |
); | ||
|
||
expect(entity.streamManagement.outbound).toBe(0); | ||
expect(entity.streamManagement.outbound_q).toBeEmpty(); | ||
expect(entity.streamManagement.enabled).toBe(false); | ||
expect(entity.streamManagement.id).toBe(""); | ||
|
||
|
@@ -112,6 +114,7 @@ test("enable - failed", async () => { | |
); | ||
|
||
expect(entity.streamManagement.outbound).toBe(0); | ||
expect(entity.streamManagement.outbound_q).toBeEmpty(); | ||
entity.streamManagement.enabled = true; | ||
|
||
entity.mockInput( | ||
|
@@ -125,6 +128,54 @@ test("enable - failed", async () => { | |
expect(entity.streamManagement.enabled).toBe(false); | ||
}); | ||
|
||
test("enable - enabled - message", async () => { | ||
sonnyp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const { entity } = mockClient(); | ||
|
||
entity.mockInput( | ||
<features xmlns="http://etherx.jabber.org/streams"> | ||
<sm xmlns="urn:xmpp:sm:3" /> | ||
</features>, | ||
); | ||
|
||
expect(await entity.catchOutgoing()).toEqual( | ||
<enable xmlns="urn:xmpp:sm:3" resume="true" />, | ||
); | ||
|
||
entity.mockInput( | ||
<enabled | ||
xmlns="urn:xmpp:sm:3" | ||
id="some-long-sm-id" | ||
location="[2001:41D0:1:A49b::1]:9222" | ||
resume="true" | ||
/>, | ||
); | ||
|
||
await tick(); | ||
sonnyp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
expect(entity.streamManagement.outbound).toBe(0); | ||
expect(entity.streamManagement.outbound_q).toBeEmpty(); | ||
expect(entity.streamManagement.enabled).toBe(true); | ||
|
||
await entity.send(<message id="a" />); | ||
entity.streamManagement._teardown(); | ||
|
||
expect(entity.streamManagement.outbound).toBe(0); | ||
expect(entity.streamManagement.outbound_q).toHaveLength(1); | ||
|
||
let acks = 0; | ||
entity.streamManagement.on("ack", (stanza) => { | ||
expect(stanza.attrs.id).toBe("a"); | ||
acks++; | ||
}); | ||
|
||
entity.mockInput(<a xmlns="urn:xmpp:sm:3" h="1" />); | ||
await tick(); | ||
|
||
expect(acks).toBe(1); | ||
expect(entity.streamManagement.outbound).toBe(1); | ||
expect(entity.streamManagement.outbound_q).toHaveLength(0); | ||
}); | ||
|
||
test("resume - resumed", async () => { | ||
const { entity } = mockClient(); | ||
|
||
|
@@ -138,6 +189,7 @@ test("resume - resumed", async () => { | |
); | ||
|
||
entity.streamManagement.outbound = 45; | ||
entity.streamManagement.outbound_q = [<message id="a" />, <message id="b" />]; | ||
|
||
expect(await entity.catchOutgoing()).toEqual( | ||
<resume xmlns="urn:xmpp:sm:3" previd="bar" h="0" />, | ||
|
@@ -147,11 +199,31 @@ test("resume - resumed", async () => { | |
|
||
expect(entity.status).toBe("offline"); | ||
|
||
entity.mockInput(<resumed xmlns="urn:xmpp:sm:3" />); | ||
entity.mockInput(<resumed xmlns="urn:xmpp:sm:3" h="46" />); | ||
|
||
await tick(); | ||
let acks = 0; | ||
entity.streamManagement.on("ack", (stanza) => { | ||
expect(stanza.attrs.id).toBe("a"); | ||
acks++; | ||
}); | ||
|
||
expect(await entity.catchOutgoing()).toEqual(<message id="b" />); | ||
|
||
expect(entity.streamManagement.outbound).toBe(45); | ||
let resumed = false; | ||
entity.streamManagement.on("resumed", () => { | ||
resumed = true; | ||
}); | ||
|
||
await tick(); | ||
entity.streamManagement._teardown(); | ||
|
||
expect(resumed).toBe(true); | ||
expect(acks).toBe(1); | ||
expect(entity.streamManagement.outbound).toBe(46); | ||
expect(entity.streamManagement.outbound_q).toHaveLength(1); | ||
expect( | ||
entity.streamManagement.outbound_q[0].getChild("delay", "urn:xmpp:delay"), | ||
).not.toBeUndefined(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please move this to a new test |
||
expect(entity.status).toBe("online"); | ||
}); | ||
|
||
|
@@ -162,6 +234,7 @@ test("resume - failed", async () => { | |
entity.streamManagement.id = "bar"; | ||
entity.streamManagement.enabled = true; | ||
entity.streamManagement.outbound = 45; | ||
entity.streamManagement.outbound_q = ["hai"]; | ||
|
||
entity.mockInput( | ||
<features xmlns="http://etherx.jabber.org/streams"> | ||
|
@@ -179,10 +252,18 @@ test("resume - failed", async () => { | |
</failed>, | ||
); | ||
|
||
let failures = 0; | ||
entity.streamManagement.on("fail", (failed) => { | ||
failures++; | ||
expect(failed).toBe("hai"); | ||
}); | ||
sonnyp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
await tick(); | ||
|
||
expect(failures).toBe(1); | ||
expect(entity.status).toBe("bar"); | ||
expect(entity.streamManagement.id).toBe(""); | ||
expect(entity.streamManagement.enabled).toBe(false); | ||
expect(entity.streamManagement.outbound).toBe(0); | ||
expect(entity.streamManagement.outbound_q).toBeEmpty(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if
entity.send
rejects ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then resumed will fail. What context causes send to reject?