Skip to content

Commit da5cfcd

Browse files
authored
fix: reset a concurrent-worker instance when a fork is detected, but the pooled model is not production ready and is not used yet (#264)
1 parent 594f8be commit da5cfcd

File tree

1 file changed

+19
-5
lines changed
  • lib/redis_client/cluster/concurrent_worker

1 file changed

+19
-5
lines changed

lib/redis_client/cluster/concurrent_worker/pooled.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
# frozen_string_literal: true
22

3+
require 'redis_client/pid_cache'
4+
35
class RedisClient
46
class Cluster
57
module ConcurrentWorker
6-
# This class is just an experimental implementation. There are some bugs for race condition.
8+
# This class is just an experimental implementation.
79
# Ruby VM allocates 1 MB memory as a stack for a thread.
810
# It is a fixed size but we can modify the size with some environment variables.
911
# So it consumes memory 1 MB multiplied a number of workers.
1012
class Pooled
1113
def initialize
12-
@q = Queue.new
13-
size = ::RedisClient::Cluster::ConcurrentWorker::MAX_WORKERS
14-
size = size.positive? ? size : 5
15-
@workers = Array.new(size)
14+
setup
1615
end
1716

1817
def new_group(size:)
1918
raise ArgumentError, "size must be positive: #{size} given" unless size.positive?
2019

20+
reset if @pid != ::RedisClient::PIDCache.pid
2121
ensure_workers if @workers.first.nil?
2222
::RedisClient::Cluster::ConcurrentWorker::Group.new(worker: self, size: size)
2323
end
@@ -31,6 +31,7 @@ def close
3131
@workers.each { |t| t&.exit }
3232
@workers.clear
3333
@q.close
34+
@pid = nil
3435
nil
3536
end
3637

@@ -47,6 +48,19 @@ def spawn_worker
4748
loop { q.pop.exec }
4849
end
4950
end
51+
52+
def setup
53+
@q = Queue.new
54+
size = ::RedisClient::Cluster::ConcurrentWorker::MAX_WORKERS
55+
size = size.positive? ? size : 5
56+
@workers = Array.new(size)
57+
@pid = ::RedisClient::PIDCache.pid
58+
end
59+
60+
def reset
61+
close
62+
setup
63+
end
5064
end
5165
end
5266
end

0 commit comments

Comments
 (0)