@@ -2,12 +2,9 @@ mod config;
2
2
3
3
use core:: * ;
4
4
use config:: * ;
5
- use once_cell:: sync:: Lazy ;
6
5
use std:: { net:: SocketAddr , time:: Duration } ;
7
6
8
- use tokio:: { net:: { TcpListener , TcpStream } , io:: AsyncWriteExt , task:: JoinHandle , sync:: Mutex , time:: sleep} ;
9
-
10
- static API : Lazy < Mutex < String > > = Lazy :: new ( || Mutex :: new ( "" . into ( ) ) ) ;
7
+ use tokio:: { net:: { TcpListener , TcpStream } , io:: AsyncWriteExt , task:: JoinHandle , time:: sleep} ;
11
8
12
9
#[ tokio:: main]
13
10
async fn main ( ) {
@@ -19,23 +16,42 @@ async fn main() {
19
16
config_file = args. next ( ) ;
20
17
} ,
21
18
"--reload" | "-reload" | "--r" | "-r" => {
22
- let api = API . lock ( ) . await ;
23
- let _ = TcpStream :: connect ( api. as_str ( ) ) . await ;
24
- return ;
19
+ return reload ( config_file) . await ;
25
20
}
26
21
_ => { }
27
22
}
28
- } ;
23
+ }
29
24
loop {
30
25
boot ( config_file. clone ( ) ) . await ;
31
26
sleep ( Duration :: from_millis ( 5000 ) ) . await ;
32
27
}
33
28
}
34
29
30
+ /// 热重启, 目前暂时不支持修改热重启接口, 修改将导致无法再次通过命令行进行热重启
31
+ async fn reload ( config_file : Option < String > ) {
32
+ let config = match read_config ( config_file) . await {
33
+ Some ( v) => v,
34
+ None => return ,
35
+ } ;
36
+ match load_config ( & config) . await {
37
+ Ok ( ( _listen, api) ) => {
38
+ match TcpStream :: connect ( api) . await {
39
+ Ok ( _) => {
40
+ i ! ( "Restarting..." ) ;
41
+ }
42
+ _ => ( )
43
+ }
44
+ } ,
45
+ Err ( e) => {
46
+ return e ! ( "Config load failed: {e}" ) ;
47
+ }
48
+ }
49
+ }
50
+
35
51
async fn boot ( config_file : Option < String > ) {
36
- let config = match find_config ( config_file) . await {
37
- Ok ( v) => v,
38
- _ => return e ! ( "Not found the config file(such as: config.yml, oneport.yml)!" ) ,
52
+ let config = match read_config ( config_file) . await {
53
+ Some ( v) => v,
54
+ None => return ,
39
55
} ;
40
56
let ( listen, api) = match load_config ( & config) . await {
41
57
Ok ( ( listen, api) ) => {
@@ -47,12 +63,10 @@ async fn boot(config_file: Option<String>) {
47
63
}
48
64
} ;
49
65
let task = tokio:: spawn ( boot_oneport ( listen) ) ;
50
- let mut mutex_api = API . lock ( ) . await ;
51
- * mutex_api = api. clone ( ) ;
52
66
boot_api ( api, task) . await ;
53
67
}
54
68
55
- /// 默认监听 127.0.0.111:1111, 有客户端连接时 , 在不断开已有会话的前提下重启服务
69
+ /// 启动热重启服务, 默认监听 127.0.0.111:11111, 当有客户端连接时 , 在不断开已有会话的前提下重启服务
56
70
async fn boot_api ( api : String , task : JoinHandle < ( ) > ) {
57
71
i ! ( "Starting api service on {api}" ) ;
58
72
let listener = TcpListener :: bind ( api) . await . unwrap ( ) ;
@@ -66,6 +80,7 @@ async fn boot_api(api: String, task: JoinHandle<()>) {
66
80
}
67
81
}
68
82
83
+ /// 启动oneport主服务, 默认监听 0.0.0.0:1111
69
84
async fn boot_oneport ( listen : String ) {
70
85
i ! ( "Starting oneport service on {listen}" ) ;
71
86
let listener = TcpListener :: bind ( listen) . await . unwrap ( ) ;
@@ -75,6 +90,7 @@ async fn boot_oneport(listen: String) {
75
90
Err ( e) => unreachable ! ( "{:?}" , e) ,
76
91
} ;
77
92
i ! ( "Request {addr} incoming" ) ;
93
+ // Feature: 已有的会话不会在热重启时断开
78
94
tokio:: spawn ( async move {
79
95
serv ( visitor, addr) . await ;
80
96
} ) ;
@@ -100,9 +116,7 @@ async fn serv(mut visitor: TcpStream, addr: SocketAddr) {
100
116
return ;
101
117
}
102
118
}
103
- for i in 0 ..2 {
104
- d ! ( "Request {addr} msg[{}] = {}" , i, msg[ i] ) ;
105
- }
119
+ i ! ( "Request {addr} msg = {:?}" , & msg[ ..if msg. len( ) > 10 { 10 } else { msg. len( ) } ] ) ;
106
120
let rules = RULES . lock ( ) . await ;
107
121
let mut address = None ;
108
122
for ( rule, target) in rules. as_slice ( ) {
0 commit comments