Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x | import path from 'path';
import { EventEmitter } from 'events';
import grpc, { ServiceError, status as ServiceErrorStatus } from 'grpc';
import { ApplicationConfiguration, getServiceConfiguration } from './configuration';
import { Transaction } from './transaction';
import { QUEUE_ENTITY_EVENTS } from './queue-entity';
import { loadSync as loadProtoSync } from '@grpc/proto-loader';
import {
CreateTransactionPayload,
CreateTransactionResponse
} from './protos/types/transaction';
const EVENTS = {
TRANSACTION_PUBLISH_SUCCESS: Symbol(),
TRANSACTION_PUBLISH_ERROR: Symbol(),
TRANSACTION_CANCELLED: Symbol(),
CONNECTION_CREATED: Symbol(),
CONNECTION_CLOSED: Symbol(),
};
export const TRANSPORTER_EVENTS = EVENTS;
export interface TransactionPublishedPayload {
callId: string;
transaction: Transaction;
}
export interface TransactionErrorPayload {
callId?: string;
reason: string;
transaction?: Transaction;
}
export interface TransactionCancelledPayload {
transaction: Transaction;
}
export interface TransactionErrorPayload {
callId?: string;
reason: string;
metadata?: any;
details?: any;
transaction?: Transaction;
}
export interface TransactionTransporter extends EventEmitter {
start(): void;
stop(): void;
push(transaction: Transaction): void;
}
export class DefaultTransactionTransporter extends EventEmitter implements TransactionTransporter {
// Defaults for processing
static readonly PROCESS_INTERVAL = 1000;
static readonly PROCESS_COUNT = 50;
private config: ApplicationConfiguration;
private pendingTransactions: Map<string, Transaction> = new Map();
private processableTransactions: Map<string, Transaction> = new Map();
private processingTransactions: Map<string, Transaction> = new Map();
private interval: NodeJS.Timeout | null;
private call: grpc.ServerDuplexStream<CreateTransactionPayload, any>;
private client: grpc.Client;
private checkCount = 0;
get isRunning(): boolean {
return !!this.client;
}
constructor(config: ApplicationConfiguration) {
super();
this.config = config;
this.interval = null;
}
public start(): void {
if (this.isRunning) {
// Dont create a duplicate client if one exists
return;
}
const definition = grpc.loadPackageDefinition(
loadProtoSync(
`${path.join(__dirname, 'protos')}/transaction.proto`,
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
}
)
);
const { address, credentials } = getServiceConfiguration();
this.client = new (definition.transaction as any).Transaction(address, credentials);
this.createCall();
this.interval = setInterval(
this.processList.bind(this),
DefaultTransactionTransporter.PROCESS_INTERVAL
);
}
public stop(): void {
if (this.isRunning) {
this.client.close();
}
clearTimeout(this.interval);
}
public push(transaction: Transaction): void {
this.pendingTransactions.set(transaction.trackingKey, transaction);
// Setup transaction movements between queues
transaction.once(QUEUE_ENTITY_EVENTS.ENQUEUE, () => {
this.removeTransaction(transaction);
this.processableTransactions.set(transaction.trackingKey, transaction);
});
transaction.once(QUEUE_ENTITY_EVENTS.CANCEL, () => {
this.removeTransaction(transaction);
this.emit(EVENTS.TRANSACTION_CANCELLED, {
transaction,
} as TransactionCancelledPayload);
});
transaction.on(QUEUE_ENTITY_EVENTS.PROCESS, () => {
this.removeTransaction(transaction);
this.processTransaction(transaction);
});
transaction.on(QUEUE_ENTITY_EVENTS.RETRY, () => {
this.removeTransaction(transaction);
this.processableTransactions.set(transaction.trackingKey, transaction);
});
transaction.once(QUEUE_ENTITY_EVENTS.FINISHED, metadata => {
this.removeTransaction(transaction);
this.emit(EVENTS.TRANSACTION_PUBLISH_SUCCESS, {
...metadata,
transaction
} as TransactionPublishedPayload);
});
transaction.once(QUEUE_ENTITY_EVENTS.FAILED, metadata => {
this.removeTransaction(transaction);
this.emit(EVENTS.TRANSACTION_PUBLISH_ERROR, {
...metadata,
transaction,
} as TransactionErrorPayload);
});
}
public removeTransaction(transaction: Transaction | CreateTransactionResponse): void {
this.pendingTransactions.delete(transaction.trackingKey);
this.processableTransactions.delete(transaction.trackingKey);
this.processingTransactions.delete(transaction.trackingKey);
}
private createCall(): void {
this.call = (this.client as any).createTransaction();
this.call
.on('data', (response: CreateTransactionResponse) => {
const transaction = this.processingTransactions.get(response.trackingKey);
if (!transaction) {
// TODO: Handle case when there is no queued item to find
return;
}
if (response.state.accepted) {
// Message was accepted
return transaction.published({
callId: response.callId,
});
}
// If message was not accepted then retry
transaction.retry({
callId: response.callId,
reason: 'Error occurred during publishing the transaction' // TODO: Read error from the response
});
})
.on('error', (err: ServiceError) => {
this.emit(EVENTS.TRANSACTION_PUBLISH_ERROR, {
reason: err.message,
metadata: err.metadata,
details: err.details,
} as TransactionErrorPayload);
// Connection was interrupted, recreate
const retriableErrorStatuses = [
ServiceErrorStatus.INTERNAL,
ServiceErrorStatus.UNKNOWN,
ServiceErrorStatus.CANCELLED,
ServiceErrorStatus.DEADLINE_EXCEEDED,
ServiceErrorStatus.ABORTED,
ServiceErrorStatus.UNAVAILABLE
];
if (retriableErrorStatuses.includes(err.code)) {
this.retryAll();
}
})
.on('end', () => {
// stream closed
this.emit(EVENTS.CONNECTION_CLOSED);
this.retryAll();
});
}
private processTransaction(transaction: Transaction): void {
this.processingTransactions.set(transaction.trackingKey, transaction);
this.call.write(transaction.toMessage());
}
private retryAll(): void {
this.createCall();
this.processingTransactions.forEach(transaction => {
this.call.write(transaction.toMessage());
});
}
private processList(): void {
if (process.env.DEBUG_INSTRUMENTATION && (++this.checkCount % 180 === 0)) {
console.log(`processing list, processing: ${this.processingTransactions.size}, processable: ${this.processableTransactions.size}, pending: ${this.pendingTransactions.size}`);
}
let addToProcessingCount = DefaultTransactionTransporter.PROCESS_COUNT - this.processingTransactions.size;
for (const transaction of this.processableTransactions.values()) {
if (--addToProcessingCount < 1) {
break;
}
transaction.process();
}
}
}
|