(proxy: Proxy)
| 692 | } |
| 693 | |
| 694 | fn start_wavekv_watch_task(proxy: Proxy) -> Result<()> { |
| 695 | let kv_store = proxy.kv_store.clone(); |
| 696 | |
| 697 | // Watch for instance changes |
| 698 | let proxy_clone = proxy.clone(); |
| 699 | let store_clone = kv_store.clone(); |
| 700 | // Register watcher first, then do initial load to avoid race condition |
| 701 | let mut rx = store_clone.watch_instances(); |
| 702 | reload_instances_from_kv_store(&proxy_clone, &store_clone) |
| 703 | .context("Failed to initial load instances from KvStore")?; |
| 704 | tokio::spawn(async move { |
| 705 | loop { |
| 706 | if rx.changed().await.is_err() { |
| 707 | break; |
| 708 | } |
| 709 | info!("WaveKV: detected remote instance changes, reloading..."); |
| 710 | if let Err(err) = reload_instances_from_kv_store(&proxy_clone, &store_clone) { |
| 711 | error!("Failed to reload instances from KvStore: {err:?}"); |
| 712 | } |
| 713 | } |
| 714 | }); |
| 715 | |
| 716 | // Initial WireGuard configuration |
| 717 | proxy.lock().reconfigure()?; |
| 718 | |
| 719 | // Watch for node changes and reconfigure WireGuard |
| 720 | let mut rx = kv_store.watch_nodes(); |
| 721 | let proxy_for_nodes = proxy.clone(); |
| 722 | tokio::spawn(async move { |
| 723 | loop { |
| 724 | if rx.changed().await.is_err() { |
| 725 | break; |
| 726 | } |
| 727 | info!("WaveKV: detected remote node changes, reconfiguring WireGuard..."); |
| 728 | if let Err(err) = proxy_for_nodes.lock().reconfigure() { |
| 729 | error!("Failed to reconfigure WireGuard: {err:?}"); |
| 730 | } |
| 731 | } |
| 732 | }); |
| 733 | |
| 734 | // Start periodic persistence task |
| 735 | let persist_interval = proxy.config.sync.persist_interval; |
| 736 | if !persist_interval.is_zero() { |
| 737 | let kv_store_for_persist = kv_store.clone(); |
| 738 | tokio::spawn(async move { |
| 739 | let mut ticker = tokio::time::interval(persist_interval); |
| 740 | loop { |
| 741 | ticker.tick().await; |
| 742 | match kv_store_for_persist.persist_if_dirty() { |
| 743 | Ok(true) => info!("WaveKV: periodic persist completed"), |
| 744 | Ok(false) => {} // No changes to persist |
| 745 | Err(err) => error!("WaveKV: periodic persist failed: {err:?}"), |
| 746 | } |
| 747 | } |
| 748 | }); |
| 749 | info!("WaveKV: periodic persistence enabled (interval: {persist_interval:?})"); |
| 750 | } |
| 751 |
no test coverage detected