sbsm 1.3.4 → 1.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.txt +5 -0
- data/lib/sbsm/app.rb +39 -34
- data/lib/sbsm/cgi.rb +0 -1
- data/lib/sbsm/session.rb +85 -75
- data/lib/sbsm/session_store.rb +179 -0
- data/lib/sbsm/state.rb +1 -0
- data/lib/sbsm/validator.rb +4 -3
- data/lib/sbsm/version.rb +1 -1
- data/lib/sbsm/viralstate.rb +1 -1
- data/test/config.ru +11 -0
- data/test/simple_sbsm.rb +17 -15
- data/test/test_application.rb +55 -44
- data/test/test_customized_app.rb +232 -0
- data/test/test_logger.rb +0 -0
- data/test/test_session.rb +94 -61
- data/test/wrk_visit.lua +12 -0
- metadata +9 -7
- data/lib/cgi/drbsession.rb +0 -39
- data/lib/sbsm/drb.rb +0 -22
- data/lib/sbsm/drbserver.rb +0 -167
- data/test/test_drbserver.rb +0 -84
data/lib/sbsm/drb.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# encoding: utf-8
|
3
|
-
#--
|
4
|
-
# DRb::DRbObject -- fix bug respond_to argument -- 22.08.2011 -- mhatakeyama@ywesee.com
|
5
|
-
# DRb::DRbObject -- fix bug in ruby -- 23.09.2005 -- hwyss@ywesee.com
|
6
|
-
#++
|
7
|
-
require 'drb'
|
8
|
-
|
9
|
-
module DRb
|
10
|
-
class DRbObject
|
11
|
-
def respond_to?(msg_id, *args)
|
12
|
-
case msg_id
|
13
|
-
when :_dump
|
14
|
-
true
|
15
|
-
when :marshal_dump
|
16
|
-
false
|
17
|
-
else
|
18
|
-
method_missing(:respond_to?, msg_id)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
data/lib/sbsm/drbserver.rb
DELETED
@@ -1,167 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# encoding: utf-8
|
3
|
-
#--
|
4
|
-
#
|
5
|
-
# State Based Session Management
|
6
|
-
# Copyright (C) 2004 Hannes Wyss
|
7
|
-
#
|
8
|
-
# This library is free software; you can redistribute it and/or
|
9
|
-
# modify it under the terms of the GNU Lesser General Public
|
10
|
-
# License as published by the Free Software Foundation; either
|
11
|
-
# version 2.1 of the License, or (at your option) any later version.
|
12
|
-
#
|
13
|
-
# This library is distributed in the hope that it will be useful,
|
14
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
16
|
-
# Lesser General Public License for more details.
|
17
|
-
#
|
18
|
-
# You should have received a copy of the GNU Lesser General Public
|
19
|
-
# License along with this library; if not, write to the Free Software
|
20
|
-
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
21
|
-
#
|
22
|
-
# ywesee - intellectual capital connected, Winterthurerstrasse 52, CH-8006 Zürich, Switzerland
|
23
|
-
# hwyss@ywesee.com
|
24
|
-
#
|
25
|
-
# DrbServer -- sbsm -- hwyss@ywesee.com
|
26
|
-
#++
|
27
|
-
|
28
|
-
require 'delegate'
|
29
|
-
require 'sbsm/drb'
|
30
|
-
require 'sbsm/session'
|
31
|
-
require 'sbsm/user'
|
32
|
-
require 'thread'
|
33
|
-
require 'digest/md5'
|
34
|
-
require 'sbsm/logger'
|
35
|
-
require 'sbsm/validator'
|
36
|
-
|
37
|
-
module SBSM
|
38
|
-
class DRbServer < SimpleDelegator
|
39
|
-
include DRbUndumped
|
40
|
-
CLEANING_INTERVAL = 30
|
41
|
-
CAP_MAX_THRESHOLD = 120
|
42
|
-
ENABLE_ADMIN = false
|
43
|
-
MAX_SESSIONS = 100
|
44
|
-
RUN_CLEANER = true
|
45
|
-
SESSION = Session
|
46
|
-
UNKNOWN_USER = UnknownUser
|
47
|
-
VALIDATOR = nil
|
48
|
-
attr_reader :cleaner, :updater
|
49
|
-
def initialize(persistence_layer=nil)
|
50
|
-
@sessions = {}
|
51
|
-
@mutex = Mutex.new
|
52
|
-
@cleaner = run_cleaner if(self.class.const_get(:RUN_CLEANER))
|
53
|
-
@admin_threads = ThreadGroup.new
|
54
|
-
@async = ThreadGroup.new
|
55
|
-
@system = persistence_layer
|
56
|
-
super(persistence_layer)
|
57
|
-
end
|
58
|
-
def _admin(src, result, priority=0)
|
59
|
-
raise "admin interface disabled" unless(self::class::ENABLE_ADMIN)
|
60
|
-
t = Thread.new {
|
61
|
-
Thread.current.abort_on_exception = false
|
62
|
-
result << begin
|
63
|
-
response = begin
|
64
|
-
instance_eval(src)
|
65
|
-
rescue NameError => e
|
66
|
-
e
|
67
|
-
end
|
68
|
-
str = response.to_s
|
69
|
-
if(str.length > 200)
|
70
|
-
response.class
|
71
|
-
else
|
72
|
-
str
|
73
|
-
end
|
74
|
-
rescue StandardError => e
|
75
|
-
e.message
|
76
|
-
end.to_s
|
77
|
-
}
|
78
|
-
t[:source] = src
|
79
|
-
t.priority = priority
|
80
|
-
@admin_threads.add(t)
|
81
|
-
t
|
82
|
-
end
|
83
|
-
def async(&block)
|
84
|
-
@async.add(Thread.new(&block))
|
85
|
-
end
|
86
|
-
def cap_max_sessions(now = Time.now)
|
87
|
-
if(@sessions.size > self::class::CAP_MAX_THRESHOLD)
|
88
|
-
SBSM.info "too many sessions! Keeping only #{self::class::MAX_SESSIONS}"
|
89
|
-
sess = nil
|
90
|
-
sorted = @sessions.values.sort
|
91
|
-
sorted[0...(-self::class::MAX_SESSIONS)].each { |sess|
|
92
|
-
sess.__checkout
|
93
|
-
@sessions.delete(sess.key)
|
94
|
-
}
|
95
|
-
if(sess)
|
96
|
-
age = sess.age(now)
|
97
|
-
SBSM.info sprintf("deleted all sessions that had not been accessed for more than %im %is", age / 60, age % 60)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
def clean
|
102
|
-
now = Time.now
|
103
|
-
@sessions.delete_if { |key, s|
|
104
|
-
begin
|
105
|
-
if s.respond_to?(:expired?)
|
106
|
-
if s.expired?(now)
|
107
|
-
s.__checkout
|
108
|
-
true
|
109
|
-
else
|
110
|
-
s.cap_max_states
|
111
|
-
false
|
112
|
-
end
|
113
|
-
else
|
114
|
-
true
|
115
|
-
end
|
116
|
-
rescue
|
117
|
-
true
|
118
|
-
end
|
119
|
-
}
|
120
|
-
#cap_max_sessions(now)
|
121
|
-
end
|
122
|
-
def clear
|
123
|
-
@sessions.each_value { |sess| sess.__checkout }
|
124
|
-
@sessions.clear
|
125
|
-
end
|
126
|
-
def delete_session(key)
|
127
|
-
if(sess = @sessions.delete(key))
|
128
|
-
sess.__checkout
|
129
|
-
end
|
130
|
-
end
|
131
|
-
def reset
|
132
|
-
@mutex.synchronize {
|
133
|
-
@sessions.clear
|
134
|
-
}
|
135
|
-
end
|
136
|
-
def run_cleaner
|
137
|
-
# puts "running cleaner thread"
|
138
|
-
Thread.new {
|
139
|
-
Thread.current.abort_on_exception = true
|
140
|
-
#Thread.current.priority = 1
|
141
|
-
loop {
|
142
|
-
sleep self::class::CLEANING_INTERVAL
|
143
|
-
@mutex.synchronize {
|
144
|
-
clean()
|
145
|
-
}
|
146
|
-
}
|
147
|
-
}
|
148
|
-
end
|
149
|
-
def unknown_user
|
150
|
-
self::class::UNKNOWN_USER.new
|
151
|
-
end
|
152
|
-
def [](key)
|
153
|
-
@mutex.synchronize {
|
154
|
-
unless((s = @sessions[key]) && !s.expired?)
|
155
|
-
args = [key, self]
|
156
|
-
if(klass = self::class::VALIDATOR)
|
157
|
-
args.push(klass.new)
|
158
|
-
end
|
159
|
-
s = @sessions[key] = self::class::SESSION.new(*args.compact)
|
160
|
-
end
|
161
|
-
s.reset()
|
162
|
-
s.touch()
|
163
|
-
s
|
164
|
-
}
|
165
|
-
end
|
166
|
-
end
|
167
|
-
end
|
data/test/test_drbserver.rb
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# encoding: utf-8
|
3
|
-
#
|
4
|
-
# State Based Session Management
|
5
|
-
# Copyright (C) 2004 Hannes Wyss
|
6
|
-
#
|
7
|
-
# This library is free software; you can redistribute it and/or
|
8
|
-
# modify it under the terms of the GNU Lesser General Public
|
9
|
-
# License as published by the Free Software Foundation; either
|
10
|
-
# version 2.1 of the License, or (at your option) any later version.
|
11
|
-
#
|
12
|
-
# This library is distributed in the hope that it will be useful,
|
13
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
15
|
-
# Lesser General Public License for more details.
|
16
|
-
#
|
17
|
-
# You should have received a copy of the GNU Lesser General Public
|
18
|
-
# License along with this library; if not, write to the Free Software
|
19
|
-
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
20
|
-
#
|
21
|
-
# ywesee - intellectual capital connected, Winterthurerstrasse 52, CH-8006 Zürich, Switzerland
|
22
|
-
# hwyss@ywesee.com
|
23
|
-
#
|
24
|
-
# TestDRbServer -- ODDB -- 27.11.2003 -- hwyss@ywesee.com
|
25
|
-
|
26
|
-
|
27
|
-
$: << File.dirname(__FILE__)
|
28
|
-
$: << File.expand_path('../lib', File.dirname(__FILE__))
|
29
|
-
|
30
|
-
require 'minitest/autorun'
|
31
|
-
require 'sbsm/drbserver'
|
32
|
-
|
33
|
-
class TestDRbServer < Minitest::Test
|
34
|
-
class DRbServer < ::SBSM::DRbServer
|
35
|
-
attr_reader :sessions
|
36
|
-
CAP_MAX_THRESHOLD = 3
|
37
|
-
MAX_SESSIONS = 3
|
38
|
-
end
|
39
|
-
def setup
|
40
|
-
@server = DRbServer.new
|
41
|
-
end
|
42
|
-
def test_session
|
43
|
-
ses1 = @server['test1']
|
44
|
-
assert_instance_of(SBSM::Session, ses1)
|
45
|
-
assert_equal({'test1'=>ses1}, @server.sessions)
|
46
|
-
ses2 = @server['test2']
|
47
|
-
assert_instance_of(SBSM::Session, ses2)
|
48
|
-
refute_equal(ses1, ses2)
|
49
|
-
expected = {
|
50
|
-
'test1' => ses1,
|
51
|
-
'test2' => ses2,
|
52
|
-
}
|
53
|
-
assert_equal(expected, @server.sessions)
|
54
|
-
ses3 = @server['test3']
|
55
|
-
assert_instance_of(SBSM::Session, ses3)
|
56
|
-
refute_equal(ses1, ses3)
|
57
|
-
refute_equal(ses2, ses3)
|
58
|
-
expected = {
|
59
|
-
'test1' => ses1,
|
60
|
-
'test2' => ses2,
|
61
|
-
'test3' => ses3,
|
62
|
-
}
|
63
|
-
assert_equal(expected, @server.sessions)
|
64
|
-
ses4 = @server['test4']
|
65
|
-
assert_instance_of(SBSM::Session, ses4)
|
66
|
-
refute_equal(ses1, ses4)
|
67
|
-
refute_equal(ses2, ses4)
|
68
|
-
expected = {
|
69
|
-
'test2' => ses2,
|
70
|
-
'test3' => ses3,
|
71
|
-
'test4' => ses4,
|
72
|
-
}
|
73
|
-
@server.cap_max_sessions
|
74
|
-
assert_equal(expected, @server.sessions)
|
75
|
-
ses2.touch
|
76
|
-
ses5 = @server['test5']
|
77
|
-
assert_instance_of(SBSM::Session, ses5)
|
78
|
-
expected = {
|
79
|
-
'test2' => ses2,
|
80
|
-
'test4' => ses4,
|
81
|
-
'test5' => ses5,
|
82
|
-
}
|
83
|
-
end
|
84
|
-
end
|