Skip to content

Introduce decide_action callback#4

Open
kazeburo wants to merge 6 commits intotravail:masterfrom
kazeburo:introduce-decide-action
Open

Introduce decide_action callback#4
kazeburo wants to merge 6 commits intotravail:masterfrom
kazeburo:introduce-decide-action

Conversation

@kazeburo
Copy link

@kazeburo kazeburo commented Jan 6, 2017

decide_action callback can control that parent process create or not create new processes.
With this PR, max-worker can be changed on demand.

@travail travail self-assigned this Jan 6, 2017
Copy link
Owner

@travail travail left a comment

Choose a reason for hiding this comment

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

Please check the following comments.
Remaining concern is that a lambda function in $decide_actoin does not have an interface such as

  • Arguments are ambiguous.
  • It can return any type of value.

Originally this library is from Perl, so I can accept these ambiguities, but it would be great if you resolve them.

lib/Prefork.php Outdated
/**
* @var callable lamda fuction that decide action
*/
public $decide_action = null;
Copy link
Owner

Choose a reason for hiding this comment

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

You can set string, int or whatever to $decide_action in current implementation, which cause a fatal error here.
This property should be private or protected. And then define mutator for it and validate the argument with is_callable or set an empty lambda function at __construct().

lib/Prefork.php Outdated
$action = $currect_workers < $this->max_workers;
if ( isset($this->decide_action) ) {
try {
$action = call_user_func_array($this->decide_action,array($currect_workers, $this->max_workers));
Copy link
Owner

Choose a reason for hiding this comment

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

It's not sure $this->decide_action is callable, which cause a fatal error.

lib/Prefork.php Outdated
printf("Exception. Use default action: %s\n",$e->getMessage());
}
}
if ($action) {
Copy link
Owner

Choose a reason for hiding this comment

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

Do you expect $action is bool to determine the parent process should spawn child processes or not? If so the name $action is ambiguous. It should be $can_spawn, $should_fork or something like that.

lib/Prefork.php Outdated
if (count(array_keys($this->worker_pids)) < $this->max_workers) {
$currect_workers = count(array_keys($this->worker_pids));
$action = $currect_workers < $this->max_workers;
if ( isset($this->decide_action) ) {
Copy link
Owner

Choose a reason for hiding this comment

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

Use is_callable instead.

$cb->($exit_pid, $status);
}
*/
call_user_func_array($cb, array($exit_pid, $status));
Copy link
Owner

Choose a reason for hiding this comment

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

Validate $cb is callable if enable this method. $cb is null by default.

Copy link
Author

Choose a reason for hiding this comment

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

Added validation on 7b94aee

lib/Prefork.php Outdated
*/
public $on_child_reap = null;
/**
* @var callable lamda fuction that decide action
Copy link
Owner

Choose a reason for hiding this comment

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

lamda should be lambda.
If action means a function to determine the parent process should spawn child processes or not, this comment should be so.

Copy link
Owner

@travail travail left a comment

Choose a reason for hiding this comment

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

Please check on this.

? $args['decide_action'] : null;
if ( isset($this->decide_action) && !is_callable($this->decide_action) ){
die("decide_action should be callable\n");
}
Copy link
Owner

@travail travail Jan 31, 2017

Choose a reason for hiding this comment

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

What I wanted to say is:

  • Define setter for $decide_action. e.g. setDecideAction($function)
  • Validate $function above is_callable() in the setter.
  • No need to accept $args['decide_action'] in __construct() like above.
  • Set empty lambda function to $decide_action in __construct() to ensure $decide_action is callable.

Simple usage is like this.

$pp = new Prefork([
    ...,
]);
$pp->setDecideAction(function (...) use(...) { ... });

$pid = null;
if (count(array_keys($this->worker_pids)) < $this->max_workers) {
$currect_workers = count(array_keys($this->worker_pids));
$should_fork = $currect_workers < $this->max_workers;
Copy link
Owner

@travail travail Jan 31, 2017

Choose a reason for hiding this comment

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

You might be able to set default $decide_action in __construct().

// Set default $decide_action in __construct().
$this->decide_action = function($current_workers, $max_workers)) { return $current_workers < $max_workers; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants