Skip to content

Commit c369355

Browse files
authored
RUBY-2495 rework the check_out method (#2671)
* modularize connection pool * further modularize the pool * create condition variable * RUBY-2495 add size condition variable * fix test and add comment * RUBY-2495 fix tests and add test * fix condition variable * attempts to fix signaling * fix neg timeout * fix condition variable, add tests, modify waiting * update driver-evergreen-tools * use ubuntu-20.04 for gh actions * update download-mongodb.sh and put back ubuntu-latest * put back ubuntu-20.04 * put back det * fix condition variable tests * remove failing tests * Update lib/mongo/server/connection_pool.rb * address comments * verify lock is acquired
1 parent 932b06b commit c369355

File tree

9 files changed

+483
-177
lines changed

9 files changed

+483
-177
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ jobs:
1010
env:
1111
CI: true
1212
TESTOPTS: "-v"
13-
runs-on: ubuntu-latest
13+
runs-on: ubuntu-20.04
1414
continue-on-error: true
1515
strategy:
1616
fail-fast: false
1717
matrix:
18-
os: [ ubuntu-latest ]
18+
os: [ ubuntu-20.04 ]
1919
ruby: ["2.5", "2.6", "2.7", "3.0", "3.1"]
2020
mongodb: ["3.6", "4.4", "5.0"]
2121
topology: [replica_set, sharded_cluster]

lib/mongo.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
require 'mongo/bson'
3939
require 'mongo/semaphore'
4040
require 'mongo/distinguishing_semaphore'
41+
require 'mongo/condition_variable'
4142
require 'mongo/options'
4243
require 'mongo/loggable'
4344
require 'mongo/cluster_time'

lib/mongo/condition_variable.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
# encoding: utf-8
3+
4+
# Copyright (C) 2020 MongoDB Inc.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
module Mongo
19+
# This is an implementation of a condition variable.
20+
#
21+
# @api private
22+
class ConditionVariable
23+
extend Forwardable
24+
25+
def initialize(lock = Mutex.new)
26+
@lock = lock
27+
@cv = ::ConditionVariable.new
28+
end
29+
30+
# Waits for the condition variable to be signaled up to timeout seconds.
31+
# If condition variable is not signaled, returns after timeout seconds.
32+
def wait(timeout = nil)
33+
raise_unless_locked!
34+
return false if timeout && timeout < 0
35+
@cv.wait(@lock, timeout)
36+
end
37+
38+
def broadcast
39+
raise_unless_locked!
40+
@cv.broadcast
41+
end
42+
43+
def signal
44+
raise_unless_locked!
45+
@cv.signal
46+
end
47+
48+
def_delegators :@lock, :synchronize
49+
50+
private
51+
52+
def raise_unless_locked!
53+
unless @lock.owned?
54+
raise ArgumentError, "the lock must be owned when calling this method"
55+
end
56+
end
57+
end
58+
end

lib/mongo/distinguishing_semaphore.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module Mongo
2323
class DistinguishingSemaphore
2424
def initialize
2525
@lock = Mutex.new
26-
@cv = ConditionVariable.new
26+
@cv = ::ConditionVariable.new
2727
@queue = []
2828
end
2929

lib/mongo/semaphore.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module Mongo
2323
class Semaphore
2424
def initialize
2525
@lock = Mutex.new
26-
@cv = ConditionVariable.new
26+
@cv = ::ConditionVariable.new
2727
end
2828

2929
# Waits for the semaphore to be signaled up to timeout seconds.

0 commit comments

Comments
 (0)