thin-fun_embed 0.0.3 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +0 -1
- data/examples/libevent.rb +21 -0
- data/examples/rack_like.rb +14 -22
- data/lib/thin/fun_daemon.rb +68 -0
- data/lib/thin/fun_embed.rb +15 -0
- data/lib/thin/fun_embed/version.rb +1 -1
- metadata +19 -22
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 554cca8c4ae41336cb638483d6435f7e6b890c39
|
4
|
+
data.tar.gz: 6b247bd9fbc43b05020e5b18f0bcde245c993d2e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f8b61923ba35ee4012c285462a7dc05c46ce5c5c09468a01e72dcccc6aae54ecbc1c6e1de2e01ee8bac0d8386e6a43df1f37cd2f3d8f769f459c5a1001bdd501
|
7
|
+
data.tar.gz: 8fb9978fcf053c339bf15d7aae8f8b99c9ce72ad2b96cd9ec8c7c637feb3e3a5f30ad67217c6780c3e334a091fd3bae0214d73642d6eef2d695a819bbd4fb419
|
data/Gemfile
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "libevent"
|
2
|
+
|
3
|
+
# create event base
|
4
|
+
base = Libevent::Base.new
|
5
|
+
|
6
|
+
# create http server instance
|
7
|
+
http = Libevent::Http.new(base)
|
8
|
+
|
9
|
+
# bind socket
|
10
|
+
http.bind_socket("0.0.0.0", 8080)
|
11
|
+
|
12
|
+
# set handler
|
13
|
+
http.handler do |request|
|
14
|
+
request.send_reply(200, {}, ["Hello World\n"])
|
15
|
+
end
|
16
|
+
|
17
|
+
# catch SIGINT
|
18
|
+
base.trap_signal("INT") { base.exit_loop }
|
19
|
+
|
20
|
+
# start libevent loop
|
21
|
+
base.dispatch
|
data/examples/rack_like.rb
CHANGED
@@ -1,28 +1,20 @@
|
|
1
1
|
# :stopdoc:
|
2
|
-
require 'thin/
|
2
|
+
require 'thin/fun_daemon.rb'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
app = lambda{|env|
|
5
|
+
con = env['async.fun_embed']
|
6
|
+
if env['PATH_INFO'] =~ %r{async\/(\d+)}
|
7
|
+
EM.add_timer($1.to_f / 1000) do
|
8
|
+
con.send_rack_response 200, {'Content-Length'=>6}, ['hello', "\n"]
|
9
|
+
end
|
10
|
+
con.class::AsyncResponse
|
11
|
+
else
|
12
|
+
[200, {'Content-Length'=>6}, ['hello', "\n"]]
|
8
13
|
end
|
9
|
-
|
10
|
-
|
11
|
-
app = proc{|env| [200, {'Content-Length'=>6}, ['hello', "\n"]]}
|
14
|
+
}
|
12
15
|
|
13
16
|
host, port = '0.0.0.0', 8080
|
14
|
-
all_conns = {}
|
15
|
-
trap(:INT) do
|
16
|
-
EM.schedule{
|
17
|
-
all_conns.each{|conn, _| conn.close_after_writting}
|
18
|
-
EM.next_tick{ EM.stop }
|
19
|
-
}
|
20
|
-
end
|
21
17
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
all_conns[conn] = true
|
26
|
-
conn.unbind_callback = all_conns.method(:delete)
|
27
|
-
end
|
28
|
-
end
|
18
|
+
Thin::FunDaemon.trap
|
19
|
+
Thin::FunDaemon.run_rack host, port, app
|
20
|
+
EM.run
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require_relative 'fun_embed'
|
2
|
+
|
3
|
+
module Thin
|
4
|
+
module FunDaemon
|
5
|
+
def self.add_conn(conn)
|
6
|
+
@conns ||= {}
|
7
|
+
@conns[conn] = true
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.del_conn(conn)
|
11
|
+
@conns.delete(conn)
|
12
|
+
if @trap_called && @conns.empty?
|
13
|
+
EM.next_tick{ EM.stop }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.servers
|
18
|
+
@servers ||= {}
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.trap
|
22
|
+
return if @trap_set
|
23
|
+
@trap_set = true
|
24
|
+
::Signal.trap(:INT){ trap_call }
|
25
|
+
::Signal.trap(:TERM){ trap_call }
|
26
|
+
::Signal.trap(:QUIT){ trap_call }
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.trap_call
|
30
|
+
@trap_called = true
|
31
|
+
Thread.new do
|
32
|
+
EM.schedule do
|
33
|
+
servers.each do |serv, _|
|
34
|
+
EM.stop_tcp_server serv
|
35
|
+
end
|
36
|
+
if @conns && !@conns.empty?
|
37
|
+
@conns.each do |conn, _|
|
38
|
+
conn.close_connection_after_writing
|
39
|
+
end
|
40
|
+
else
|
41
|
+
EM.next_tick{ EM.stop }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
::Signal.trap(:INT, 'DEFAULT')
|
46
|
+
::Signal.trap(:TERM, 'DEFAULT')
|
47
|
+
::Signal.trap(:QUIT, 'DEFAULT')
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.run_class(host, port, klass, *args, &block)
|
51
|
+
EM.schedule do
|
52
|
+
serv = EM.start_server host, port, klass, *args do |conn|
|
53
|
+
yield conn
|
54
|
+
conn.unbind_callback = method(:del_conn)
|
55
|
+
add_conn conn
|
56
|
+
end
|
57
|
+
servers[serv] = true
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.run_rack(host, port, app = nil, &blk)
|
62
|
+
app ||= blk
|
63
|
+
run_class host, port, FunRackLike do |conn|
|
64
|
+
conn.app = app
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/lib/thin/fun_embed.rb
CHANGED
@@ -177,4 +177,19 @@ module Thin # :nodoc:
|
|
177
177
|
@unbind_callback && @unbind_callback.call(self)
|
178
178
|
end
|
179
179
|
end
|
180
|
+
|
181
|
+
class FunRackLike < FunEmbed
|
182
|
+
attr_accessor :app
|
183
|
+
ASYNC_CALLBACK = 'async.callback'.freeze
|
184
|
+
FUN_EMBED = 'async.fun_embed'.freeze
|
185
|
+
AsyncResponse = [-1, {}.freeze, [].freeze].freeze
|
186
|
+
def handle_http_request(env)
|
187
|
+
env[ASYNC_CALLBACK] = method(:send_rack_response)
|
188
|
+
env[FUN_EMBED] = self
|
189
|
+
res = app.call(env)
|
190
|
+
if res[0] != -1
|
191
|
+
send_rack_response(*res)
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
180
195
|
end
|
metadata
CHANGED
@@ -1,30 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thin-fun_embed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.5
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Sokolov Yura 'funny-falcon'
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-01-16 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: thin
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '1.4'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '1.4'
|
30
27
|
description: trim of Thin web server for embedding into eventmachined application
|
@@ -34,43 +31,43 @@ executables: []
|
|
34
31
|
extensions: []
|
35
32
|
extra_rdoc_files: []
|
36
33
|
files:
|
37
|
-
-
|
34
|
+
- Gemfile
|
35
|
+
- LICENSE
|
36
|
+
- README.rdoc
|
37
|
+
- Rakefile
|
38
|
+
- examples/config.ru
|
39
|
+
- examples/libevent.rb
|
38
40
|
- examples/rack_like.rb
|
39
|
-
- examples/status_body.rb
|
40
41
|
- examples/rains.conf
|
41
|
-
- examples/
|
42
|
+
- examples/simple_200_ok.rb
|
43
|
+
- examples/status_body.rb
|
44
|
+
- lib/thin/fun_daemon.rb
|
45
|
+
- lib/thin/fun_embed.rb
|
42
46
|
- lib/thin/fun_embed/constants.rb
|
43
47
|
- lib/thin/fun_embed/version.rb
|
44
|
-
- lib/thin/fun_embed.rb
|
45
|
-
- Gemfile
|
46
|
-
- LICENSE
|
47
|
-
- Rakefile
|
48
|
-
- README.rdoc
|
49
48
|
- thin-fun_embed.gemspec
|
50
49
|
homepage: https://github.com/funny-falcon/thin-fun_embed
|
51
50
|
licenses: []
|
51
|
+
metadata: {}
|
52
52
|
post_install_message:
|
53
53
|
rdoc_options: []
|
54
54
|
require_paths:
|
55
55
|
- lib
|
56
56
|
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
57
|
requirements:
|
59
|
-
- -
|
58
|
+
- - ">="
|
60
59
|
- !ruby/object:Gem::Version
|
61
60
|
version: '0'
|
62
61
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
-
none: false
|
64
62
|
requirements:
|
65
|
-
- -
|
63
|
+
- - ">="
|
66
64
|
- !ruby/object:Gem::Version
|
67
65
|
version: '0'
|
68
66
|
requirements: []
|
69
67
|
rubyforge_project:
|
70
|
-
rubygems_version:
|
68
|
+
rubygems_version: 2.4.5
|
71
69
|
signing_key:
|
72
|
-
specification_version:
|
70
|
+
specification_version: 4
|
73
71
|
summary: Subclass of EM::Connection which uses thin internals to do http request handling.
|
74
72
|
It is intentionally not Rack server, but could be used to build some.
|
75
73
|
test_files: []
|
76
|
-
has_rdoc:
|