Commit 71fa8ce31e076f693c68131f4ea2ee4b49379c07
fix waiting bug
wanderer committed on 1/7/2018, 10:21:28 PMParent: c77c7618fb3abd31a3d1b797af55b9f21a318173
Files changed
inbox.js | changed |
scheduler.js | changed |
inbox.js | ||
---|---|---|
@@ -81,21 +81,18 @@ | ||
81 | 81 | throw new Error('already waiting for next message') |
82 | 82 | } |
83 | 83 | |
84 | 84 | let message = this._getOldestMessage() |
85 | + if (message) { | |
86 | + timeout = message._fromTicks | |
87 | + } else { | |
88 | + timeout += this.actor.ticks | |
89 | + } | |
85 | 90 | |
86 | - timeout += this.actor.ticks | |
87 | 91 | let oldestTime = this.hypervisor.scheduler.leastNumberOfTicks(this.actor.id) |
88 | 92 | |
89 | 93 | while (true) { |
90 | 94 | if (message) { |
91 | - // if the message we recived had more ticks then we currently have then | |
92 | - // update our ticks to it, since we jumped forward in time | |
93 | - if (message._fromTicks > this.actor.ticks) { | |
94 | - this.actor.ticks = message._fromTicks | |
95 | - this.hypervisor.scheduler.update(this.actor) | |
96 | - } | |
97 | - | |
98 | 95 | // if there is a message that is "older" then the timeout, the lower |
99 | 96 | // the timeout to the oldest message |
100 | 97 | if (message._fromTicks < timeout) { |
101 | 98 | timeout = message._fromTicks |
@@ -118,8 +115,14 @@ | ||
118 | 115 | ]) |
119 | 116 | oldestTime = this.hypervisor.scheduler.leastNumberOfTicks(this.actor.id) |
120 | 117 | } |
121 | 118 | this._gettingNextMessage = false |
119 | + // if the message we recived had more ticks then we currently have then | |
120 | + // update our ticks to it, since we jumped forward in time | |
121 | + if (message && message._fromTicks > this.actor.ticks) { | |
122 | + this.actor.ticks = message._fromTicks | |
123 | + this.hypervisor.scheduler.update(this.actor) | |
124 | + } | |
122 | 125 | return this._deQueueMessage() |
123 | 126 | } |
124 | 127 | |
125 | 128 | // returns a promise that resolve when a message older then the given message |
scheduler.js | ||
---|---|---|
@@ -21,8 +21,9 @@ | ||
21 | 21 | * @param {string} id |
22 | 22 | * @return {function} the resolve function to call once it to unlock |
23 | 23 | */ |
24 | 24 | lock (id) { |
25 | + id = id.toString('hex') | |
25 | 26 | let r |
26 | 27 | const p = new Promise((resolve, reject) => { |
27 | 28 | r = resolve |
28 | 29 | }) |
@@ -36,33 +37,34 @@ | ||
36 | 37 | * updates an instance with a new tick count |
37 | 38 | * @param {Object} instance - an actor instance |
38 | 39 | */ |
39 | 40 | update (instance) { |
40 | - this._waits = this._waits.filter(wait => wait.id !== instance.id) | |
41 | 41 | this._update(instance) |
42 | - this._running.add(instance.id) | |
42 | + this._running.add(instance.id.toString('hex')) | |
43 | 43 | this._checkWaits() |
44 | 44 | } |
45 | 45 | |
46 | 46 | _update (instance) { |
47 | - this.instances.delete(instance.id) | |
48 | - this.instances.set(instance.id, instance) | |
47 | + this.instances.delete(instance.id.toString('hex')) | |
48 | + this.instances.set(instance.id.toString('hex'), instance) | |
49 | 49 | } |
50 | 50 | |
51 | 51 | /** |
52 | 52 | * returns an Actor instance |
53 | 53 | * @param {String} id |
54 | 54 | * @return {Object} |
55 | 55 | */ |
56 | 56 | getInstance (id) { |
57 | + id = id.toString('hex') | |
57 | 58 | return this.instances.get(id) |
58 | 59 | } |
59 | 60 | |
60 | 61 | /** |
61 | 62 | * deletes an instance from the scheduler |
62 | 63 | * @param {String} id - the containers id |
63 | 64 | */ |
64 | 65 | done (id) { |
66 | + id = id.toString('hex') | |
65 | 67 | this._running.delete(id) |
66 | 68 | this.instances.delete(id) |
67 | 69 | this._checkWaits() |
68 | 70 | } |
@@ -74,9 +76,12 @@ | ||
74 | 76 | * @param {string} id - optional id of the container that is waiting |
75 | 77 | * @return {Promise} |
76 | 78 | */ |
77 | 79 | wait (ticks, id) { |
78 | - this._running.delete(id) | |
80 | + if (id) { | |
81 | + id = id.toString('hex') | |
82 | + this._running.delete(id) | |
83 | + } | |
79 | 84 | |
80 | 85 | return new Promise((resolve, reject) => { |
81 | 86 | binarySearchInsert(this._waits, comparator, { |
82 | 87 | ticks: ticks, |
Built with git-ssb-web