dat-tcp 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/dat-tcp.gemspec +2 -0
- data/lib/dat-tcp.rb +17 -6
- data/lib/dat-tcp/version.rb +1 -1
- data/lib/dat-tcp/worker_pool.rb +22 -11
- metadata +22 -7
data/dat-tcp.gemspec
CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
19
|
gem.require_paths = ["lib"]
|
20
20
|
|
21
|
+
gem.add_dependency("SystemTimer", ["~> 1.2"])
|
22
|
+
|
21
23
|
gem.add_development_dependency('assert', ['~> 1.0'])
|
22
24
|
gem.add_development_dependency('assert-mocha', ['~> 1.0'])
|
23
25
|
end
|
data/lib/dat-tcp.rb
CHANGED
@@ -16,14 +16,17 @@ module DatTCP
|
|
16
16
|
|
17
17
|
def initialize(config = nil)
|
18
18
|
config = OpenStruct.new(config || {})
|
19
|
-
@backlog_size
|
20
|
-
@debug
|
21
|
-
@min_workers
|
22
|
-
@max_workers
|
23
|
-
@ready_timeout
|
19
|
+
@backlog_size = config.backlog_size || 1024
|
20
|
+
@debug = config.debug || false
|
21
|
+
@min_workers = config.min_workers || 2
|
22
|
+
@max_workers = config.max_workers || 4
|
23
|
+
@ready_timeout = config.ready_timeout || 1
|
24
|
+
@shutdown_timeout = config.shutdown_timeout || 15
|
24
25
|
|
25
26
|
@logger = DatTCP::Logger.new(@debug)
|
26
27
|
|
28
|
+
check_configuration
|
29
|
+
|
27
30
|
@tcp_server = nil
|
28
31
|
@work_loop_thread = nil
|
29
32
|
@worker_pool = nil
|
@@ -193,7 +196,7 @@ module DatTCP
|
|
193
196
|
|
194
197
|
def shutdown_worker_pool
|
195
198
|
self.logger.info "Shutting down worker pool, letting it finish..."
|
196
|
-
@worker_pool.shutdown
|
199
|
+
@worker_pool.shutdown(@shutdown_timeout)
|
197
200
|
end
|
198
201
|
|
199
202
|
def close_connection
|
@@ -209,6 +212,14 @@ module DatTCP
|
|
209
212
|
@work_loop_thread.join if @work_loop_thread
|
210
213
|
end
|
211
214
|
|
215
|
+
def check_configuration
|
216
|
+
if @min_workers > @max_workers
|
217
|
+
self.logger.warn "The minimum number of workers (#{@min_workers}) " \
|
218
|
+
"is greater than " \
|
219
|
+
"the maximum number of workers (#{@max_workers})."
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
212
223
|
def run_hook(method, *args)
|
213
224
|
self.send(method, *args)
|
214
225
|
end
|
data/lib/dat-tcp/version.rb
CHANGED
data/lib/dat-tcp/worker_pool.rb
CHANGED
@@ -1,16 +1,20 @@
|
|
1
|
+
require 'system_timer'
|
1
2
|
require 'thread'
|
2
3
|
|
3
4
|
require 'dat-tcp/logger'
|
4
5
|
|
5
6
|
module DatTCP
|
6
7
|
|
8
|
+
TimeoutError = Class.new(RuntimeError)
|
9
|
+
|
7
10
|
class WorkerPool
|
8
11
|
attr_reader :logger, :spawned
|
9
12
|
|
10
13
|
def initialize(min = 0, max = 1, debug = false, &serve_proc)
|
11
14
|
@min_workers = min
|
12
15
|
@max_workers = max
|
13
|
-
@
|
16
|
+
@debug = debug
|
17
|
+
@logger = DatTCP::Logger.new(@debug)
|
14
18
|
@serve_proc = serve_proc
|
15
19
|
|
16
20
|
@queue = DatTCP::Queue.new
|
@@ -48,16 +52,23 @@ module DatTCP
|
|
48
52
|
# down. If a worker is processing a connection, then it will be joined and
|
49
53
|
# allowed to finish.
|
50
54
|
# **NOTE** Any connections that are on the queue are not served.
|
51
|
-
def shutdown
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
55
|
+
def shutdown(timeout)
|
56
|
+
begin
|
57
|
+
SystemTimer.timeout(timeout, DatTCP::TimeoutError) do
|
58
|
+
@workers.each(&:shutdown)
|
59
|
+
@queue.shutdown
|
60
|
+
|
61
|
+
# use this pattern instead of `each` -- we don't want to call `join` on
|
62
|
+
# every worker (especially if they are shutting down on their own), we
|
63
|
+
# just want to make sure that any who haven't had a chance to finish
|
64
|
+
# get to (this is safe, otherwise you might get a dead thread in the
|
65
|
+
# `each`).
|
66
|
+
@workers.first.join until @workers.empty?
|
67
|
+
end
|
68
|
+
rescue DatTCP::TimeoutError => exception
|
69
|
+
exception.message.replace "Timed out shutting down the server"
|
70
|
+
@debug ? raise(exception) : self.logger.error(exception.message)
|
71
|
+
end
|
61
72
|
end
|
62
73
|
|
63
74
|
# public, because workers need to call it for themselves
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dat-tcp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Collin Redding
|
@@ -16,11 +16,26 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2013-02-
|
19
|
+
date: 2013-02-19 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 11
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 2
|
32
|
+
version: "1.2"
|
33
|
+
requirement: *id001
|
34
|
+
name: SystemTimer
|
35
|
+
type: :runtime
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
24
39
|
none: false
|
25
40
|
requirements:
|
26
41
|
- - ~>
|
@@ -30,12 +45,12 @@ dependencies:
|
|
30
45
|
- 1
|
31
46
|
- 0
|
32
47
|
version: "1.0"
|
33
|
-
requirement: *
|
48
|
+
requirement: *id002
|
34
49
|
name: assert
|
35
50
|
type: :development
|
36
51
|
- !ruby/object:Gem::Dependency
|
37
52
|
prerelease: false
|
38
|
-
version_requirements: &
|
53
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
39
54
|
none: false
|
40
55
|
requirements:
|
41
56
|
- - ~>
|
@@ -45,7 +60,7 @@ dependencies:
|
|
45
60
|
- 1
|
46
61
|
- 0
|
47
62
|
version: "1.0"
|
48
|
-
requirement: *
|
63
|
+
requirement: *id003
|
49
64
|
name: assert-mocha
|
50
65
|
type: :development
|
51
66
|
description: A generic threaded TCP server API. It is designed for use as a base for application servers.
|