Skip to content
Snippets Groups Projects
Verified Commit 14f490fe authored by Alexander Olofsson's avatar Alexander Olofsson
Browse files

api: Switch to defaulting threadsafe: :multithread

parent 3819ec1d
Branches
No related tags found
No related merge requests found
## **Unreleased** ## **Unreleased**
- Adds some multi-thread usage support to the API (create your API/client with `threadsafe: :multithread`) - Adds some multi-thread usage support to the API (create your API/client with `threadsafe: :multithread/true/false`)
The API will currently default to running with multi-threaded requests. In case your application is single-threaded - or never performs requests from multiple threads - then you can set `threadsafe: true/false` to support connection reuse.
- Changes room finding to ignore non-canonical aliases by default - Changes room finding to ignore non-canonical aliases by default
- Improves room alias handling - Improves room alias handling
......
...@@ -39,9 +39,10 @@ module MatrixSdk ...@@ -39,9 +39,10 @@ module MatrixSdk
# @option params [Hash] :global_headers Additional headers to set for all requests # @option params [Hash] :global_headers Additional headers to set for all requests
# @option params [Boolean] :skip_login Should the API skip logging in if the HS URL contains user information # @option params [Boolean] :skip_login Should the API skip logging in if the HS URL contains user information
# @option params [Boolean] :synapse (true) Is the API connecting to a Synapse instance # @option params [Boolean] :synapse (true) Is the API connecting to a Synapse instance
# @option params [Boolean,:multithread] :threadsafe (true/:multithread) Should the connection be threadsafe - or # @option params [Boolean,:multithread] :threadsafe (:multithread) Should the connection be threadsafe/mutexed - or
# even safe for simultaneous multi-thread usage. Will default to +:multithread+ for JRuby, +true+ otherwise. # safe for simultaneous multi-thread usage. Will default to +:multithread+ - a.k.a. per-thread HTTP connections
# @note Setting threadsafe to +:multithread+ currently doesn't support connection re-use # and requests
# @note Using threadsafe +:multithread+ currently doesn't support connection re-use
def initialize(homeserver, **params) def initialize(homeserver, **params)
@homeserver = homeserver @homeserver = homeserver
raise ArgumentError, 'Homeserver URL must be String or URI' unless @homeserver.is_a?(String) || @homeserver.is_a?(URI) raise ArgumentError, 'Homeserver URL must be String or URI' unless @homeserver.is_a?(String) || @homeserver.is_a?(URI)
...@@ -67,7 +68,7 @@ module MatrixSdk ...@@ -67,7 +68,7 @@ module MatrixSdk
@synapse = params.fetch(:synapse, true) @synapse = params.fetch(:synapse, true)
@http = nil @http = nil
self.threadsafe = params.fetch(:threadsafe, RUBY_ENGINE == 'jruby' ? :multithread : true) self.threadsafe = params.fetch(:threadsafe, :multithread)
([params.fetch(:protocols, [:CS])].flatten - protocols).each do |proto| ([params.fetch(:protocols, [:CS])].flatten - protocols).each do |proto|
self.class.include MatrixSdk::Protocols.const_get(proto) self.class.include MatrixSdk::Protocols.const_get(proto)
...@@ -251,6 +252,7 @@ module MatrixSdk ...@@ -251,6 +252,7 @@ module MatrixSdk
# @return [Boolean,:multithread] # @return [Boolean,:multithread]
def threadsafe=(threadsafe) def threadsafe=(threadsafe)
raise ArgumentError, 'Threadsafe must be either a boolean or :multithread' unless [true, false, :multithread].include? threadsafe raise ArgumentError, 'Threadsafe must be either a boolean or :multithread' unless [true, false, :multithread].include? threadsafe
raise ArugmentError, 'JRuby only support :multithread/false for threadsafe' if RUBY_ENGINE == 'jruby' && threadsafe == true
@threadsafe = threadsafe @threadsafe = threadsafe
@http_lock = nil unless threadsafe == true @http_lock = nil unless threadsafe == true
......
...@@ -136,10 +136,11 @@ module MatrixSdk::Protocols::MSC ...@@ -136,10 +136,11 @@ module MatrixSdk::Protocols::MSC
break break
end end
end end
break unless ctx[:run] break unless ctx[:run]
end end
rescue StandardError ensure
@http_lock.unlock if @http_lock.owned? @http_lock.unlock if @http_lock&.owned?
end end
# rubocop:enable Metrics/BlockLength # rubocop:enable Metrics/BlockLength
......
...@@ -202,7 +202,7 @@ class ApiTest < Test::Unit::TestCase ...@@ -202,7 +202,7 @@ class ApiTest < Test::Unit::TestCase
response = Net::HTTPSuccess.new(nil, 200, 'GET') response = Net::HTTPSuccess.new(nil, 200, 'GET')
response.stubs(:body).returns({ user_id: '@alice:example.com' }.to_json) response.stubs(:body).returns({ user_id: '@alice:example.com' }.to_json)
api = MatrixSdk::Api.new 'https://example.com' api = MatrixSdk::Api.new 'https://example.com', threadsafe: false
http = api.send(:http) http = api.send(:http)
http.expects(:request).returns(response) http.expects(:request).returns(response)
...@@ -236,7 +236,7 @@ class ApiTest < Test::Unit::TestCase ...@@ -236,7 +236,7 @@ class ApiTest < Test::Unit::TestCase
assert_equal 5, http.read_timeout assert_equal 5, http.read_timeout
assert_equal OpenSSL::SSL::VERIFY_PEER, http.verify_mode assert_equal OpenSSL::SSL::VERIFY_PEER, http.verify_mode
api = MatrixSdk::Api.new 'https://example.com' api = MatrixSdk::Api.new 'https://example.com', threadsafe: false
api.send(:http).expects(:finish).times(4) api.send(:http).expects(:finish).times(4)
......
...@@ -5,7 +5,7 @@ class ApiTest < Test::Unit::TestCase ...@@ -5,7 +5,7 @@ class ApiTest < Test::Unit::TestCase
@http = mock @http = mock
@http.stubs(:active?).returns(true) @http.stubs(:active?).returns(true)
@api = MatrixSdk::Api.new 'https://example.com', protocols: :CS @api = MatrixSdk::Api.new 'https://example.com', protocols: :CS, threadsafe: false
@api.instance_variable_set :@http, @http @api.instance_variable_set :@http, @http
@api.stubs(:print_http) @api.stubs(:print_http)
......
...@@ -5,7 +5,7 @@ class ApiCSVerificationTest < Test::Unit::TestCase ...@@ -5,7 +5,7 @@ class ApiCSVerificationTest < Test::Unit::TestCase
@http = mock @http = mock
@http.stubs(:active?).returns(true) @http.stubs(:active?).returns(true)
@api = MatrixSdk::Api.new 'https://example.com', protocols: :CS, autoretry: false @api = MatrixSdk::Api.new 'https://example.com', protocols: :CS, autoretry: false, threadsafe: false
@api.instance_variable_set :@http, @http @api.instance_variable_set :@http, @http
@api.stubs(:print_http) @api.stubs(:print_http)
......
...@@ -5,7 +5,7 @@ class ApiSSTest < Test::Unit::TestCase ...@@ -5,7 +5,7 @@ class ApiSSTest < Test::Unit::TestCase
@http = mock @http = mock
@http.stubs(:active?).returns(true) @http.stubs(:active?).returns(true)
@api = MatrixSdk::Api.new 'https://example.com', protocols: :SS @api = MatrixSdk::Api.new 'https://example.com', protocols: :SS, threadsafe: false
@api.instance_variable_set :@http, @http @api.instance_variable_set :@http, @http
@api.stubs(:print_http) @api.stubs(:print_http)
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment