Skip to content

Instantly share code, notes, and snippets.

View jkyberneees's full-sized avatar
😎
up and running

Rolando Santamaria Maso jkyberneees

😎
up and running
View GitHub Profile
const gateway = require('fastify')({})
gateway.register(require('fastify-reply-from'))
gateway.register(require('k-fastify-gateway'), {
middlewares: [
require('cors')(), // https://www.npmjs.com/package/cors
require('helmet')() // https://helmetjs.github.io/
],
const { ServiceBroker } = require("./../index");
let broker = new ServiceBroker({
logger: true,
transporter: {
type: "hazelcast",
options: {
init(cfg) {
cfg.properties["hazelcast.logging"] = "off";
}
// Get a distributed lock called "my-distributed-lock"
ILock lock = hz.getILock("my-distributed-lock");
// Now create a lock and execute some guarded code.
lock.lock();
try {
//do something here
} catch (...) {
lock.unlock();
}
// on application instance start, init near cache Map
const {Client, Config} = require('hazelcast-client')
const nearCachedMapName = 'my-holy-fast-map'
const cfg = new Config.NearCacheConfig()
cfg.name = nearCachedMapName
cfg.invalidateOnChange = true
cfg.nearCacheConfigs[nearCachedMapName] = cfg
const hazelcast = await Client.newHazelcastClient(cfg)
const lock = hazelcast.getLock('my-distributed-lock')
lock.lock().then(() => {
// the execution of this function will happen once
// at a time across all distributed instances
// ...
console.log('Happy Locking!')
}).finally(() => lock.unlock())
// on application instance start...
const counter = hazelcast.getAtomicLong('distributed-pushes-counter')
await counter.compareAndSet(0, await PushLogs.count({}))
// on push...
await counter.incrementAndGet()
// on request
await counter.get()
(async () => {
const Client = require('hazelcast-client').Client
const hz = await Client.newHazelcastClient()
const list = hz.getList('my-distributed-list')
await list.add(process.pid)
console.log(await list.toArray())
await hz.shutdown()
})()
const server = require('express')({})
server.get('/', (req, res) => {
res.send('Hello World!')
})
server.listen(3000)
const express = require('express')
const bodyParser = require('body-parser')
const axios = require('axios')
const httpc = axios.create({
baseURL: 'http://localhost:3000',
timeout: 30000
})
const app = express()
const N = 2000000
const items = []
for (var i = 0; i < N; i++) {
items.push({ index: i })
}
console.log(items.map(e => e.index).filter(v => v % 2 === 0).reduce((sum, v) => sum += v))