1
- use std:: error:: Error ;
2
- use std:: { fs, sync:: Arc } ;
3
-
4
1
use async_trait:: async_trait;
5
- use notify:: { Event , RecommendedWatcher , RecursiveMode , Watcher } ;
2
+ use notify:: { PollWatcher , RecursiveMode , Watcher } ;
6
3
use pingora:: { server:: ShutdownWatch , services:: background:: BackgroundService } ;
7
4
use serde_json:: Value ;
8
- use tokio:: runtime:: { Handle , Runtime } ;
5
+ use std:: error:: Error ;
6
+ use std:: { fs, sync:: Arc } ;
9
7
use tracing:: { error, info, warn} ;
10
8
11
9
use crate :: { config:: Config , State , Tier } ;
@@ -42,16 +40,6 @@ impl TierBackgroundService {
42
40
}
43
41
}
44
42
45
- fn runtime_handle ( ) -> Handle {
46
- match Handle :: try_current ( ) {
47
- Ok ( h) => h,
48
- Err ( _) => {
49
- let rt = Runtime :: new ( ) . unwrap ( ) ;
50
- rt. handle ( ) . clone ( )
51
- }
52
- }
53
- }
54
-
55
43
#[ async_trait]
56
44
impl BackgroundService for TierBackgroundService {
57
45
async fn start ( & self , mut _shutdown : ShutdownWatch ) {
@@ -60,19 +48,13 @@ impl BackgroundService for TierBackgroundService {
60
48
return ;
61
49
}
62
50
63
- let ( tx, mut rx) = tokio :: sync:: mpsc:: channel :: < Event > ( 1 ) ;
51
+ let ( tx, rx) = std :: sync:: mpsc:: channel ( ) ;
64
52
65
- let watcher_result = RecommendedWatcher :: new (
66
- move |result : Result < Event , notify:: Error > | {
67
- let event = result. unwrap ( ) ;
68
- if event. kind . is_modify ( ) {
69
- runtime_handle ( ) . block_on ( async {
70
- tx. send ( event) . await . unwrap ( ) ;
71
- } ) ;
72
- }
73
- } ,
74
- notify:: Config :: default ( ) ,
75
- ) ;
53
+ let watcher_config = notify:: Config :: default ( )
54
+ . with_compare_contents ( true )
55
+ . with_poll_interval ( self . config . proxy_tiers_poll_interval ) ;
56
+
57
+ let watcher_result = PollWatcher :: new ( tx, watcher_config) ;
76
58
if let Err ( err) = watcher_result {
77
59
error ! ( error = err. to_string( ) , "error to watcher tier" ) ;
78
60
return ;
@@ -85,14 +67,17 @@ impl BackgroundService for TierBackgroundService {
85
67
return ;
86
68
}
87
69
88
- loop {
89
- if rx. recv ( ) . await . is_some ( ) {
90
- if let Err ( err) = self . update_tiers ( ) . await {
91
- error ! ( error = err. to_string( ) , "error to update tiers" ) ;
92
- continue ;
93
- }
70
+ for result in rx {
71
+ match result {
72
+ Ok ( _event) => {
73
+ if let Err ( err) = self . update_tiers ( ) . await {
74
+ error ! ( error = err. to_string( ) , "error to update tiers" ) ;
75
+ continue ;
76
+ }
94
77
95
- info ! ( "tiers modified" ) ;
78
+ info ! ( "tiers modified" ) ;
79
+ }
80
+ Err ( err) => error ! ( error = err. to_string( ) , "watch error" ) ,
96
81
}
97
82
}
98
83
}
0 commit comments