Files: 8219bd66a439d14f4728e4a0195f4c017c42ab90 / inbox.js
3995 bytesRaw
1 | const binarySearchInsert = require('binary-search-insert') |
2 | const Buffer = require('safe-buffer').Buffer |
3 | |
4 | // decides which message to go first |
5 | function messageArbiter (messageA, messageB) { |
6 | // order by number of ticks if messages have different number of ticks |
7 | if (messageA._fromTicks !== messageB._fromTicks) { |
8 | return messageA._fromTicks > messageB._fromTicks |
9 | } else { |
10 | // sender id |
11 | return Buffer.compare(messageA._fromId, messageB._fromId) |
12 | } |
13 | } |
14 | |
15 | module.exports = class Inbox { |
16 | /** |
17 | * The inbox manages and sorts incoming messages and provides functions |
18 | * to wait on messages |
19 | * @param {Object} opts |
20 | * @param {Object} opts.state |
21 | * @param {Object} opts.hypervisor |
22 | */ |
23 | constructor (opts) { |
24 | this.actor = opts.actor |
25 | this.hypervisor = opts.hypervisor |
26 | this._queue = [] |
27 | this._waitingTagsQueue = [] |
28 | this._oldestMessagePromise = new Promise((resolve, reject) => { |
29 | this._oldestMessageResolve = resolve |
30 | }) |
31 | } |
32 | |
33 | /** |
34 | * queues a message |
35 | * @param {Message} message |
36 | */ |
37 | queue (message) { |
38 | this._queueMessage(message) |
39 | |
40 | const oldestMessage = this._getOldestMessage() |
41 | if (oldestMessage === message) { |
42 | this._oldestMessageResolve(message) |
43 | this._oldestMessagePromise = new Promise((resolve, reject) => { |
44 | this._oldestMessageResolve = resolve |
45 | }) |
46 | } |
47 | } |
48 | |
49 | /** |
50 | * Waits for a message sent with a capablitly that has one of the given tags |
51 | * @param {Array<*>} tags |
52 | * @param {Integer} timeout |
53 | * @returns {Promise} |
54 | */ |
55 | async nextTaggedMessage (tags, timeout) { |
56 | this._waitingTags = new Set(tags) |
57 | this._queue = this._queue.filter(message => !this._queueTaggedMessage(message)) |
58 | |
59 | // todo: add saturation test |
60 | const message = await this.nextMessage(timeout) |
61 | delete this._waitingTags |
62 | this._waitingTagsQueue.forEach(message => this._queueMessage(message)) |
63 | this._waitingTagsQueue = [] |
64 | |
65 | return message |
66 | } |
67 | |
68 | /** |
69 | * Waits for the the next message if any |
70 | * @param {Integer} timeout |
71 | * @returns {Promise} |
72 | */ |
73 | async nextMessage (timeout = 0) { |
74 | if (this._gettingNextMessage) { |
75 | throw new Error('already getting next message') |
76 | } else { |
77 | this._gettingNextMessage = true |
78 | } |
79 | |
80 | await Promise.all([...this.actor._sending.values()]) |
81 | let message = this._getOldestMessage() |
82 | if (message === undefined && timeout === 0) { |
83 | return |
84 | } |
85 | |
86 | timeout += this.actor.ticks |
87 | let oldestTime = this.hypervisor.scheduler.leastNumberOfTicks(this.actor.id) |
88 | |
89 | while (true) { |
90 | if (message && message._fromTicks < timeout) { |
91 | timeout = message._fromTicks |
92 | } |
93 | |
94 | if (oldestTime >= timeout) { |
95 | break |
96 | } |
97 | |
98 | await Promise.race([ |
99 | this.hypervisor.scheduler.wait(timeout, this.actor.id).then(() => { |
100 | message = this._getOldestMessage() |
101 | }), |
102 | this._olderMessage(message).then(m => { |
103 | message = m |
104 | }) |
105 | ]) |
106 | oldestTime = this.hypervisor.scheduler.leastNumberOfTicks(this.actor.id) |
107 | } |
108 | this._gettingNextMessage = false |
109 | return this._deQueueMessage() |
110 | } |
111 | |
112 | // returns a promise that resolve when a message older then the given message |
113 | // is recived |
114 | _olderMessage (message) { |
115 | return this._oldestMessagePromise |
116 | } |
117 | |
118 | _getOldestMessage () { |
119 | if (this._waitingTags) { |
120 | return this._waitingTagsQueue[0] |
121 | } else { |
122 | return this._queue[0] |
123 | } |
124 | } |
125 | |
126 | _deQueueMessage () { |
127 | if (this._waitingTags) { |
128 | return this._waitingTagsQueue.shift() |
129 | } else { |
130 | return this._queue.shift() |
131 | } |
132 | } |
133 | |
134 | _queueMessage (message) { |
135 | if (!(this._waitingTags && this._queueTaggedMessage(message))) { |
136 | binarySearchInsert(this._queue, messageArbiter, message) |
137 | } |
138 | } |
139 | |
140 | _queueTaggedMessage (message) { |
141 | if (this._waitingTags.has(message.tag)) { |
142 | this._waitingTags.delete(message.tag) |
143 | binarySearchInsert(this._waitingTagsQueue, messageArbiter, message) |
144 | return true |
145 | } else { |
146 | return false |
147 | } |
148 | } |
149 | } |
150 |
Built with git-ssb-web