Class: SurrealDB::Connections::HTTP
- Defined in:
- lib/surrealdb/connections/http.rb
Overview
HTTP transport for SurrealDB RPC.
Each request is a synchronous POST to /rpc with a CBOR body. Does not support live queries, sessions, or transactions.
Thread Safety
A single HTTP connection is NOT safe for concurrent use from multiple threads. The underlying Net::HTTP instance is not thread-safe and the header state (ns/db/token) is shared. Use a separate Client per thread or wrap calls in your own Mutex.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #close ⇒ Object
- #connect ⇒ Object
-
#initialize(url, **options) ⇒ HTTP
constructor
A new instance of HTTP.
- #send_request(method, params = []) ⇒ Object
Methods inherited from Base
#connected?, #supports_live_queries?
Constructor Details
#initialize(url, **options) ⇒ HTTP
Returns a new instance of HTTP.
20 21 22 23 24 25 26 27 |
# File 'lib/surrealdb/connections/http.rb', line 20 def initialize(url, **) super @timeout = .fetch(:timeout, SurrealDB.configuration.timeout) @http = nil @namespace = nil @database = nil @auth_token = nil end |
Instance Method Details
#close ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/surrealdb/connections/http.rb', line 43 def close return unless @connected @http&.finish if @http&.started? @connected = false @http = nil log(:debug, 'HTTP connection closed') end |
#connect ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/surrealdb/connections/http.rb', line 29 def connect uri = URI.parse(@url) @http = Net::HTTP.new(uri.host, uri.port) @http.use_ssl = (uri.scheme == 'https') @http.open_timeout = @timeout @http.read_timeout = @timeout @http.verify_mode = OpenSSL::SSL::VERIFY_PEER if @http.use_ssl? @http.start @connected = true log(:debug, "HTTP connected to #{@url}") end |
#send_request(method, params = []) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/surrealdb/connections/http.rb', line 52 def send_request(method, params = []) raise ConnectionError, 'not connected' unless @connected _id, encoded = @rpc.encode_request(method, params) track_use(method, params) request = build_request(encoded) response = execute_request(request) decoded = @rpc.decode_response(response.body) result = Protocol::Response.extract_result(decoded) track_auth(method, params, result) result end |