Advanced thread pool system for sharing worker processes across multiple HappyPack instances, enabling optimal resource utilization and minimizing thread idle time.
Creates a shared thread pool that can be used by multiple HappyPack plugin instances.
/**
* Creates a shared thread pool for multiple HappyPack instances
* @param config - Thread pool configuration object
* @param config.size - Number of worker threads to create (required)
* @param config.id - Optional identifier for logging purposes
* @param config.verbose - Enable verbose logging output
* @param config.debug - Enable debug logging output
* @param config.bufferedMessaging - Enable Windows compatibility mode
* @returns Thread pool instance
*/
HappyPack.ThreadPool(config);
interface ThreadPoolConfig {
/** Number of worker threads to create (required) */
size: number;
/** Optional identifier for logging purposes */
id?: string;
/** Enable verbose logging output */
verbose?: boolean;
/** Enable debug logging output */
debug?: boolean;
/** Enable Windows compatibility mode for message passing */
bufferedMessaging?: boolean;
}Usage Examples:
const HappyPack = require('happypack');
// Basic shared pool
const sharedPool = HappyPack.ThreadPool({ size: 5 });
// Pool with logging
const debugPool = HappyPack.ThreadPool({
size: 4,
id: 'main-pool',
verbose: true,
debug: true
});
// Windows-compatible pool
const winPool = HappyPack.ThreadPool({
size: 3,
bufferedMessaging: true
});Methods and properties available on thread pool instances.
interface ThreadPool {
/** Number of threads in this pool */
size: number;
/**
* Initialize the thread pool and configure workers
* @param compilerId - Unique compiler identifier
* @param compiler - Webpack compiler instance
* @param compilerOptions - Serialized webpack options
* @param done - Completion callback
*/
start(compilerId: string, compiler: object, compilerOptions: string, done: function): void;
/**
* Check if the thread pool is currently running
* @returns True if all threads are active
*/
isRunning(): boolean;
/**
* Compile files using available threads in the pool
* @param loaderId - Unique loader identifier
* @param loader - Loader context object
* @param params - Compilation parameters
* @param done - Completion callback with results
*/
compile(loaderId: string, loader: object, params: object, done: function): void;
/**
* Shutdown threads associated with a specific compiler
* @param compilerId - Compiler identifier to stop
*/
stop(compilerId: string): void;
}Usage Examples:
const pool = HappyPack.ThreadPool({ size: 4 });
// Start pool for a compiler
pool.start('main-compiler', compiler, serializedOptions, (err) => {
if (err) {
console.error('Pool startup failed:', err);
return;
}
console.log('Pool ready');
});
// Check if pool is running
if (pool.isRunning()) {
console.log('Pool has active threads');
}
// Compile using the pool
pool.compile('loader-1', loaderContext, {
loaders: resolvedLoaders,
loaderContext: context
}, (err, result) => {
if (err) {
console.error('Compilation failed:', err);
} else {
console.log('Compilation successful:', result);
}
});
// Shutdown pool
pool.stop('main-compiler');Using shared thread pools across multiple HappyPack instances for optimal resource management.
/**
* Shared pool configuration pattern for multiple HappyPack instances
*/
interface SharedPoolSetup {
/** Create single shared pool */
pool: ThreadPool;
/** Configure multiple plugins to use the same pool */
plugins: HappyPlugin[];
}Usage Examples:
const HappyPack = require('happypack');
// Create shared pool
const sharedThreadPool = HappyPack.ThreadPool({ size: 5 });
// Configure multiple plugins to use shared pool
module.exports = {
plugins: [
// JavaScript processing
new HappyPack({
id: 'js',
threadPool: sharedThreadPool,
loaders: ['babel-loader']
}),
// TypeScript processing
new HappyPack({
id: 'ts',
threadPool: sharedThreadPool,
loaders: ['ts-loader']
}),
// Style processing
new HappyPack({
id: 'styles',
threadPool: sharedThreadPool,
loaders: ['style-loader', 'css-loader', 'less-loader']
})
]
};Understanding thread pool lifecycle and resource management.
/**
* Thread pool lifecycle management
*/
interface ThreadPoolLifecycle {
/** Pool creation and configuration */
creation: 'HappyPack.ThreadPool()';
/** Worker thread initialization */
initialization: 'pool.start()';
/** Active compilation phase */
compilation: 'pool.compile()';
/** Resource cleanup */
shutdown: 'pool.stop()';
}Lifecycle Example:
const HappyPack = require('happypack');
// 1. Creation
const pool = HappyPack.ThreadPool({
size: 4,
verbose: true
});
// 2. Initialization (happens automatically when first plugin starts)
// pool.start() is called internally by HappyPack plugins
// 3. Compilation (happens during webpack build)
// pool.compile() is called internally for each file
// 4. Shutdown (happens automatically when webpack finishes)
// pool.stop() is called internally when compilation endsUnderstanding how threads are managed internally within the pool.
/**
* Internal thread management details
*/
interface InternalThreadManagement {
/** Round-robin thread selection for load balancing */
scheduling: 'round-robin';
/** Thread reuse across compilation tasks */
reuse: boolean;
/** Automatic cleanup of idle threads */
cleanup: 'automatic';
/** Cross-platform process communication */
communication: 'message-passing';
}Error handling patterns for thread pool operations.
/**
* Thread pool error handling
*/
interface ThreadPoolErrorHandling {
/** Startup errors during thread pool initialization */
startupErrors: Error;
/** Compilation errors during file processing */
compilationErrors: Error;
/** Communication errors between main and worker processes */
communicationErrors: Error;
/** Resource exhaustion errors */
resourceErrors: Error;
}Error Handling Examples:
const pool = HappyPack.ThreadPool({ size: 4 });
// Handle startup errors
pool.start('compiler', compiler, options, (err) => {
if (err) {
console.error('Thread pool startup failed:', err.message);
// Fallback to synchronous processing
return;
}
console.log('Thread pool started successfully');
});
// Handle compilation errors
pool.compile('loader', loader, params, (err, result) => {
if (err) {
console.error('Compilation error:', err.message);
// Error is passed to webpack for handling
return;
}
// Process successful result
console.log('Compilation completed:', result);
});Guidelines for optimal thread pool configuration and usage.
/**
* Performance optimization guidelines
*/
interface PerformanceGuidelines {
/** Optimal thread count based on CPU cores */
threadCount: 'cpu_cores + 1';
/** Memory usage per thread */
memoryUsage: 'varies by loader complexity';
/** Thread startup overhead */
startupOverhead: 'high for small projects';
/** Ideal use cases */
useCase: 'large projects with heavy loaders';
}Performance Tips:
const os = require('os');
// CPU-based thread count
const optimalThreads = os.cpus().length;
const pool = HappyPack.ThreadPool({
size: Math.min(optimalThreads, 8) // Cap at 8 for diminishing returns
});
// Memory-conscious configuration for large projects
const memoryOptimizedPool = HappyPack.ThreadPool({
size: 4, // Lower thread count to reduce memory usage
verbose: false, // Reduce logging overhead
debug: false
});
// Development vs production configuration
const devPool = HappyPack.ThreadPool({
size: 2, // Faster startup for development
verbose: true // More feedback during development
});
const prodPool = HappyPack.ThreadPool({
size: 6, // More threads for production builds
verbose: false // Less logging for production
});