oxigdal-wasm 0.1.4

WebAssembly bindings for OxiGDAL - Browser-based geospatial processing
Documentation
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
//! Web Worker support for parallel tile loading
//!
//! This module provides Web Worker communication, parallel tile fetching,
//! job queue management, worker pool coordination, and progress tracking for
//! high-performance geospatial data visualization in the browser.
//!
//! # Overview
//!
//! The worker module implements a sophisticated worker pool system for parallel
//! tile loading:
//!
//! - **Worker Pool**: Manages multiple Web Workers for concurrent tile loading
//! - **Job Queue**: FIFO queue with support for job prioritization
//! - **Load Balancing**: Distributes work evenly across available workers
//! - **Timeout Handling**: Detects and recovers from stuck workers
//! - **Progress Tracking**: Monitors tile loading progress for UI updates
//! - **Error Recovery**: Handles worker failures gracefully
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────┐
//! │   Main Thread   │
//! │  (Rust/WASM)    │
//! └────────┬────────┘
//!//!          │ Job Queue
//!//! ┌────────▼────────────────────────────────┐
//! │         Worker Pool Manager             │
//! │  - Job scheduling                       │
//! │  - Worker health monitoring             │
//! │  - Load balancing                       │
//! └───┬─────────┬─────────┬────────┬────────┘
//!     │         │         │        │
//! ┌───▼───┐ ┌──▼───┐ ┌───▼──┐ ┌───▼───┐
//! │Worker │ │Worker│ │Worker│ │Worker │
//! │  #1   │ │  #2  │ │  #3  │ │  #4   │
//! └───────┘ └──────┘ └──────┘ └───────┘
//!     │         │         │        │
//!     └─────────┴─────────┴────────┘
//!              Network
//! ```
//!
//! # Web Worker Basics
//!
//! Web Workers run JavaScript/WASM code in separate threads, allowing:
//! - True parallel execution (not just concurrent)
//! - No blocking of main UI thread
//! - Independent memory space per worker
//! - Communication via postMessage
//!
//! Limitations:
//! - No direct DOM access
//! - Message passing overhead (serialization)
//! - Limited to ~4-8 workers per page (browser dependent)
//!
//! # Job Lifecycle
//!
//! ```text
//! Submit ─> Pending ─> In Progress ─> Completed
//!//!                          └────────> Failed/Timed Out
//! ```
//!
//! 1. **Submit**: Job is added to the queue
//! 2. **Pending**: Job waits for available worker
//! 3. **In Progress**: Worker is processing the job
//! 4. **Completed**: Job finished successfully
//! 5. **Failed**: Job encountered an error
//! 6. **Timed Out**: Job exceeded time limit
//!
//! # Performance Characteristics
//!
//! Overhead costs:
//! - Worker creation: ~50-100ms per worker
//! - Message passing: ~0.1-1ms per message
//! - Job dispatch: ~0.1ms
//! - Serialization: ~0.5ms per 256KB tile
//!
//! Optimal pool size:
//! - CPU-bound work: Number of cores (typically 4-8)
//! - Network-bound work: 2-4 workers
//! - Mixed workload: 3-6 workers
//!
//! Throughput example (4 workers, good network):
//! - Sequential: 4 tiles/sec
//! - Parallel: 12-16 tiles/sec (3-4x speedup)
//!
//! # Example: Basic Worker Pool
//!
//! ```ignore
//! use oxigdal_wasm::worker::{WorkerPool, WorkerJobRequest, WorkerRequestType};
//! use oxigdal_wasm::tile::TileCoord;
//!
//! // Create pool with 4 workers
//! let mut pool = WorkerPool::new(4).expect("Failed to create pool");
//!
//! // Submit a tile loading job
//! let request = WorkerJobRequest {
//!     job_id: 0,
//!     request_type: WorkerRequestType::LoadTile {
//!         url: "https://example.com/image.tif".to_string(),
//!         coord: TileCoord::new(0, 0, 0),
//!     },
//! };
//!
//! let job_id = pool.submit_job(request, 0.0, 30000)
//!     .expect("Failed to submit job");
//!
//! println!("Job {} submitted", job_id);
//!
//! // Check pool statistics
//! let stats = pool.stats();
//! println!("Pool utilization: {:.1}%", stats.utilization() * 100.0);
//! ```
//!
//! # Example: Job Prioritization
//!
//! ```ignore
//! use oxigdal_wasm::worker::{WorkerPool, WorkerJobRequest, WorkerRequestType};
//! use oxigdal_wasm::tile::TileCoord;
//!
//! let mut pool = WorkerPool::new(4).expect("Create failed");
//!
//! // Submit high-priority jobs (visible tiles)
//! for coord in visible_tiles {
//!     let request = WorkerJobRequest {
//!         job_id: 0,
//!         request_type: WorkerRequestType::LoadTile {
//!             url: url.clone(),
//!             coord,
//!         },
//!     };
//!     pool.submit_job(request, timestamp, 10000)?;
//! }
//!
//! // Submit low-priority jobs (prefetch)
//! for coord in prefetch_tiles {
//!     let request = WorkerJobRequest {
//!         job_id: 0,
//!         request_type: WorkerRequestType::Prefetch {
//!             url: url.clone(),
//!             coords: vec![coord],
//!         },
//!     };
//!     pool.submit_job(request, timestamp, 60000)?;
//! }
//! ```
//!
//! # Example: Monitoring and Health Checks
//!
//! ```ignore
//! use oxigdal_wasm::worker::WorkerPool;
//!
//! let mut pool = WorkerPool::new(4).expect("Create failed");
//!
//! // Periodically check for timeouts
//! loop {
//!     let current_time = js_sys::Date::now();
//!     let timed_out = pool.check_timeouts(current_time);
//!
//!     for job_id in timed_out {
//!         println!("Job {} timed out!", job_id);
//!         // Resubmit or report error
//!     }
//!
//!     // Check pool health
//!     let stats = pool.stats();
//!     if stats.idle_workers == 0 && stats.pending_jobs > 10 {
//!         println!("Warning: Pool is saturated!");
//!         // Consider reducing load or increasing pool size
//!     }
//!
//!     // Wait before next check
//!     await sleep(1000);
//! }
//! ```
//!
//! # Best Practices
//!
//! ## Pool Sizing
//! - Start with 3-4 workers for most applications
//! - Increase to 6-8 for high-bandwidth connections
//! - Decrease to 2-3 for mobile devices
//! - Monitor CPU usage and adjust accordingly
//!
//! ## Job Management
//! - Set appropriate timeouts (10-30 seconds typical)
//! - Clean up completed jobs periodically
//! - Prioritize visible content over prefetch
//! - Batch small jobs to reduce overhead
//!
//! ## Error Handling
//! - Always handle job failures
//! - Implement retry logic for transient errors
//! - Report persistent failures to user
//! - Gracefully degrade on worker unavailability
//!
//! ## Memory Management
//! - Limit concurrent jobs to prevent memory pressure
//! - Transfer large ArrayBuffers (use Transferable)
//! - Clean up job results after processing
//! - Monitor overall memory usage
//!
//! # Troubleshooting
//!
//! ## Workers not starting
//! - Check CORS headers on worker script
//! - Verify worker script URL is correct
//! - Check browser console for errors
//! - Ensure WASM module is loaded in worker
//!
//! ## Poor performance
//! - Reduce number of workers (might be too many)
//! - Check network latency (workers waiting on network)
//! - Profile worker code for bottlenecks
//! - Verify efficient message passing (avoid large copies)
//!
//! ## Jobs timing out
//! - Increase timeout duration
//! - Check network reliability
//! - Verify server response times
//! - Implement retry logic for failed requests

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use wasm_bindgen::prelude::*;
use web_sys::{Worker, WorkerOptions, WorkerType};

use crate::error::{WasmError, WasmResult, WorkerError};
use crate::tile::TileCoord;

/// Maximum number of workers in the pool
#[allow(dead_code)]
pub const DEFAULT_WORKER_POOL_SIZE: usize = 4;

/// Maximum number of pending jobs per worker
pub const MAX_PENDING_JOBS_PER_WORKER: usize = 10;

/// Job timeout in milliseconds
#[allow(dead_code)]
pub const DEFAULT_JOB_TIMEOUT_MS: u64 = 30000;

/// Unique job identifier
pub type JobId = u64;

/// Worker job request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkerJobRequest {
    /// Job ID
    pub job_id: JobId,
    /// Request type
    pub request_type: WorkerRequestType,
}

/// Worker request types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum WorkerRequestType {
    /// Load a tile
    LoadTile {
        /// URL of the COG
        url: String,
        /// Tile coordinate
        coord: TileCoord,
    },
    /// Load multiple tiles
    LoadTiles {
        /// URL of the COG
        url: String,
        /// Tile coordinates
        coords: Vec<TileCoord>,
    },
    /// Prefetch tiles
    Prefetch {
        /// URL of the COG
        url: String,
        /// Tile coordinates
        coords: Vec<TileCoord>,
    },
    /// Get metadata
    GetMetadata {
        /// URL of the COG
        url: String,
    },
}

/// Worker job response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkerJobResponse {
    /// Job ID
    pub job_id: JobId,
    /// Response type
    pub response_type: WorkerResponseType,
}

/// Worker response types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum WorkerResponseType {
    /// Tile loaded successfully
    TileLoaded {
        /// Tile coordinate
        coord: TileCoord,
        /// Tile data
        data: Vec<u8>,
    },
    /// Multiple tiles loaded
    TilesLoaded {
        /// Tiles data
        tiles: Vec<(TileCoord, Vec<u8>)>,
    },
    /// Prefetch completed
    PrefetchCompleted {
        /// Number of tiles prefetched
        count: usize,
    },
    /// Metadata retrieved
    Metadata {
        /// Metadata JSON
        metadata: String,
    },
    /// Error occurred
    Error {
        /// Error message
        message: String,
    },
}

/// Job status
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JobStatus {
    /// Job is pending
    Pending,
    /// Job is in progress
    InProgress,
    /// Job completed successfully
    Completed,
    /// Job failed
    Failed,
    /// Job timed out
    TimedOut,
}

/// Pending job information
#[derive(Debug, Clone)]
pub struct PendingJob {
    /// Job ID
    pub job_id: JobId,
    /// Worker ID (if assigned)
    pub worker_id: Option<u32>,
    /// Job request
    pub request: WorkerJobRequest,
    /// Job status
    pub status: JobStatus,
    /// Submission timestamp
    pub submitted_at: f64,
    /// Start timestamp (when worker started processing)
    pub started_at: Option<f64>,
    /// Completion timestamp
    pub completed_at: Option<f64>,
    /// Timeout duration in milliseconds
    pub timeout_ms: u64,
}

impl PendingJob {
    /// Creates a new pending job
    pub fn new(job_id: JobId, request: WorkerJobRequest, timestamp: f64, timeout_ms: u64) -> Self {
        Self {
            job_id,
            worker_id: None,
            request,
            status: JobStatus::Pending,
            submitted_at: timestamp,
            started_at: None,
            completed_at: None,
            timeout_ms,
        }
    }

    /// Checks if the job has timed out
    pub fn is_timed_out(&self, current_time: f64) -> bool {
        if let Some(started) = self.started_at {
            (current_time - started) * 1000.0 > self.timeout_ms as f64
        } else {
            false
        }
    }

    /// Returns the elapsed time in milliseconds
    pub fn elapsed_ms(&self, current_time: f64) -> f64 {
        if let Some(started) = self.started_at {
            (current_time - started) * 1000.0
        } else {
            0.0
        }
    }
}

/// Worker information
#[derive(Debug)]
pub struct WorkerInfo {
    /// Worker ID
    pub id: u32,
    /// Worker instance
    pub worker: Worker,
    /// Current job (if any)
    pub current_job: Option<JobId>,
    /// Number of completed jobs
    pub completed_jobs: u64,
    /// Number of failed jobs
    pub failed_jobs: u64,
    /// Total processing time in milliseconds
    pub total_processing_time_ms: f64,
}

impl WorkerInfo {
    /// Creates a new worker info
    pub fn new(id: u32, worker: Worker) -> Self {
        Self {
            id,
            worker,
            current_job: None,
            completed_jobs: 0,
            failed_jobs: 0,
            total_processing_time_ms: 0.0,
        }
    }

    /// Checks if the worker is idle
    pub const fn is_idle(&self) -> bool {
        self.current_job.is_none()
    }

    /// Returns the average processing time per job
    pub fn average_processing_time_ms(&self) -> f64 {
        let total_jobs = self.completed_jobs + self.failed_jobs;
        if total_jobs == 0 {
            0.0
        } else {
            self.total_processing_time_ms / total_jobs as f64
        }
    }
}

/// Worker pool for parallel tile loading
pub struct WorkerPool {
    /// Workers in the pool
    workers: Vec<WorkerInfo>,
    /// Job queue
    job_queue: VecDeque<JobId>,
    /// Pending jobs
    pending_jobs: HashMap<JobId, PendingJob>,
    /// Next job ID
    next_job_id: JobId,
    /// Pool size
    pool_size: usize,
    /// Maximum pending jobs
    max_pending_jobs: usize,
}

impl WorkerPool {
    /// Creates a new worker pool
    pub fn new(pool_size: usize) -> WasmResult<Self> {
        let mut workers = Vec::with_capacity(pool_size);

        for i in 0..pool_size {
            let worker = Self::create_worker(i as u32)?;
            workers.push(WorkerInfo::new(i as u32, worker));
        }

        Ok(Self {
            workers,
            job_queue: VecDeque::new(),
            pending_jobs: HashMap::new(),
            next_job_id: 0,
            pool_size,
            max_pending_jobs: pool_size * MAX_PENDING_JOBS_PER_WORKER,
        })
    }

    /// Creates a new worker
    fn create_worker(_id: u32) -> WasmResult<Worker> {
        let options = WorkerOptions::new();
        options.set_type(WorkerType::Module);

        // Create worker from script URL
        // The worker script should be served alongside the main application
        let worker = Worker::new_with_options("./cog-worker.js", &options).map_err(|e| {
            WasmError::Worker(WorkerError::CreationFailed {
                message: format!("Failed to create worker: {e:?}"),
            })
        })?;

        Ok(worker)
    }

    /// Submits a job to the pool
    pub fn submit_job(
        &mut self,
        request: WorkerJobRequest,
        timestamp: f64,
        timeout_ms: u64,
    ) -> WasmResult<JobId> {
        if self.pending_jobs.len() >= self.max_pending_jobs {
            return Err(WasmError::Worker(WorkerError::PoolExhausted {
                pool_size: self.pool_size,
                pending_jobs: self.pending_jobs.len(),
            }));
        }

        let job_id = self.next_job_id;
        self.next_job_id += 1;

        let job = PendingJob::new(job_id, request, timestamp, timeout_ms);
        self.pending_jobs.insert(job_id, job);
        self.job_queue.push_back(job_id);

        self.dispatch_jobs(timestamp)?;

        Ok(job_id)
    }

    /// Dispatches pending jobs to idle workers
    fn dispatch_jobs(&mut self, timestamp: f64) -> WasmResult<()> {
        while let Some(job_id) = self.job_queue.pop_front() {
            // Find an idle worker
            let worker_idx = self.workers.iter().position(|w| w.is_idle());

            if let Some(idx) = worker_idx {
                // Assign job to worker
                self.workers[idx].current_job = Some(job_id);

                if let Some(job) = self.pending_jobs.get_mut(&job_id) {
                    job.worker_id = Some(self.workers[idx].id);
                    job.status = JobStatus::InProgress;
                    job.started_at = Some(timestamp);

                    // Post message to worker
                    let message = serde_json::to_string(&job.request).map_err(|e| {
                        WasmError::Worker(WorkerError::PostMessageFailed {
                            worker_id: self.workers[idx].id,
                            message: e.to_string(),
                        })
                    })?;

                    self.workers[idx]
                        .worker
                        .post_message(&JsValue::from_str(&message))
                        .map_err(|e| {
                            WasmError::Worker(WorkerError::PostMessageFailed {
                                worker_id: self.workers[idx].id,
                                message: format!("{e:?}"),
                            })
                        })?;
                }
            } else {
                // No idle workers, put job back in queue
                self.job_queue.push_front(job_id);
                break;
            }
        }

        Ok(())
    }

    /// Handles a worker response
    pub fn handle_response(
        &mut self,
        worker_id: u32,
        response: WorkerJobResponse,
        timestamp: f64,
    ) -> WasmResult<()> {
        let job_id = response.job_id;

        // Find the worker
        let worker_idx = self
            .workers
            .iter()
            .position(|w| w.id == worker_id)
            .ok_or_else(|| {
                WasmError::Worker(WorkerError::InvalidResponse {
                    expected: format!("worker {worker_id}"),
                    actual: "unknown worker".to_string(),
                })
            })?;

        // Update job status
        if let Some(job) = self.pending_jobs.get_mut(&job_id) {
            let elapsed = if let Some(started) = job.started_at {
                (timestamp - started) * 1000.0
            } else {
                0.0
            };

            match &response.response_type {
                WorkerResponseType::Error { .. } => {
                    job.status = JobStatus::Failed;
                    self.workers[worker_idx].failed_jobs += 1;
                }
                _ => {
                    job.status = JobStatus::Completed;
                    self.workers[worker_idx].completed_jobs += 1;
                }
            }

            job.completed_at = Some(timestamp);
            self.workers[worker_idx].total_processing_time_ms += elapsed;
        }

        // Mark worker as idle
        self.workers[worker_idx].current_job = None;

        // Dispatch next job
        self.dispatch_jobs(timestamp)?;

        Ok(())
    }

    /// Checks for timed out jobs
    pub fn check_timeouts(&mut self, current_time: f64) -> Vec<JobId> {
        let mut timed_out = Vec::new();

        for (job_id, job) in &mut self.pending_jobs {
            if job.status == JobStatus::InProgress && job.is_timed_out(current_time) {
                job.status = JobStatus::TimedOut;
                timed_out.push(*job_id);

                // Mark worker as idle
                if let Some(worker_id) = job.worker_id {
                    if let Some(worker) = self.workers.iter_mut().find(|w| w.id == worker_id) {
                        worker.current_job = None;
                        worker.failed_jobs += 1;
                    }
                }
            }
        }

        timed_out
    }

    /// Returns the job status
    pub fn job_status(&self, job_id: JobId) -> Option<JobStatus> {
        self.pending_jobs.get(&job_id).map(|j| j.status)
    }

    /// Cancels a job
    pub fn cancel_job(&mut self, job_id: JobId) -> WasmResult<()> {
        if let Some(job) = self.pending_jobs.get_mut(&job_id) {
            job.status = JobStatus::Failed;

            // Remove from queue if pending
            if let Some(pos) = self.job_queue.iter().position(|&id| id == job_id) {
                self.job_queue.remove(pos);
            }

            // If job is in progress, we can't easily stop the worker
            // Just mark it as failed and the worker will be released when it responds
        }

        Ok(())
    }

    /// Returns pool statistics
    pub fn stats(&self) -> PoolStats {
        let idle_workers = self.workers.iter().filter(|w| w.is_idle()).count();
        let total_completed: u64 = self.workers.iter().map(|w| w.completed_jobs).sum();
        let total_failed: u64 = self.workers.iter().map(|w| w.failed_jobs).sum();

        PoolStats {
            pool_size: self.pool_size,
            idle_workers,
            pending_jobs: self.job_queue.len(),
            total_jobs: self.pending_jobs.len(),
            completed_jobs: total_completed,
            failed_jobs: total_failed,
        }
    }

    /// Clears completed and failed jobs
    pub fn cleanup_jobs(&mut self) {
        self.pending_jobs.retain(|_, job| {
            job.status == JobStatus::Pending || job.status == JobStatus::InProgress
        });
    }

    /// Shuts down the worker pool
    pub fn shutdown(&mut self) {
        // Terminate all workers
        for worker in &self.workers {
            worker.worker.terminate();
        }
        self.workers.clear();
        self.job_queue.clear();
        self.pending_jobs.clear();
    }
}

/// Worker pool statistics
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct PoolStats {
    /// Total pool size
    pub pool_size: usize,
    /// Number of idle workers
    pub idle_workers: usize,
    /// Number of pending jobs in queue
    pub pending_jobs: usize,
    /// Total number of jobs (including in progress)
    pub total_jobs: usize,
    /// Number of completed jobs
    pub completed_jobs: u64,
    /// Number of failed jobs
    pub failed_jobs: u64,
}

impl PoolStats {
    /// Returns the pool utilization (fraction of busy workers)
    pub fn utilization(&self) -> f64 {
        if self.pool_size == 0 {
            0.0
        } else {
            (self.pool_size - self.idle_workers) as f64 / self.pool_size as f64
        }
    }

    /// Returns the success rate
    pub fn success_rate(&self) -> f64 {
        let total = self.completed_jobs + self.failed_jobs;
        if total == 0 {
            1.0
        } else {
            self.completed_jobs as f64 / total as f64
        }
    }
}

/// Tile loading coordinator
#[allow(dead_code)]
pub struct TileLoadCoordinator {
    /// Worker pool
    pool: WorkerPool,
    /// Job callbacks
    callbacks: HashMap<JobId, Box<dyn FnOnce(Result<WorkerJobResponse, WasmError>)>>,
}

#[allow(dead_code)]
impl TileLoadCoordinator {
    /// Creates a new tile load coordinator
    pub fn new(pool_size: usize) -> WasmResult<Self> {
        let pool = WorkerPool::new(pool_size)?;
        Ok(Self {
            pool,
            callbacks: HashMap::new(),
        })
    }

    /// Loads a tile asynchronously
    pub fn load_tile<F>(
        &mut self,
        url: String,
        coord: TileCoord,
        timestamp: f64,
        callback: F,
    ) -> WasmResult<JobId>
    where
        F: FnOnce(Result<Vec<u8>, WasmError>) + 'static,
    {
        let request = WorkerJobRequest {
            job_id: 0, // Will be set by submit_job
            request_type: WorkerRequestType::LoadTile { url, coord },
        };

        let job_id = self
            .pool
            .submit_job(request, timestamp, DEFAULT_JOB_TIMEOUT_MS)?;

        // Wrap callback
        let wrapped = Box::new(
            move |result: Result<WorkerJobResponse, WasmError>| match result {
                Ok(response) => match response.response_type {
                    WorkerResponseType::TileLoaded { data, .. } => callback(Ok(data)),
                    WorkerResponseType::Error { message } => {
                        callback(Err(WasmError::Worker(WorkerError::InvalidResponse {
                            expected: "TileLoaded".to_string(),
                            actual: message,
                        })))
                    }
                    _ => callback(Err(WasmError::Worker(WorkerError::InvalidResponse {
                        expected: "TileLoaded".to_string(),
                        actual: format!("{:?}", response.response_type),
                    }))),
                },
                Err(e) => callback(Err(e)),
            },
        );

        self.callbacks.insert(job_id, wrapped);

        Ok(job_id)
    }

    /// Loads multiple tiles asynchronously
    pub fn load_tiles<F>(
        &mut self,
        url: String,
        coords: Vec<TileCoord>,
        timestamp: f64,
        callback: F,
    ) -> WasmResult<JobId>
    where
        F: FnOnce(Result<Vec<(TileCoord, Vec<u8>)>, WasmError>) + 'static,
    {
        let request = WorkerJobRequest {
            job_id: 0,
            request_type: WorkerRequestType::LoadTiles { url, coords },
        };

        let job_id = self
            .pool
            .submit_job(request, timestamp, DEFAULT_JOB_TIMEOUT_MS)?;

        let wrapped = Box::new(
            move |result: Result<WorkerJobResponse, WasmError>| match result {
                Ok(response) => match response.response_type {
                    WorkerResponseType::TilesLoaded { tiles } => callback(Ok(tiles)),
                    WorkerResponseType::Error { message } => {
                        callback(Err(WasmError::Worker(WorkerError::InvalidResponse {
                            expected: "TilesLoaded".to_string(),
                            actual: message,
                        })))
                    }
                    _ => callback(Err(WasmError::Worker(WorkerError::InvalidResponse {
                        expected: "TilesLoaded".to_string(),
                        actual: format!("{:?}", response.response_type),
                    }))),
                },
                Err(e) => callback(Err(e)),
            },
        );

        self.callbacks.insert(job_id, wrapped);

        Ok(job_id)
    }

    /// Returns pool statistics
    pub fn stats(&self) -> PoolStats {
        self.pool.stats()
    }
}

/// WASM bindings for worker pool (for demonstration/testing)
#[wasm_bindgen]
pub struct WasmWorkerPool {
    pool_size: usize,
}

#[wasm_bindgen]
impl WasmWorkerPool {
    /// Creates a new worker pool
    #[wasm_bindgen(constructor)]
    pub fn new(pool_size: usize) -> Self {
        Self { pool_size }
    }

    /// Returns the pool size
    #[wasm_bindgen(js_name = poolSize)]
    pub fn pool_size(&self) -> usize {
        self.pool_size
    }

    /// Returns a message about worker support
    #[wasm_bindgen(js_name = getInfo)]
    pub fn get_info(&self) -> String {
        format!(
            "Worker pool configured with {} workers. Worker creation requires a separate worker script.",
            self.pool_size
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_pending_job() {
        let request = WorkerJobRequest {
            job_id: 1,
            request_type: WorkerRequestType::GetMetadata {
                url: "test.tif".to_string(),
            },
        };

        let job = PendingJob::new(1, request, 0.0, 1000);
        assert_eq!(job.job_id, 1);
        assert_eq!(job.status, JobStatus::Pending);
    }

    #[test]
    fn test_job_timeout() {
        let request = WorkerJobRequest {
            job_id: 1,
            request_type: WorkerRequestType::GetMetadata {
                url: "test.tif".to_string(),
            },
        };

        let mut job = PendingJob::new(1, request, 0.0, 1000);
        job.started_at = Some(0.0);

        assert!(!job.is_timed_out(0.5)); // 500ms elapsed
        assert!(job.is_timed_out(2.0)); // 2000ms elapsed
    }

    #[test]
    fn test_pool_stats() {
        let stats = PoolStats {
            pool_size: 4,
            idle_workers: 2,
            pending_jobs: 5,
            total_jobs: 10,
            completed_jobs: 100,
            failed_jobs: 10,
        };

        assert_eq!(stats.utilization(), 0.5); // 2 out of 4 workers busy
        assert!((stats.success_rate() - 0.909).abs() < 0.01); // 100/110
    }

    #[test]
    fn test_job_request_serialization() {
        let request = WorkerJobRequest {
            job_id: 42,
            request_type: WorkerRequestType::LoadTile {
                url: "test.tif".to_string(),
                coord: TileCoord::new(5, 10, 20),
            },
        };

        let json = serde_json::to_string(&request).expect("Serialization failed");
        let parsed: WorkerJobRequest = serde_json::from_str(&json).expect("Deserialization failed");

        assert_eq!(parsed.job_id, 42);
    }

    #[test]
    fn test_wasm_worker_pool() {
        let pool = WasmWorkerPool::new(4);
        assert_eq!(pool.pool_size(), 4);

        let info = pool.get_info();
        assert!(info.contains("4 workers"));
    }
}