@@ -125,15 +125,21 @@ impl ServerBuilder {
125
125
126
126
/// Sets the maximum per-worker number of concurrent connections.
127
127
///
128
- /// All socket listeners will stop accepting connections when this limit is
129
- /// reached for each worker.
128
+ /// All socket listeners will stop accepting connections when this limit is reached for
129
+ /// each worker.
130
130
///
131
- /// By default max connections is set to a 25k per worker.
132
- pub fn maxconn ( mut self , num : usize ) -> Self {
131
+ /// By default max connections is set to a 25,600 per worker.
132
+ pub fn max_concurrent_connections ( mut self , num : usize ) -> Self {
133
133
self . worker_config . max_concurrent_connections ( num) ;
134
134
self
135
135
}
136
136
137
+ #[ doc( hidden) ]
138
+ #[ deprecated( since = "2.0.0" , note = "Renamed to `max_concurrent_connections`." ) ]
139
+ pub fn maxconn ( self , num : usize ) -> Self {
140
+ self . max_concurrent_connections ( num)
141
+ }
142
+
137
143
/// Stop Actix system.
138
144
pub fn system_exit ( mut self ) -> Self {
139
145
self . exit = true ;
@@ -158,7 +164,11 @@ impl ServerBuilder {
158
164
self
159
165
}
160
166
161
- /// Add new service to the server.
167
+ /// Bind server to socket addresses.
168
+ ///
169
+ /// Binds to all network interface addresses that resolve from the `addr` argument.
170
+ /// Eg. using `localhost` might bind to both IPv4 and IPv6 addresses. Bind to multiple distinct
171
+ /// interfaces at the same time by passing a list of socket addresses.
162
172
pub fn bind < F , U , N : AsRef < str > > ( mut self , name : N , addr : U , factory : F ) -> io:: Result < Self >
163
173
where
164
174
F : ServiceFactory < TcpStream > ,
@@ -180,56 +190,9 @@ impl ServerBuilder {
180
190
Ok ( self )
181
191
}
182
192
183
- /// Add new unix domain service to the server.
184
- #[ cfg( unix) ]
185
- pub fn bind_uds < F , U , N > ( self , name : N , addr : U , factory : F ) -> io:: Result < Self >
186
- where
187
- F : ServiceFactory < actix_rt:: net:: UnixStream > ,
188
- N : AsRef < str > ,
189
- U : AsRef < std:: path:: Path > ,
190
- {
191
- // The path must not exist when we try to bind.
192
- // Try to remove it to avoid bind error.
193
- if let Err ( e) = std:: fs:: remove_file ( addr. as_ref ( ) ) {
194
- // NotFound is expected and not an issue. Anything else is.
195
- if e. kind ( ) != std:: io:: ErrorKind :: NotFound {
196
- return Err ( e) ;
197
- }
198
- }
199
-
200
- let lst = crate :: socket:: StdUnixListener :: bind ( addr) ?;
201
- self . listen_uds ( name, lst, factory)
202
- }
203
-
204
- /// Add new unix domain service to the server.
205
- /// Useful when running as a systemd service and
206
- /// a socket FD can be acquired using the systemd crate.
207
- #[ cfg( unix) ]
208
- pub fn listen_uds < F , N : AsRef < str > > (
209
- mut self ,
210
- name : N ,
211
- lst : crate :: socket:: StdUnixListener ,
212
- factory : F ,
213
- ) -> io:: Result < Self >
214
- where
215
- F : ServiceFactory < actix_rt:: net:: UnixStream > ,
216
- {
217
- use std:: net:: { IpAddr , Ipv4Addr } ;
218
- lst. set_nonblocking ( true ) ?;
219
- let token = self . next_token ( ) ;
220
- let addr = StdSocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , 8080 ) ;
221
- self . services . push ( StreamNewService :: create (
222
- name. as_ref ( ) . to_string ( ) ,
223
- token,
224
- factory,
225
- addr,
226
- ) ) ;
227
- self . sockets
228
- . push ( ( token, name. as_ref ( ) . to_string ( ) , MioListener :: from ( lst) ) ) ;
229
- Ok ( self )
230
- }
231
-
232
- /// Add new service to the server.
193
+ /// Bind server to existing TCP listener.
194
+ ///
195
+ /// Useful when running as a systemd service and a socket FD can be passed to the process.
233
196
pub fn listen < F , N : AsRef < str > > (
234
197
mut self ,
235
198
name : N ,
@@ -240,9 +203,10 @@ impl ServerBuilder {
240
203
F : ServiceFactory < TcpStream > ,
241
204
{
242
205
lst. set_nonblocking ( true ) ?;
243
- let addr = lst. local_addr ( ) ?;
244
206
207
+ let addr = lst. local_addr ( ) ?;
245
208
let token = self . next_token ( ) ;
209
+
246
210
self . services . push ( StreamNewService :: create (
247
211
name. as_ref ( ) . to_string ( ) ,
248
212
token,
@@ -434,6 +398,62 @@ impl ServerBuilder {
434
398
}
435
399
}
436
400
401
+ /// Unix Domain Socket (UDS) support.
402
+ #[ cfg( unix) ]
403
+ impl ServerBuilder {
404
+ /// Add new unix domain service to the server.
405
+ pub fn bind_uds < F , U , N > ( self , name : N , addr : U , factory : F ) -> io:: Result < Self >
406
+ where
407
+ F : ServiceFactory < actix_rt:: net:: UnixStream > ,
408
+ N : AsRef < str > ,
409
+ U : AsRef < std:: path:: Path > ,
410
+ {
411
+ // The path must not exist when we try to bind.
412
+ // Try to remove it to avoid bind error.
413
+ if let Err ( e) = std:: fs:: remove_file ( addr. as_ref ( ) ) {
414
+ // NotFound is expected and not an issue. Anything else is.
415
+ if e. kind ( ) != std:: io:: ErrorKind :: NotFound {
416
+ return Err ( e) ;
417
+ }
418
+ }
419
+
420
+ let lst = crate :: socket:: StdUnixListener :: bind ( addr) ?;
421
+ self . listen_uds ( name, lst, factory)
422
+ }
423
+
424
+ /// Add new unix domain service to the server.
425
+ ///
426
+ /// Useful when running as a systemd service and a socket FD can be passed to the process.
427
+ pub fn listen_uds < F , N : AsRef < str > > (
428
+ mut self ,
429
+ name : N ,
430
+ lst : crate :: socket:: StdUnixListener ,
431
+ factory : F ,
432
+ ) -> io:: Result < Self >
433
+ where
434
+ F : ServiceFactory < actix_rt:: net:: UnixStream > ,
435
+ {
436
+ use std:: net:: { IpAddr , Ipv4Addr } ;
437
+
438
+ lst. set_nonblocking ( true ) ?;
439
+
440
+ let addr = StdSocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , 8080 ) ;
441
+ let token = self . next_token ( ) ;
442
+
443
+ self . services . push ( StreamNewService :: create (
444
+ name. as_ref ( ) . to_string ( ) ,
445
+ token,
446
+ factory,
447
+ addr,
448
+ ) ) ;
449
+
450
+ self . sockets
451
+ . push ( ( token, name. as_ref ( ) . to_string ( ) , MioListener :: from ( lst) ) ) ;
452
+
453
+ Ok ( self )
454
+ }
455
+ }
456
+
437
457
impl Future for ServerBuilder {
438
458
type Output = ( ) ;
439
459
@@ -452,29 +472,30 @@ pub(super) fn bind_addr<S: ToSocketAddrs>(
452
472
backlog : u32 ,
453
473
) -> io:: Result < Vec < MioTcpListener > > {
454
474
let mut err = None ;
455
- let mut succ = false ;
475
+ let mut success = false ;
456
476
let mut sockets = Vec :: new ( ) ;
477
+
457
478
for addr in addr. to_socket_addrs ( ) ? {
458
479
match create_tcp_listener ( addr, backlog) {
459
480
Ok ( lst) => {
460
- succ = true ;
481
+ success = true ;
461
482
sockets. push ( lst) ;
462
483
}
463
484
Err ( e) => err = Some ( e) ,
464
485
}
465
486
}
466
487
467
- if !succ {
468
- if let Some ( e) = err. take ( ) {
469
- Err ( e)
488
+ if success {
489
+ Ok ( sockets)
490
+ } else {
491
+ if let Some ( err) = err. take ( ) {
492
+ Err ( err)
470
493
} else {
471
494
Err ( io:: Error :: new (
472
495
io:: ErrorKind :: Other ,
473
- "Can not bind to address. " ,
496
+ "Can not bind to socket address" ,
474
497
) )
475
498
}
476
- } else {
477
- Ok ( sockets)
478
499
}
479
500
}
480
501
0 commit comments