Class: LWTarantool::Connection
- Inherits:
-
Object
- Object
- LWTarantool::Connection
- Defined in:
- lib/lwtarantool/connection.rb,
ext/lwtarantool/conn.c,
ext/lwtarantool/conn.c,
ext/lwtarantool/conn.c,
ext/lwtarantool/conn.c
Overview
Class for work with Tarantool connections
Instance Method Summary collapse
-
#call(func, args) ⇒ LWTarantool::Request
Call a function in tarantool.
-
#connected? ⇒ Boolean
Check if connection established.
-
#disconnect ⇒ Object
Close tarantool connection.
-
#initialize(args) ⇒ LWTarantool::Connection
constructor
Create new connection to Tarantool.
-
#read ⇒ LWTarantool::Request
Read a single response from tarantool.
Constructor Details
#initialize(args) ⇒ LWTarantool::Connection
Create new connection to Tarantool.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'ext/lwtarantool/conn.c', line 134 static VALUE lwt_conn_initialize(VALUE self, VALUE args) { lwt_conn_t * conn; Data_Get_Struct(self, lwt_conn_t, conn); VALUE val; val = rb_mutex_new(); rb_iv_set(self, "@mutex", val); if (TYPE(args) != T_HASH) rb_raise(rb_eArgError, "args must be a Hash"); val = rb_hash_aref(args, ID2SYM(rb_intern( "url"))); if (TYPE(val) != T_STRING) rb_raise(rb_eArgError, "url must be a String"); int url_len = RSTRING_LEN(val); char url[url_len+1]; url[url_len] = '\0'; strncpy(url, RSTRING_PTR(val), url_len); tnt_set(conn->tnt, TNT_OPT_URI, url); lwt_conn_connect(self); return Qnil; } |
Instance Method Details
#call(func, args) ⇒ LWTarantool::Request
Call a function in tarantool.
Connection can be one-time reestablished in case of fail.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/lwtarantool/connection.rb', line 28 def call(func, args) mutex.synchronize do _connect unless connected? _call(func, args.to_msgpack) end rescue SystemError attempt ||= 0 attempt += 1 disconnect retry if attempt <= 1 raise end |
#connected? ⇒ Boolean
Check if connection established.
248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'ext/lwtarantool/conn.c', line 248 static VALUE lwt_conn_is_connected(VALUE self) { lwt_conn_t * conn; Data_Get_Struct(self, lwt_conn_t, conn); struct tnt_stream_net *sn = TNT_SNET_CAST(conn->tnt); if (sn->connected) return Qtrue; else return Qfalse; } |
#disconnect ⇒ Object
Close tarantool connection.
All active requests will be terminated.
74 75 76 77 78 |
# File 'lib/lwtarantool/connection.rb', line 74 def disconnect mutex.synchronize do _disconnect end end |
#read ⇒ LWTarantool::Request
Read a single response from tarantool.
Returns request instance and update already exists request object.
All active requests will be terminated in case of connection fail.
57 58 59 60 61 62 63 64 |
# File 'lib/lwtarantool/connection.rb', line 57 def read mutex.synchronize do _read end rescue SystemError disconnect raise end |