Skip to content

Instantly share code, notes, and snippets.

View nikostoulas's full-sized avatar

Nikos Kostoulas nikostoulas

View GitHub Profile
@nikostoulas
nikostoulas / streaming-rpc-example.js
Created September 9, 2021 08:05
RPC example with backpressure, timeout and stop
const file = await rabbit.getReply(‘readFile’, {
correlationId,
headers: { backpressure: true, timeout: 1000 },
});
for await (const line of file) {
console.log(line);
process(line);
if(processingHasFinished()) {
// send stop signal to producer of the stream which is a different microservice
@nikostoulas
nikostoulas / a.get-local-file.js
Last active September 10, 2021 15:43
RPC with streams example
// Reading a local file into a stream
const readStream = fs.createReadStream('file.txt');
@nikostoulas
nikostoulas / a.calculator-service.js
Last active September 10, 2021 15:43
Calculator - Billing service RPC examples
// boilerplate to connect with RabbitMQ
const rabbit = require(‘./connect’);
rabbit.createQueue(‘add’, { durable: false }, (msg, ack) => {
const numbers = JSON.parse(msg.content.toString());
ack(null, numbers[0] + numbers[1]);
});
const http = require(‘http’);
const server = http.createServer((req, res) => {
// `req` is an http.IncomingMessage, which is a Readable Stream.
// `res` is an http.ServerResponse, which is a Writable Stream.
let body = ‘’;
// Get the data as utf 8 strings.
// If an encoding is not set, Buffer objects will be received.
req.setEncoding(‘utf8’);
// Readable streams emit ‘data’ events once a listener is added.
req.on(‘data’, chunk => {
const https = require('https');
https
.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY', resp => {
let data = '';
// A chunk of data has been received.
resp.on('data', chunk => {
data += chunk;
});
@nikostoulas
nikostoulas / prefetch.js
Created March 19, 2020 14:04
Synchronised block of code in node.js - Example inspired by rabbit-queue implementation of prefetch
let globalPrefetch = 10;
async function changePrefetch(prefetchNum) {
await new Promise(r => setTimeout(r, 10));
globalPrefetch = prefetchNum;
}
async function subscribeToQueue(name, prefetch) {
await changePrefetch(prefetch);
await new Promise(r => setTimeout(r, 20));
console.log(`subscribing to queue ${name} with prefetch ${globalPrefetch}`);
@nikostoulas
nikostoulas / .gitconfig
Last active January 23, 2023 08:57
Github aliases
# To make it easier to work on a anti git flow branching model described here: http://endoflineblog.com/gitflow-considered-harmful
# the following aliases have been created
[alias]
lg = log --graph --pretty=format:'%Cred%h%Creset [%C(dim cyan)%ad%Creset] %C(dim green)%an%Creset: %C(bold dim white)%s%Creset %C(auto)%d%Creset' --date=short
make-hotfix = !git stash && git hotfix-create $1 && git stash pop && :
graph = log --graph --color --pretty=format:'%Cred%h%Creset [%C(dim cyan)%cr%Creset] %C(dim green)%an%Creset: %C(bold dim white)%s%Creset %C(auto)%d%Creset'
tree = log --all --graph --decorate=short --color --format=format:'%C(bold blue)%h%C(reset) %C(auto)%d%C(reset)\n %C(dim cyan)[%cr]%C(reset) %x09%C(dim cyan)%an:%C(reset) %C(bold cyan)%s %C(reset)'
hotfix-create = "!f() {\
red=`tput setaf 1`;\
yellow=`tput setaf 3`;\