bayserver-core 2.3.0 → 2.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f41f9fb4f873cb31dc74958aa960ea25631f97b3c247ec19e79610e7c7401891
4
- data.tar.gz: 9399102f0b30b1ad6dfe92439707ec97e6b678593855b5a3c0d933253f1d0744
3
+ metadata.gz: 2f0a7628ec3be8f1d727392730648c785b8018968fb343dc20434549ae60abae
4
+ data.tar.gz: 0ef73388d47ef99c304a1b0aaaa951eec6d25222a5fbb0bd71e7f6aeb37edebd
5
5
  SHA512:
6
- metadata.gz: 5eaa95c017ca683078c214ba13700b3b99735a4883a74433b1a9654342930d2d5e297d7ab9fdcac3291da1ca51629e17315a88cd4a89b34124e5b76a3e8da05e
7
- data.tar.gz: dc9635c29598928060361e0ec733c3af899bc211486256b806f0d7ca00b38a6d4f73982d16a94624db3463ed23319105d59dbe03f2f4100f732b1506be55219d
6
+ metadata.gz: cd9122b0cfaf0eae17477d39ec7fd9e323a7944210222a6182861a91a11087da69560d4e70bca2f25c14f76804af0e57ffbf09c3c5fd2f5337b80351e98a74d6
7
+ data.tar.gz: 32b56a60249f626736a1102681cabbc1dec4f7dca68ee8e156c5a42930a04b38c69ca77aeef8eedc979d3ca419b5371ccb12ee1d758675c1af1b3facda28f439
@@ -37,7 +37,7 @@ module Baykit
37
37
  when GrandAgent::CMD_MEM_USAGE
38
38
  @agent.print_usage()
39
39
  when GrandAgent::CMD_SHUTDOWN
40
- @agent.shutdown()
40
+ @agent.req_shutdown()
41
41
  @aborted = true
42
42
  when GrandAgent::CMD_ABORT
43
43
  IOUtil.write_int32(@communication_channel, GrandAgent::CMD_OK)
@@ -125,97 +125,90 @@ module Baykit
125
125
  busy = true
126
126
  begin
127
127
  while not @aborted
128
- begin
129
- count = -1
130
-
131
- if @accept_handler
132
- test_busy = @accept_handler.ch_count >= @max_inbound_ships
133
- if test_busy != busy
134
- busy = test_busy
135
- if busy
136
- @accept_handler.on_busy()
137
- else
138
- @accept_handler.on_free()
139
- end
128
+
129
+ count = -1
130
+
131
+ if @accept_handler
132
+ test_busy = @accept_handler.ch_count >= @max_inbound_ships
133
+ if test_busy != busy
134
+ busy = test_busy
135
+ if busy
136
+ @accept_handler.on_busy()
137
+ else
138
+ @accept_handler.on_free()
140
139
  end
141
140
  end
141
+ end
142
142
 
143
- if !busy && @selector.count() == 2
144
- # agent finished
145
- BayLog.debug("%s Selector has no key", self)
146
- break
147
- end
143
+ if !busy && @selector.count() == 2
144
+ # agent finished
145
+ BayLog.debug("%s Selector has no key", self)
146
+ break
147
+ end
148
148
 
149
- if !@spin_handler.empty?
150
- timeout = 0
151
- else
152
- timeout = @select_timeout_sec
153
- end
149
+ if !@spin_handler.empty?
150
+ timeout = 0
151
+ else
152
+ timeout = @select_timeout_sec
153
+ end
154
154
 
155
- #@BayServer.debug("Selecting... read=" + read_list.to_s)
156
- selected_map = @selector.select(timeout)
157
- #BayLog.debug("%s selected: %s", self, selected_map)
155
+ if @aborted
156
+ BayLog.info("%s aborted by another thread", self)
157
+ break
158
+ end
159
+ #@BayServer.debug("Selecting... read=" + read_list.to_s)
160
+ selected_map = @selector.select(timeout)
161
+ #BayLog.debug("%s selected: %s", self, selected_map)
158
162
 
159
- processed = @non_blocking_handler.register_channel_ops() > 0
163
+ processed = @non_blocking_handler.register_channel_ops() > 0
160
164
 
161
- if selected_map.length == 0
162
- # No channel is selected
163
- processed |= @spin_handler.process_data()
164
- end
165
+ if @aborted
166
+ BayLog.info("%s aborted by another thread", self)
167
+ break
168
+ end
165
169
 
166
- selected_map.keys().each do |ch|
167
- if ch == @select_wakeup_pipe[0]
168
- # Waked up by ask_to_*
169
- on_waked_up(ch)
170
- elsif ch == @command_receiver.communication_channel
171
- @command_receiver.on_pipe_readable()
172
- elsif @accept_handler && @accept_handler.server_socket?(ch)
173
- @accept_handler.on_acceptable(ch)
174
- else
175
- @non_blocking_handler.handle_channel(ch, selected_map[ch])
176
- end
177
- processed = true
178
- end
170
+ if selected_map.length == 0
171
+ # No channel is selected
172
+ processed |= @spin_handler.process_data()
173
+ end
179
174
 
180
- if not processed
181
- # timeout check
182
- @timer_handlers.each do |h|
183
- h.on_timer()
184
- end
175
+ selected_map.keys().each do |ch|
176
+ if ch == @select_wakeup_pipe[0]
177
+ # Waked up by ask_to_*
178
+ on_waked_up(ch)
179
+ elsif ch == @command_receiver.communication_channel
180
+ @command_receiver.on_pipe_readable()
181
+ elsif @accept_handler && @accept_handler.server_socket?(ch)
182
+ @accept_handler.on_acceptable(ch)
183
+ else
184
+ @non_blocking_handler.handle_channel(ch, selected_map[ch])
185
185
  end
186
+ processed = true
187
+ end
186
188
 
187
- rescue => e
188
- raise e
189
+ if not processed
190
+ # timeout check
191
+ @timer_handlers.each do |h|
192
+ h.on_timer()
193
+ end
189
194
  end
195
+
190
196
  end # while
191
197
 
192
198
  rescue => e
193
- BayLog.error_e(e)
194
- raise e
199
+ BayLog.fatal(e)
195
200
  ensure
196
201
  BayLog.info("%s end", self)
197
- abort_agent(nil, 0)
202
+ shutdown()
198
203
  end
199
204
  end
200
205
 
201
206
  def shutdown()
207
+ BayLog.info("%s shutdown", self)
208
+
202
209
  if @accept_handler != nil
203
210
  @accept_handler.shutdown()
204
211
  end
205
- @aborted = true
206
- wakeup()
207
- end
208
-
209
- def abort_agent(err = nil, status = 1)
210
- BayLog.info("%s abort aborted=%s", self, @aborted)
211
- if @aborted
212
- return
213
- end
214
-
215
- if err
216
- BayLog.fatal("%s abort", self)
217
- BayLog.fatal_e(err)
218
- end
219
212
 
220
213
  @command_receiver.end()
221
214
  GrandAgent.listeners.each do |lis|
@@ -223,14 +216,20 @@ module Baykit
223
216
  end
224
217
 
225
218
  GrandAgent.agents.delete(@agent_id)
219
+ clean()
226
220
 
227
- @aborted = true
228
221
  if BayServer.harbor.multi_core
229
222
  exit(1)
230
- else
231
- clean()
232
223
  end
224
+ end
233
225
 
226
+ def abort_agent()
227
+ BayLog.info("%s abort", self)
228
+ end
229
+
230
+ def req_shutdown()
231
+ @aborted = true
232
+ wakeup()
234
233
  end
235
234
 
236
235
  def reload_cert()
@@ -83,6 +83,10 @@ module Baykit
83
83
  end
84
84
  end
85
85
 
86
+ def on_timer()
87
+
88
+ end
89
+
86
90
  def post(data, adr, tag)
87
91
  @lock.synchronize do
88
92
  empty = @write_queue.empty?
@@ -66,17 +66,17 @@ module Baykit
66
66
  tur = pair[1]
67
67
  tur.check_tour_id pair[0]
68
68
 
69
- if !tur.res.header_sent
70
- BayLog.error("%s Send ServiceUnavailable: tur=%s", self, tur);
71
- tur.res.send_error(Tour::TOUR_ID_NOCHECK, HttpStatus::SERVICE_UNAVAILABLE, "Server closed on reading headers")
72
- else
73
- # NOT treat EOF as Error
74
- BayLog.debug("%s EOF is not an error: tur=%s", self, tur);
75
- begin
69
+ begin
70
+ if !tur.res.header_sent
71
+ BayLog.debug("%s Send ServiceUnavailable: tur=%s", self, tur)
72
+ tur.res.send_error(Tour::TOUR_ID_NOCHECK, HttpStatus::SERVICE_UNAVAILABLE, "Server closed on reading headers")
73
+ else
74
+ # NOT treat EOF as Error
75
+ BayLog.debug("%s EOF is not an error: tur=%s", self, tur)
76
76
  tur.res.end_content(Tour::TOUR_ID_NOCHECK)
77
- rescue IOError => e
78
- BayLog::debug_e(e, "%s end content error: tur=%s", self, tur);
79
77
  end
78
+ rescue IOError => e
79
+ BayLog::debug_e(e)
80
80
  end
81
81
  end
82
82
 
@@ -8,6 +8,7 @@ module Baykit
8
8
  # abstract method
9
9
  #
10
10
  # depart()
11
+ # on_timer()
11
12
  #
12
13
 
13
14
  class << self
@@ -63,15 +63,30 @@ module Baykit
63
63
 
64
64
  def terminate()
65
65
  @agent.remove_timer_handler(self)
66
+ @exe.shutdown
66
67
  end
67
68
 
68
69
  def submit(txi)
69
- @lock.synchronize do
70
- @running_taxis << txi
71
- end
72
- @exe.submit(txi)
73
- @lock.synchronize do
74
- @running_taxis.delete(txi)
70
+ @exe.submit() do
71
+ if @agent.aborted
72
+ BayLog.error("Agent is aborted")
73
+ return
74
+ end
75
+
76
+ @lock.synchronize do
77
+ @running_taxis << txi
78
+ end
79
+
80
+ begin
81
+ txi.run()
82
+ rescue => e
83
+ BayLog.fatal_e(e)
84
+ @agent.req_shutdown
85
+ ensure
86
+ @lock.synchronize do
87
+ @running_taxis.delete(txi)
88
+ end
89
+ end
75
90
  end
76
91
  end
77
92
 
@@ -24,6 +24,7 @@ module Baykit
24
24
  attr :running
25
25
  attr :lock
26
26
  attr :agent_id
27
+ attr :start_time
27
28
 
28
29
  def initialize(agt_id, buf_size)
29
30
  super()
@@ -58,28 +59,36 @@ module Baykit
58
59
  ######################################################
59
60
 
60
61
  def depart()
61
- @lock.synchronize do
62
- begin
63
- @buf.clear()
64
- @infile.read(@buf_size, @buf)
65
-
66
- if @buf.length == 0
67
- @data_listener.notify_eof()
68
- close()
69
- return
70
- end
62
+ @start_time = Time.now.to_i
63
+ begin
64
+ @buf.clear()
65
+ @infile.read(@buf_size, @buf)
71
66
 
72
- act = @data_listener.notify_read(@buf, nil)
67
+ if @buf.length == 0
68
+ close()
69
+ return
70
+ end
73
71
 
74
- @running = false
75
- if act == NextSocketAction::CONTINUE
76
- next_run()
77
- end
72
+ act = @data_listener.notify_read(@buf, nil)
78
73
 
79
- rescue Exception => e
80
- BayLog.error_e(e)
81
- close()
74
+ @running = false
75
+ if act == NextSocketAction::CONTINUE
76
+ next_run()
82
77
  end
78
+
79
+ rescue IOError => e
80
+ BayLog.debug_e(e)
81
+ close()
82
+ rescue Exception => e
83
+ close()
84
+ raise e
85
+ end
86
+ end
87
+
88
+ def on_timer()
89
+ duration_sec = Time.now.to_i - @start_time
90
+ if (@data_listener.check_timeout(duration_sec))
91
+ close()
83
92
  end
84
93
  end
85
94
 
@@ -94,9 +103,21 @@ module Baykit
94
103
  end
95
104
 
96
105
  def close()
97
- @ch_valid = false
98
- @infile.close()
99
- @data_listener.notify_close()
106
+ @lock.synchronize do
107
+ if !@ch_valid
108
+ return
109
+ end
110
+
111
+ @ch_valid = false
112
+ @data_listener.notify_eof()
113
+
114
+ begin
115
+ @infile.close()
116
+ rescue IOError => e
117
+ end
118
+
119
+ @data_listener.notify_close()
120
+ end
100
121
  end
101
122
  end
102
123
  end
@@ -57,7 +57,11 @@ module Baykit
57
57
 
58
58
  def notify_eof()
59
59
  BayLog.trace("%s EOF(^o^) %s", self, @file_name)
60
- @tour.res.end_content(@tour_id)
60
+ begin
61
+ @tour.res.end_content(@tour_id)
62
+ rescue IOError => e
63
+ BayLog.debug_e(e)
64
+ end
61
65
  return NextSocketAction::CLOSE
62
66
  end
63
67
 
@@ -123,17 +123,9 @@ module Baykit
123
123
  else
124
124
  begin
125
125
  city.enter(self)
126
- rescue Sink => e
127
- change_state(TOUR_ID_NOCHECK, TourState::ABORTED)
128
- raise e
129
126
  rescue HttpException => e
130
- BayLog.error_e(e)
131
127
  change_state(TOUR_ID_NOCHECK, TourState::ABORTED)
132
128
  raise e
133
- rescue => e
134
- BayLog.error_e(e)
135
- change_state(TOUR_ID_NOCHECK, TourState::ABORTED)
136
- raise HttpException.new(HttpStatus::INTERNAL_SERVER_ERROR, e.message)
137
129
  end
138
130
  end
139
131
  end
@@ -208,26 +208,27 @@ module Baykit
208
208
  end
209
209
 
210
210
  def abort()
211
- if !@tour.preparing?
212
- BayLog.debug("%s cannot abort non-preparing tour", @tour)
213
- return false
214
- end
215
-
216
211
  BayLog.debug("%s abort", @tour)
217
- if @tour.aborted?
218
- raise Sink.new("tour has already aborted")
219
- end
212
+ if @tour.preparing?
213
+ @tour.change_state(Tour::TOUR_ID_NOCHECK, Tour::TourState::ABORTED)
214
+ return true
220
215
 
221
- aborted = true
222
- if @tour.running? && @content_handler != nil
223
- aborted = @content_handler.on_abort(@tour)
224
- end
216
+ elsif @tour.running?
217
+ aborted = true
218
+ if @content_handler != nil
219
+ aborted = @content_handler.on_abort(@tour)
220
+ end
225
221
 
226
- if aborted
227
- @tour.change_state(Tour::TOUR_ID_NOCHECK, Tour::TourState::ABORTED)
222
+ if aborted
223
+ @tour.change_state(Tour::TOUR_ID_NOCHECK, Tour::TourState::ABORTED)
224
+ end
225
+
226
+ return aborted
227
+ else
228
+ BayLog.debug("%s tour is not preparing or not running", @tour)
229
+ return false
228
230
  end
229
231
 
230
- return aborted
231
232
  end
232
233
 
233
234
  def set_content_handler(hnd)
@@ -47,13 +47,11 @@ module Baykit
47
47
  attr :bytes_consumed
48
48
  attr :bytes_limit
49
49
  attr :buffer_size
50
- attr :tour_returned
51
50
 
52
51
  def initialize(tur)
53
52
  @headers = Headers.new()
54
53
  @tour = tur
55
54
  @buffer_size = BayServer.harbor.tour_buffer_size
56
- @tour_returned = false
57
55
  end
58
56
 
59
57
  def init()
@@ -187,6 +185,7 @@ module Baykit
187
185
  raise Sink.new("Response consume listener is null")
188
186
  end
189
187
 
188
+ @bytes_posted += len
190
189
  BayLog.debug("%s posted res content len=%d posted=%d limit=%d consumed=%d",
191
190
  @tour, len, @bytes_posted, @bytes_limit, @bytes_consumed)
192
191
  if @bytes_limit > 0 && @bytes_limit < self.bytes_posted
@@ -211,10 +210,6 @@ module Baykit
211
210
  end
212
211
  end
213
212
 
214
- @bytes_posted += len
215
-
216
- BayLog.debug("%s post res content: len=%d posted=%d limit=%d consumed=%d",
217
- @tour, len, @bytes_posted, @bytes_limit, @bytes_consumed)
218
213
 
219
214
  old_available = @available
220
215
  if !buffer_available()
@@ -248,17 +243,17 @@ module Baykit
248
243
 
249
244
 
250
245
  # Done listener
246
+ tour_returned = false
251
247
  done_lis = Proc.new() do
252
248
  @tour.check_tour_id(chk_tour_id)
253
249
  @tour.ship.return_tour(@tour)
254
- @tour_returned = true
250
+ tour_returned = true
255
251
  end
256
252
 
257
253
  begin
258
254
  if @tour.zombie? || @tour.aborted?
259
255
  # Don't send peer any data. Do nothing
260
256
  BayLog.debug("%s Aborted or zombie tour. do nothing: %s state=%s", self, @tour, @tour.state)
261
- @tour.change_state(Tour::TOUR_ID_NOCHECK, Tour::TourState::ENDED)
262
257
  done_lis.call()
263
258
  else
264
259
  begin
@@ -270,8 +265,10 @@ module Baykit
270
265
  end
271
266
  end
272
267
  ensure
273
- BayLog.debug("%s Tour is returned: %s", self, @tour_returned)
274
- if !@tour_returned
268
+ # If tour is returned, we cannot change its state because
269
+ # it will become uninitialized.
270
+ BayLog.debug("%s tur#%d is returned: %s", self, chk_tour_id, tour_returned)
271
+ if !tour_returned
275
272
  @tour.change_state(Tour::TOUR_ID_NOCHECK, Tour::TourState::ENDED)
276
273
  end
277
274
  end
@@ -326,8 +323,8 @@ module Baykit
326
323
  end
327
324
 
328
325
  if @header_sent
329
- BayLog.warn("Try to send error after response header is sent (Ignore)")
330
- BayLog.warn("%s: status=%d, message=%s", self, status, msg)
326
+ BayLog.debug("Try to send error after response header is sent (Ignore)")
327
+ BayLog.debug("%s: status=%d, message=%s", self, status, msg)
331
328
  if err != nil
332
329
  BayLog.error_e(err);
333
330
  end
@@ -341,7 +338,7 @@ module Baykit
341
338
  begin
342
339
  @tour.ship.send_error(@tour.ship_id, @tour, status, msg, err)
343
340
  rescue IOError => e
344
- BayLog.debug("%s Error on sending end tour", self)
341
+ BayLog.debug("%s Error on sending error", self)
345
342
  @tour.change_state(Tour::TOUR_ID_NOCHECK, Tour::TourState::ABORTED)
346
343
  end
347
344
  @header_sent = true
@@ -20,17 +20,25 @@ module Baykit
20
20
  @name = name
21
21
  end
22
22
 
23
+ def to_s
24
+ @name
25
+ end
26
+
23
27
  def run
24
28
  while true
25
- tsk = @que.deq
26
- BayLog.debug("%s Start task on: %s", tsk, @name)
27
- tsk.run
28
- BayLog.debug("%s End task on: %s", tsk, @name)
29
+ block = @que.deq
30
+ if block == nil
31
+ break
32
+ end
33
+ BayLog.debug("Start task on: %s", @name)
34
+ block.call
35
+ BayLog.debug("End task on: %s", @name)
29
36
  end
30
37
  end
31
38
 
32
- def to_s
33
- @name
39
+ def shutdown
40
+ @que.pop until @que.empty?
41
+ @que.enq(nil)
34
42
  end
35
43
  end
36
44
 
@@ -40,12 +48,14 @@ module Baykit
40
48
  attr :count
41
49
  attr :max_queue_len
42
50
  attr :name
51
+ attr :executors
43
52
 
44
53
  def initialize(name, count)
45
54
  @que = Thread::Queue.new
46
55
  @count = count
47
56
  @max_queue_len = MAX_LEN_PER_EXECUTOR * count
48
57
  @name = name
58
+ @executors = []
49
59
 
50
60
  count.times do |i|
51
61
  started = false
@@ -56,6 +66,7 @@ module Baykit
56
66
  Thread.current.name = th_name
57
67
  e = Executor.new @que, id, th_name
58
68
  e.run
69
+ @executors << e
59
70
  end
60
71
  while !started
61
72
  sleep(0.01)
@@ -63,17 +74,23 @@ module Baykit
63
74
  end
64
75
  end
65
76
 
66
- def to_s()
77
+ def to_s
67
78
  return "ExecutorService[#{@name}]"
68
79
  end
69
80
 
70
81
  # post task
71
- def submit(tsk)
72
- BayLog.debug("%s Submit: task=%s (qlen=%d/%d)", self, tsk, @que.length, @max_queue_len)
82
+ def submit(&block)
83
+ BayLog.debug("%s Submit: (qlen=%d/%d)", self, @que.length, @max_queue_len)
73
84
  if @que.length > @max_queue_len
74
85
  raise IOError("Task queue is full (>_<)")
75
86
  end
76
- @que.enq(tsk)
87
+ @que.enq(block)
88
+ end
89
+
90
+ def shutdown
91
+ @executors.each do |exe|
92
+ exe.shutdown
93
+ end
77
94
  end
78
95
  end
79
96
  end
@@ -1,7 +1,7 @@
1
1
  module Baykit
2
2
  module BayServer
3
3
  class Version
4
- VERSION='2.3.0'
4
+ VERSION='2.3.1'
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bayserver-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michisuke-P
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-15 00:00:00.000000000 Z
11
+ date: 2023-12-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: BayServer is one of the high-speed web servers. It operates as a single-threaded,
14
14
  asynchronous server, which makes it exceptionally fast. It also supports multi-core