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.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'ext/lwtarantool/conn.c', line 182 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"); _lwt_conn_set_num_option(args, "recv_buf_size", conn, TNT_OPT_RECV_BUF); _lwt_conn_set_num_option(args, "send_buf_size", conn, TNT_OPT_SEND_BUF); _lwt_conn_set_timeval_option(args, "connect_timeout", conn, TNT_OPT_TMOUT_CONNECT); _lwt_conn_set_timeval_option(args, "open_timeout", conn, TNT_OPT_TMOUT_CONNECT); // TODO: How should we process a partial read/write before timeout? //_lwt_conn_set_timeval_option(args, "receive_timeout", conn, TNT_OPT_TMOUT_RECV); //_lwt_conn_set_timeval_option(args, "send_timeout", conn, TNT_OPT_TMOUT_SEND); // handle url option 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); if (tnt_set(conn->tnt, TNT_OPT_URI, url) != 0) rb_raise(rb_eArgError, "invalid url value"); 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.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/lwtarantool/connection.rb', line 29 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.
316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'ext/lwtarantool/conn.c', line 316 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.
75 76 77 78 79 |
# File 'lib/lwtarantool/connection.rb', line 75 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.
58 59 60 61 62 63 64 65 |
# File 'lib/lwtarantool/connection.rb', line 58 def read mutex.synchronize do _read end rescue SystemError disconnect raise end |