Skip to content

Commit 6ce8307

Browse files
authored
remove num_cpus dependency (#488)
1 parent 9cb8a1f commit 6ce8307

File tree

6 files changed

+115
-49
lines changed

6 files changed

+115
-49
lines changed

.github/workflows/ci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ jobs:
3737
run: sudo ifconfig lo0 alias 127.0.0.3
3838

3939
- uses: actions/checkout@v3
40+
41+
- name: Free Disk Space
42+
if: matrix.target.os == 'ubuntu-latest'
43+
run: ./scripts/free-disk-space.sh
4044

4145
- name: Install OpenSSL
4246
if: matrix.target.os == 'windows-latest'

actix-server/CHANGES.md

+44-44
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
# Changes
22

3-
## Unreleased - 2023-xx-xx
3+
## Unreleased
44

55
- Add support for MultiPath TCP (MPTCP) with `MpTcp` enum and `ServerBuilder::mptcp()` method.
66
- Minimum supported Rust version (MSRV) is now 1.65.
77

8-
## 2.2.0 - 2022-12-21
8+
## 2.2.0
99

1010
- Minimum supported Rust version (MSRV) is now 1.59.
1111
- Update `tokio-uring` dependency to `0.4`. [#473]
1212

1313
[#473]: https://github.com/actix/actix-net/pull/473
1414

15-
## 2.1.1 - 2022-03-09
15+
## 2.1.1
1616

1717
- No significant changes since `2.1.0`.
1818

19-
## 2.1.0 - 2022-03-08
19+
## 2.1.0
2020

2121
- Update `tokio-uring` dependency to `0.3`. [#448]
2222
- Logs emitted now use the `tracing` crate with `log` compatibility. [#448]
@@ -25,47 +25,47 @@
2525
[#443]: https://github.com/actix/actix-net/pull/443
2626
[#448]: https://github.com/actix/actix-net/pull/448
2727

28-
## 2.0.0 - 2022-01-19
28+
## 2.0.0
2929

3030
- No significant changes since `2.0.0-rc.4`.
3131

32-
## 2.0.0-rc.4 - 2022-01-12
32+
## 2.0.0-rc.4
3333

3434
- Update `tokio-uring` dependency to `0.2`. [#436]
3535

3636
[#436]: https://github.com/actix/actix-net/pull/436
3737

38-
## 2.0.0-rc.3 - 2021-12-31
38+
## 2.0.0-rc.3
3939

4040
- No significant changes since `2.0.0-rc.2`.
4141

42-
## 2.0.0-rc.2 - 2021-12-27
42+
## 2.0.0-rc.2
4343

4444
- Simplify `TestServer`. [#431]
4545

4646
[#431]: https://github.com/actix/actix-net/pull/431
4747

48-
## 2.0.0-rc.1 - 2021-12-05
48+
## 2.0.0-rc.1
4949

5050
- Hide implementation details of `Server`. [#424]
5151
- `Server` now runs only after awaiting it. [#425]
5252

5353
[#424]: https://github.com/actix/actix-net/pull/424
5454
[#425]: https://github.com/actix/actix-net/pull/425
5555

56-
## 2.0.0-beta.9 - 2021-11-15
56+
## 2.0.0-beta.9
5757

5858
- Restore `Arbiter` support lost in `beta.8`. [#417]
5959

6060
[#417]: https://github.com/actix/actix-net/pull/417
6161

62-
## 2.0.0-beta.8 - 2021-11-05 _(YANKED)_
62+
## 2.0.0-beta.8
6363

6464
- Fix non-unix signal handler. [#410]
6565

6666
[#410]: https://github.com/actix/actix-net/pull/410
6767

68-
## 2.0.0-beta.7 - 2021-11-05 _(YANKED)_
68+
## 2.0.0-beta.7
6969

7070
- Server can be started in regular Tokio runtime. [#408]
7171
- Expose new `Server` type whose `Future` impl resolves when server stops. [#408]
@@ -78,7 +78,7 @@
7878
[#407]: https://github.com/actix/actix-net/pull/407
7979
[#408]: https://github.com/actix/actix-net/pull/408
8080

81-
## 2.0.0-beta.6 - 2021-10-11
81+
## 2.0.0-beta.6
8282

8383
- Add experimental (semver-exempt) `io-uring` feature for enabling async file I/O on linux. [#374]
8484
- Server no long listens to `SIGHUP` signal. Previously, the received was not used but did block subsequent exit signals from working. [#389]
@@ -89,19 +89,19 @@
8989
[#349]: https://github.com/actix/actix-net/pull/349
9090
[#389]: https://github.com/actix/actix-net/pull/389
9191

92-
## 2.0.0-beta.5 - 2021-04-20
92+
## 2.0.0-beta.5
9393

9494
- Server shutdown notifies all workers to exit regardless if shutdown is graceful. This causes all workers to shutdown immediately in force shutdown case. [#333]
9595

9696
[#333]: https://github.com/actix/actix-net/pull/333
9797

98-
## 2.0.0-beta.4 - 2021-04-01
98+
## 2.0.0-beta.4
9999

100100
- Prevent panic when `shutdown_timeout` is very large. [f9262db]
101101

102102
[f9262db]: https://github.com/actix/actix-net/commit/f9262db
103103

104-
## 2.0.0-beta.3 - 2021-02-06
104+
## 2.0.0-beta.3
105105

106106
- Hidden `ServerBuilder::start` method has been removed. Use `ServerBuilder::run`. [#246]
107107
- Add retry for EINTR signal (`io::Interrupted`) in `Accept`'s poll loop. [#264]
@@ -113,13 +113,13 @@
113113
[#265]: https://github.com/actix/actix-net/pull/265
114114
[#273]: https://github.com/actix/actix-net/pull/273
115115

116-
## 2.0.0-beta.2 - 2021-01-03
116+
## 2.0.0-beta.2
117117

118118
- Merge `actix-testing` to `actix-server` as `test_server` mod. [#242]
119119

120120
[#242]: https://github.com/actix/actix-net/pull/242
121121

122-
## 2.0.0-beta.1 - 2020-12-28
122+
## 2.0.0-beta.1
123123

124124
- Added explicit info log message on accept queue pause. [#215]
125125
- Prevent double registration of sockets when back-pressure is resolved. [#223]
@@ -134,127 +134,127 @@
134134
[#223]: https://github.com/actix/actix-net/pull/223
135135
[#239]: https://github.com/actix/actix-net/pull/239
136136

137-
## 1.0.4 - 2020-09-12
137+
## 1.0.4
138138

139139
- Update actix-codec to 0.3.0.
140140
- Workers must be greater than 0. [#167]
141141

142142
[#167]: https://github.com/actix/actix-net/pull/167
143143

144-
## 1.0.3 - 2020-05-19
144+
## 1.0.3
145145

146146
- Replace deprecated `net2` crate with `socket2` [#140]
147147

148148
[#140]: https://github.com/actix/actix-net/pull/140
149149

150-
## 1.0.2 - 2020-02-26
150+
## 1.0.2
151151

152152
- Avoid error by calling `reregister()` on Windows [#103]
153153

154154
[#103]: https://github.com/actix/actix-net/pull/103
155155

156-
## 1.0.1 - 2019-12-29
156+
## 1.0.1
157157

158158
- Rename `.start()` method to `.run()`
159159

160-
## 1.0.0 - 2019-12-11
160+
## 1.0.0
161161

162162
- Use actix-net releases
163163

164-
## 1.0.0-alpha.4 - 2019-12-08
164+
## 1.0.0-alpha.4
165165

166166
- Use actix-service 1.0.0-alpha.4
167167

168-
## 1.0.0-alpha.3 - 2019-12-07
168+
## 1.0.0-alpha.3
169169

170170
- Migrate to tokio 0.2
171171
- Fix compilation on non-unix platforms
172172
- Better handling server configuration
173173

174-
## 1.0.0-alpha.2 - 2019-12-02
174+
## 1.0.0-alpha.2
175175

176176
- Simplify server service (remove actix-server-config)
177177
- Allow to wait on `Server` until server stops
178178

179-
## 0.8.0-alpha.1 - 2019-11-22
179+
## 0.8.0-alpha.1
180180

181181
- Migrate to `std::future`
182182

183-
## 0.7.0 - 2019-10-04
183+
## 0.7.0
184184

185185
- Update `rustls` to 0.16
186186
- Minimum required Rust version upped to 1.37.0
187187

188-
## 0.6.1 - 2019-09-25
188+
## 0.6.1
189189

190190
- Add UDS listening support to `ServerBuilder`
191191

192-
## 0.6.0 - 2019-07-18
192+
## 0.6.0
193193

194194
- Support Unix domain sockets #3
195195

196-
## 0.5.1 - 2019-05-18
196+
## 0.5.1
197197

198198
- ServerBuilder::shutdown_timeout() accepts u64
199199

200-
## 0.5.0 - 2019-05-12
200+
## 0.5.0
201201

202202
- Add `Debug` impl for `SslError`
203203
- Derive debug for `Server` and `ServerCommand`
204204
- Upgrade to actix-service 0.4
205205

206-
## 0.4.3 - 2019-04-16
206+
## 0.4.3
207207

208208
- Re-export `IoStream` trait
209209
- Depend on `ssl` and `rust-tls` features from actix-server-config
210210

211-
## 0.4.2 - 2019-03-30
211+
## 0.4.2
212212

213213
- Fix SIGINT force shutdown
214214

215-
## 0.4.1 - 2019-03-14
215+
## 0.4.1
216216

217217
- `SystemRuntime::on_start()` - allow to run future before server service initialization
218218

219-
## 0.4.0 - 2019-03-12
219+
## 0.4.0
220220

221221
- Use `ServerConfig` for service factory
222222
- Wrap tcp socket to `Io` type
223223
- Upgrade actix-service
224224

225-
## 0.3.1 - 2019-03-04
225+
## 0.3.1
226226

227227
- Add `ServerBuilder::maxconnrate` sets the maximum per-worker number of concurrent connections
228228
- Add helper ssl error `SslError`
229229
- Rename `StreamServiceFactory` to `ServiceFactory`
230230
- Deprecate `StreamServiceFactory`
231231

232-
## 0.3.0 - 2019-03-02
232+
## 0.3.0
233233

234234
- Use new `NewService` trait
235235

236-
## 0.2.1 - 2019-02-09
236+
## 0.2.1
237237

238238
- Drop service response
239239

240-
## 0.2.0 - 2019-02-01
240+
## 0.2.0
241241

242242
- Migrate to actix-service 0.2
243243
- Updated rustls dependency
244244

245-
## 0.1.3 - 2018-12-21
245+
## 0.1.3
246246

247247
- Fix max concurrent connections handling
248248

249-
## 0.1.2 - 2018-12-12
249+
## 0.1.2
250250

251251
- rename ServiceConfig::rt() to ServiceConfig::apply()
252252
- Fix back-pressure for concurrent ssl handshakes
253253

254-
## 0.1.1 - 2018-12-11
254+
## 0.1.1
255255

256256
- Fix signal handling on windows
257257

258-
## 0.1.0 - 2018-12-09
258+
## 0.1.0
259259

260260
- Move server to separate crate

actix-server/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ actix-utils = "3"
2727
futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] }
2828
futures-util = { version = "0.3.17", default-features = false, features = ["alloc"] }
2929
mio = { version = "0.8", features = ["os-poll", "net"] }
30-
num_cpus = "1.13"
3130
socket2 = "0.5"
3231
tokio = { version = "1.23.1", features = ["sync"] }
3332
tracing = { version = "0.1.30", default-features = false, features = ["log"] }

actix-server/src/builder.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{io, time::Duration};
1+
use std::{io, num::NonZeroUsize, time::Duration};
22

33
use actix_rt::net::TcpStream;
44
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
@@ -55,7 +55,7 @@ impl ServerBuilder {
5555
let (cmd_tx, cmd_rx) = unbounded_channel();
5656

5757
ServerBuilder {
58-
threads: num_cpus::get_physical(),
58+
threads: std::thread::available_parallelism().map_or(2, NonZeroUsize::get),
5959
token: 0,
6060
factories: Vec::new(),
6161
sockets: Vec::new(),
@@ -76,6 +76,12 @@ impl ServerBuilder {
7676
/// The default worker count is the number of physical CPU cores available. If your benchmark
7777
/// testing indicates that simultaneous multi-threading is beneficial to your app, you can use
7878
/// the [`num_cpus`] crate to acquire the _logical_ core count instead.
79+
///
80+
/// # Panics
81+
///
82+
/// Panics if `num` is 0.
83+
///
84+
/// [`num_cpus`]: https://docs.rs/num_cpus
7985
pub fn workers(mut self, num: usize) -> Self {
8086
assert_ne!(num, 0, "workers must be greater than 0");
8187
self.threads = num;

actix-server/src/worker.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{
22
future::Future,
33
io, mem,
4+
num::NonZeroUsize,
45
pin::Pin,
56
rc::Rc,
67
sync::{
@@ -249,8 +250,11 @@ pub(crate) struct ServerWorkerConfig {
249250

250251
impl Default for ServerWorkerConfig {
251252
fn default() -> Self {
252-
// 512 is the default max blocking thread count of tokio runtime.
253-
let max_blocking_threads = std::cmp::max(512 / num_cpus::get_physical(), 1);
253+
let parallelism = std::thread::available_parallelism().map_or(2, NonZeroUsize::get);
254+
255+
// 512 is the default max blocking thread count of a Tokio runtime.
256+
let max_blocking_threads = std::cmp::max(512 / parallelism, 1);
257+
254258
Self {
255259
shutdown_timeout: Duration::from_secs(30),
256260
max_blocking_threads,

0 commit comments

Comments
 (0)