presidium-db-client-server 0.7.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DatabaseLoadBalancer.js +4 -4
- package/README.md +21 -0
- package/package.json +1 -1
package/DatabaseLoadBalancer.js
CHANGED
|
@@ -10,7 +10,7 @@ const notFoundHandler = require("./notFoundHandler")
|
|
|
10
10
|
const errorHandler = require("./errorHandler")
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* @name
|
|
13
|
+
* @name DatabaseLoadBalancer
|
|
14
14
|
*
|
|
15
15
|
* @docs
|
|
16
16
|
* ```specscript
|
|
@@ -18,7 +18,7 @@ const errorHandler = require("./errorHandler")
|
|
|
18
18
|
*
|
|
19
19
|
* HTTP.Handler(HTTP.Request, HTTP.Response) -> ()
|
|
20
20
|
*
|
|
21
|
-
* new
|
|
21
|
+
* new DatabaseLoadBalancer(
|
|
22
22
|
* hash(tableName string, key string, length number) -> Promise|number,
|
|
23
23
|
* options {
|
|
24
24
|
* serverIps: Array<string>|Array<Array<string>>,
|
|
@@ -27,7 +27,7 @@ const errorHandler = require("./errorHandler")
|
|
|
27
27
|
* )
|
|
28
28
|
* ```
|
|
29
29
|
*/
|
|
30
|
-
class
|
|
30
|
+
class DatabaseLoadBalancer {
|
|
31
31
|
constructor(hash, options, ...handlers) {
|
|
32
32
|
this.hash = hash
|
|
33
33
|
this.serverIps = options.serverIps
|
|
@@ -94,4 +94,4 @@ class DatabaseLoadbalancer {
|
|
|
94
94
|
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
module.exports =
|
|
97
|
+
module.exports = DatabaseLoadBalancer
|
package/README.md
CHANGED
|
@@ -100,6 +100,27 @@ for await (const postJson of postHt.reverseIterator()) {
|
|
|
100
100
|
}
|
|
101
101
|
```
|
|
102
102
|
|
|
103
|
+
## Load balance your distributed database
|
|
104
|
+
```javascript
|
|
105
|
+
const DatabaseLoadBalancer = require("presidium/DatabaseLoadBalancer")
|
|
106
|
+
|
|
107
|
+
function hash(tableName, key, length) {
|
|
108
|
+
const value = `${tableName}${key}`
|
|
109
|
+
const digest = crypto.createHash("md5").update(value).digest()
|
|
110
|
+
const integerHash = digest.readUInt16BE(0)
|
|
111
|
+
const index = Math.floor(integerHash / 65535 * length)
|
|
112
|
+
return index
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const loadBalancer = new DatabaseLoadBalancer(hash, {
|
|
116
|
+
serverIps: ["10.0.0.1:8080", "10.0.0.2:8080"],
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
loadBalancer.listen(6333, () => {
|
|
120
|
+
console.log("Database load balancer listening on port 6333")
|
|
121
|
+
})
|
|
122
|
+
```
|
|
123
|
+
|
|
103
124
|
## Docs
|
|
104
125
|
Coming soon.
|
|
105
126
|
|