@stalkchain/grpc-pool
Version:
High-availability gRPC connection pooling module with active-active configuration, deduplication, and stale connection detection
85 lines • 2.77 kB
TypeScript
/**
* lib/deduplication.ts - Transaction signature deduplication service
*
* Implements LRU cache with time-based expiration for detecting duplicate
* transactions across multiple gRPC endpoints. Uses signature buffers as
* unique identifiers with automatic cleanup. Uses binary encoding for
* optimal performance and memory usage.
*
* @module lib/deduplication
* @author StalkChain Team
* @version 1.1.0
*/
/**
* Transaction deduplication service using LRU cache with TTL
*
* Efficiently tracks seen transaction signatures to prevent duplicate
* emissions when same transaction comes from multiple endpoints.
* Uses binary encoding for optimal performance and memory efficiency.
*/
export declare class DeduplicationService {
private cache;
private cleanupInterval;
private config;
constructor(options?: {
deduplicationTtlMs?: number;
maxCacheSize?: number;
});
/**
* Check if signature has been seen before (is duplicate)
*
* Uses Buffer.toString('binary') for optimal performance and memory usage.
*
* ⚠️ IMPORTANT: toString('binary') produces strings with characters that have
* code points 0-255, which can include non-printable characters and high
* Unicode code points. This would be problematic for:
* - JSON serialization/deserialization
* - Network transmission
* - Database storage
* - Logging/debugging (hard to read)
* - Any external system interaction
*
* ✅ SAFE FOR OUR USE CASE because:
* - Only used internally as Map keys
* - Never serialized to JSON
* - Never sent over network
* - Never stored in database
* - Never logged (we use base58 for logging)
* - 34% faster than base64 (18.6M vs 13.9M ops/sec)
* - 27% less memory usage (64 vs 88 characters)
* - Perfect 1:1 mapping, no data loss
*
* @param signatureBuffer - Transaction signature as Buffer
* @returns true if duplicate, false if new/unique
*/
isDuplicate(signatureBuffer: Buffer): boolean;
/**
* Add signature to cache with current timestamp
*/
private addSignature;
/**
* Remove oldest entries from cache (LRU behavior)
*/
private removeOldestEntries;
/**
* Start automatic cleanup interval to remove expired entries
*/
private startCleanupInterval;
/**
* Remove all expired entries from cache
*/
private cleanupExpiredEntries;
/**
* Get current cache statistics for monitoring
*/
getStats(): {
size: number;
maxSize: number;
ttlMs: number;
};
/**
* Clear all entries and stop cleanup interval
*/
destroy(): void;
}
//# sourceMappingURL=deduplication.d.ts.map