Skip to content

Instantly share code, notes, and snippets.

package device.packet;
import device.packet.decoder.PacketDataDecoder;
import static device.packet.response.DeviceResponse.success;
public abstract class AbstractPacket implements Packet {
public static final int SERIAL_NUMBER_INDEX = 5;
@Sandeeb
Sandeeb / CircuitBreaker.spec.js
Created March 14, 2018 06:11
Test for custom circuit breaker
import sinon from 'sinon';
import {expect} from 'chai';
import CircuitBreaker from '../../lib/support/CircuitBreaker';
describe('CircuitBreaker', () => {
const sandbox = sinon.sandbox.create();
afterEach(() => {
sandbox.restore();
});
@Sandeeb
Sandeeb / CircuitBreaker.js
Created March 14, 2018 06:10
Custom circuit breaker implementaion wrapping callback into a promise. It accepts a http command which return a promise. It considers 4xx as success.
import CircuitBreaker from 'circuit-breaker-js';
import logger from './logger';
function is5xxError(statusCode) {
return statusCode >= 500 && statusCode < 600;
}
const defaultConfig = {
volumeThreshold: 5, // error count
errorThreshold: 50, // error percentage
windowDuration: 10000, // milliseconds
@Sandeeb
Sandeeb / retry.js
Created March 14, 2018 05:43
Sample retry pattern with ES6 promises
export function retry (func, recoverFunc) {
return func()
.catch( err => {
logger.info(`Calling function failed with error, retrying once after recovery`, err);
return recoverFunc(err).then(() => func());
});
}
const executeRequest = () => {
};
@Sandeeb
Sandeeb / MockExternalSoapService.java
Created March 14, 2018 05:34
Example- Mock soap APIs both OK and Faults using mock-server
package com.qantas.stme.tests.mock.ifly;
import com.qantas.stme.tests.mock.MockService;
import org.apache.commons.io.IOUtils;
import org.cryptacular.io.ClassPathResource;
import org.mockserver.client.server.MockServerClient;
import org.mockserver.model.Header;
import org.mockserver.model.HttpResponse;
import java.io.IOException;
import { createMessageProcessor } from './sqsMessageProcessor';
import logger from '../../logger';
import config from '../../config';
import clsContext from '../../util/cls';
export function createHandler() {
return (messageBody, done) => {
logger.info('message received', messageBody);
return doSomethingWithMessage(messageBody.data)
.then(() => done())
.catch((error) => {
@Sandeeb
Sandeeb / sqsMessageProcessor.js
Last active March 14, 2018 05:19
SQS Consumer with exponential back off strategy
import AWS from 'aws-sdk';
import SqsConsumer from 'sqs-consumer';
import uuid from 'uuid';
const MAX_VISIBILITY_TIMEOUT = 60 * 60 * 12 - 1;
export const calculateNewTimeout = ({ visibilityTimeout, visibilityQuotient }, approximateReceivedCount) => {
const newVisibilityTimeout = visibilityTimeout * (Math.pow(visibilityQuotient, approximateReceivedCount - 1) || 1);
return Math.min(newVisibilityTimeout, MAX_VISIBILITY_TIMEOUT);
};
const createProcessingErrorHandler = ({ sqs, sqsUrl, config, logger }) => (error, message) => {
const visibilityParams = {