Merge pull request 'Let workers finish old candidates.' (#5) from spivee/patience into master

Reviewed-on: #5
Reviewed-by: Craig Everett <craigeverett@qpq.swiss>
Reviewed-by: Ulf Wiger <ulfwiger@qpq.swiss>
This commit is contained in:
Ulf Wiger 2025-05-28 18:27:18 +09:00
commit 2c160b83a1

View File

@ -107,8 +107,10 @@ handle_cast({from_pool, #{via := Connector,
%% We could check whether we have already received the candidate ... %% We could check whether we have already received the candidate ...
%% For now, stop all workers, restart with new candidate %% For now, stop all workers, restart with new candidate
try try
Workers1 = stop_workers(Workers), % Most of the time we don't want to stop the worker. If we do, though, then
{Workers2, Cand1} = assign_nonces(Workers1, Cand), % we need to do it more carefully than this, or memory usage will triple.
% Workers1 = stop_workers(Workers),
{Workers2, Cand1} = assign_nonces(Workers, Cand),
#st{candidate = Cand2} = S1 = maybe_request_nonces(S#st{candidate = Cand1}), #st{candidate = Cand2} = S1 = maybe_request_nonces(S#st{candidate = Cand1}),
NewWorkers = [spawn_worker(W, Cand2) || W <- Workers2], NewWorkers = [spawn_worker(W, Cand2) || W <- Workers2],
{noreply, S1#st{workers = NewWorkers}} {noreply, S1#st{workers = NewWorkers}}
@ -371,7 +373,10 @@ spawn_worker(#worker{pid = undefined, nonce = Nonce, config = Cfg} = W, Cand) ->
init_worker(Data, Nonce, Target, Cfg1, Me) init_worker(Data, Nonce, Target, Cfg1, Me)
end), end),
MRef = erlang:monitor(process, Pid), MRef = erlang:monitor(process, Pid),
W#worker{pid = Pid, mref = MRef, cand = Cand, nonce = Nonce}. W#worker{pid = Pid, mref = MRef, cand = Cand, nonce = Nonce};
spawn_worker(W, _) ->
% Worker already has work. Don't disturb it.
W.
-spec init_worker(binary(), integer(), integer(), tuple(), pid()) -> no_return(). -spec init_worker(binary(), integer(), integer(), tuple(), pid()) -> no_return().
init_worker(Data, Nonce, Target, Config, Parent) -> init_worker(Data, Nonce, Target, Config, Parent) ->