git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit a728a4416ce9e9ab6ba38dc79dde2c7a294feb7e

wait only untill we have the number of ticks of the oldest message

wanderer committed on 5/22/2017, 11:31:06 PM
Parent: 5b9026817a9c02b306d206d7225548e34e50bb16

Files changed

exoInterface.jschanged
index.jschanged
port.jschanged
portManager.jschanged
tests/index.jschanged
exoInterface.jsView
@@ -103,10 +103,12 @@
103103 this.emit('result', result)
104104 return result
105105 }
106106
107- // returns a promise that resolves once the kernel hits the threshould tick
108- // count
107+ /**
108+ * returns a promise that resolves once the kernel hits the threshould tick count
109+ *
110+ */
109111 wait (threshold, fromPort) {
110112 if (threshold <= this.ticks) {
111113 return this.ticks
112114 } else if (this.containerState === 'idle') {
@@ -140,9 +142,9 @@
140142 // set the port that the message came from
141143 message._fromPort = this.entryPort
142144 message._fromPortTicks = this.ticks
143145
144- const container = await this.getContainer(portRef)
146+ const container = await this.getInstance(portRef)
145147 container.queue(message)
146148
147149 const waiter = this._waitingMap.get(portRef)
148150 if (waiter) {
@@ -150,8 +152,8 @@
150152 this._waitingMap.delete(portRef)
151153 }
152154 }
153155
154- getContainer (portRef) {
155- return this.hypervisor.getOrCreateInstance(portRef, this.entryPort)
156+ getInstance (portRef) {
157+ return this.hypervisor.getInstanceByPort(portRef, this.entryPort)
156158 }
157159 }
index.jsView
@@ -8,17 +8,22 @@
88 * @param {Graph} dag an instance of [ipfs.dag](https://github.com/ipfs/interface-ipfs-core/tree/master/API/dag#dag-api)
99 */
1010 constructor (dag) {
1111 this.graph = new Graph(dag)
12- this._runningContainers = new Map()
12+ this._containerInstances = new Map()
1313 this._containerTypes = {}
1414 }
1515
16- async getByPath (root, path) {
16+ /**
17+ * get a container by its path
18+ * @param {Object} root - the root container to start searching from
19+ * @param {String} path - the path to travers
20+ */
21+ async getInstanceByPath (root, path) {
1722 path = path.split('/')
1823 for (const name of path) {
1924 const portRef = root.ports.get(name)
20- root = await this.getOrCreateInstance(portRef, root.entryPort)
25+ root = await this.getInstanceByPort(portRef, root.entryPort)
2126 }
2227 return root
2328 }
2429
@@ -26,16 +31,16 @@
2631 * get a contrainer instance given its entry port and its mounting port
2732 * @param {Object} port the entry port for the container
2833 * @param {Object} parentPort the entry port of the parent container
2934 */
30- async getOrCreateInstance (port, parentPort) {
31- let instance = this._runningContainers.get(port)
35+ async getInstanceByPort (port, parentPort) {
36+ let instance = this._containerInstances.get(port)
3237 // if there is no container running crceate one
3338 if (!instance) {
3439 instance = await this.createInstance(port.type, port.link, port, parentPort)
3540 instance.on('idle', () => {
3641 // once the container is done shut it down
37- this._runningContainers.delete(port)
42+ this._containerInstances.delete(port)
3843 })
3944 }
4045 return instance
4146 }
@@ -48,9 +53,9 @@
4853 * @param {Object} fromPort the entryPort of the container requesting the
4954 * wait. Used internally so that waits don't become cyclic
5055 */
5156 async wait (port, threshold, fromPort) {
52- let instance = this._runningContainers.get(port)
57+ let instance = this._containerInstances.get(port)
5358 if (instance) {
5459 return instance.wait(threshold, fromPort)
5560 } else {
5661 return threshold
@@ -83,9 +88,9 @@
8388 Container: Container
8489 })
8590
8691 // save the newly created instance
87- this._runningContainers.set(entryPort, exoInterface)
92+ this._containerInstances.set(entryPort, exoInterface)
8893 await exoInterface.start()
8994 return exoInterface
9095 }
9196
port.jsView
@@ -16,5 +16,9 @@
1616
1717 shift () {
1818 return this._queue.shift()
1919 }
20+
21+ get size () {
22+ return this._queue.length
23+ }
2024 }
portManager.jsView
@@ -15,10 +15,10 @@
1515 return pairA
1616 }
1717
1818 // order by number of ticks if messages have different number of ticks
19- if (a._fromPortTicks !== b._fromPortTicks) {
20- return a._fromPortTicks < b._fromPortTicks ? pairA : pairB
19+ if (portA.ticks !== portB.ticks) {
20+ return portA.ticks < portB.ticks ? pairA : pairB
2121 } else if (a.priority !== b.priority) {
2222 // decide by priority
2323 return a.priority > b.priority ? pairA : pairB
2424 } else {
@@ -118,9 +118,16 @@
118118 }
119119
120120 async getNextMessage () {
121121 if (this._portMap.size) {
122- await this.wait(this.exoInterface.ticks)
122+ // find the oldest message
123+ const ticks = [...this._portMap].map(([name, port]) => {
124+ return port.size ? port.ticks : this.exoInterface.ticks
125+ }).reduce((ticksA, ticksB) => {
126+ return ticksA < ticksB ? ticksA : ticksB
127+ })
128+
129+ await this.wait(ticks)
123130 const portMap = [...this._portMap].reduce(messageArbiter)
124131 return portMap[1].shift()
125132 }
126133 }
tests/index.jsView
@@ -662,17 +662,17 @@
662662 const root = await hypervisor.createInstance('base')
663663 let port = root.ports.create('base')
664664 root.ports.bind(port, 'first')
665665
666- const first = await root.getContainer(port)
666+ const first = await root.getInstance(port)
667667 port = first.ports.create('base')
668668 first.ports.bind(port, 'second')
669669
670- const second = await first.getContainer(port)
670+ const second = await first.getInstance(port)
671671 port = second.ports.create('base')
672672 second.ports.bind(port, 'third')
673673
674- const third = await second.getContainer(port)
675- const foundThird = await hypervisor.getByPath(root, 'first/second/third')
674+ const third = await second.getInstance(port)
675+ const foundThird = await hypervisor.getInstanceByPath(root, 'first/second/third')
676676 t.equals(third, foundThird, 'should find by path')
677677 })
678678 })

Built with git-ssb-web