@@ -229,38 +229,17 @@ module DB
229229 @idle .first?
230230 end
231231
232- {% if compare_versions(Crystal ::VERSION , " 0.34.0-0" ) > 0 % }
233- private def wait_for_available
234- sync_inc_waiting_resource
235-
236- select
237- when @availability_channel .receive
238- sync_dec_waiting_resource
239- when timeout(@checkout_timeout .seconds)
240- sync_dec_waiting_resource
241- raise DB ::PoolTimeout .new(" Could not check out a connection in #{ @checkout_timeout } seconds" )
242- end
243- end
244- {% else % }
245- private def wait_for_available
246- timeout = TimeoutHelper .new(@checkout_timeout .to_f64)
247- sync_inc_waiting_resource
248-
249- timeout.start
250-
251- index, _ = Channel .select(@availability_channel .receive_select_action, timeout.receive_select_action)
252- case index
253- when 0
254- timeout.cancel
255- sync_dec_waiting_resource
256- when 1
257- sync_dec_waiting_resource
258- raise DB ::PoolTimeout .new(" Could not check out a connection in #{ @checkout_timeout } seconds" )
259- else
260- raise DB ::Error .new
261- end
232+ private def wait_for_available
233+ sync_inc_waiting_resource
234+
235+ select
236+ when @availability_channel .receive
237+ sync_dec_waiting_resource
238+ when timeout(@checkout_timeout .seconds)
239+ sync_dec_waiting_resource
240+ raise DB ::PoolTimeout .new(" Could not check out a connection in #{ @checkout_timeout } seconds" )
262241 end
263- { % end % }
242+ end
264243
265244 private def sync_inc_waiting_resource
266245 sync { @waiting_resource += 1 }
@@ -291,29 +270,5 @@ module DB
291270 @mutex .lock
292271 end
293272 end
294-
295- class TimeoutHelper
296- def initialize (@timeout : Float64 )
297- @abort_timeout = false
298- @timeout_channel = Channel (Nil ).new
299- end
300-
301- def receive_select_action
302- @timeout_channel .receive_select_action
303- end
304-
305- def start
306- spawn do
307- sleep @timeout
308- unless @abort_timeout
309- @timeout_channel .send nil
310- end
311- end
312- end
313-
314- def cancel
315- @abort_timeout = true
316- end
317- end
318273 end
319274end
0 commit comments