Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert workers greater than zero #167

Merged
merged 4 commits into from Aug 18, 2020

Conversation

adrian-wechner
Copy link
Contributor

PR Type

Feature

PR Checklist

Check your PR fulfills the following:

  • Tests for the changes have been added / updated.
  • Documentation comments have been added / updated.
  • A changelog entry has been made for the appropriate packages.
  • Format code with the latest stable rustfmt

Overview

Before setting workers(0) will actually spin 0 threads. With this change, when setting 0 it defaults back to available logical cpus using num_cpus::get(). Use case, is having a configuration system which might request the default value and can do this by passing 0 as the worker parameter.

Closes #166

@robjtede robjtede requested review from a team July 23, 2020 18:00
@codecov
Copy link

codecov bot commented Jul 23, 2020

Codecov Report

Merging #167 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #167   +/-   ##
=======================================
  Coverage   60.44%   60.45%           
=======================================
  Files          73       73           
  Lines        4783     4784    +1     
=======================================
+ Hits         2891     2892    +1     
  Misses       1892     1892           
Impacted Files Coverage Δ
actix-server/src/builder.rs 52.42% <100.00%> (+0.21%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 578a560...6067ed4. Read the comment docs.

@cetra3
Copy link

cetra3 commented Jul 23, 2020

I feel like a better change here would be to panic if it's not above 0 rather than allowing that to be set, rather than this behaviour.

It's a little too "magical" for my liking!

@fakeshadow
Copy link
Contributor

I too feel an assert would be a better way to handle this.

@robjtede
Copy link
Member

robjtede commented Jul 24, 2020

From the original issue:

I use it personally in our configuration system, that by setting 0 for the workers config, that we translate it into the system's cpu count.

Having dabbled in devops, I do think there's value in being able to use an env var set to 0 and allow that to mean "take the CPU count". What do folks think about this as a proposal:

#[non_exhaustive]
pub enum WorkerCount {
    Fixed(usize),
    NumCpus,
}

impl WorkerCount {
    fn count(&self) -> usize {
        match self {
            WorkerCount::Fixed(num) => *num,
            WorkerCount::NumCpus => num_cpus::get(),
        }
    }
}

// maintain abilitiy to just type number of workers
impl From<usize> for WorkerCount {
    fn from(num: usize) -> WorkerCount {
        WorkerCount::Fixed(num)
    }
}

impl HttpServer {
    pub fn workers(self, num: impl Into<WorkerCount>) -> Self {
        let workers: WorkerCount = num.into();
        let workers = workers.count();
        assert!(workers > 0);

        // ....
    }
}

This is akin to the the KeepAlive options for HttpServer which can accept KeepAlive::OS in addition to a numeric fixed keep alive value, and solves the problem of being able to declare in the environment to use num_cpus more easily (no conditional function calls, only conditional variable assignments).

See playground for example:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=bcebe9c88927917a30891053c5f5fff1

Also means we can keep the 0 assertion but provide a way to "reset" after the fact.

@cetra3
Copy link

cetra3 commented Jul 27, 2020

Yep, that will allow you to include either a number or to specify the default.

It would be great though if there wasn't a way to construct the WorkerCount with 0 as the number. I.e, enforce at compile time. I'm not sure how that could be done, maybe an assert on the From impl? But then they could always just manually create the WorkerCount::Fixed

@robjtede
Copy link
Member

NonZeroUsize exists but would still require runtime checks. I think the assert in server initialization is enough.

@robjtede
Copy link
Member

robjtede commented Aug 9, 2020

Want to get a version cut pretty soon. What do you think of this discussion @adrian-wechner ?

@adrian-wechner
Copy link
Contributor Author

Hi there, personally I still think the best thing to do, is to reset to default as it feels to me the most natural thing to do in that moment. But I'd be equally fine with any other solution as any other soluton would require to check by myself to default. Maybe a simple warning to the log is the better solution? This way everybody can still do whatever, nothing breaks, and if debug is enabled at least is getting some retro. What do you think of that?

Copy link
Member

@robjtede robjtede left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is perfectly reasonable first step.

@robjtede robjtede changed the title workers(0) will default to num_cpus assert workers greater than zero Aug 18, 2020
@robjtede robjtede added the server Actix server label Aug 18, 2020
@robjtede robjtede merged commit fecdfcd into actix:master Aug 18, 2020
@adrian-wechner adrian-wechner deleted the workers-0-defaults-to-num-cpus branch August 18, 2020 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
server Actix server
Projects
None yet
Development

Successfully merging this pull request may close these issues.

manually setting workers(0) will actually spin 0 thread
4 participants