Class: LWTarantool::Connection

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(args) ⇒ LWTarantool::Connection

Create new connection to Tarantool.

Examples:

LWTarantool::Connection.new(url: 'tcp://127.0.0.1:3301')

Parameters:

  • args (Hash)

    the options to establish connection

Options Hash (args):

  • :url (String)

    The tarantool address

  • :recv_buf_size (Integer)

    Receive buffer size (unknown effect)

  • :send_buf_size (Integer)

    Send buffer size (maximum request size)

Raises:



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'ext/lwtarantool/conn.c', line 138

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");

  // handle recv_buf_size option
  val = rb_hash_aref(args, ID2SYM(rb_intern( "recv_buf_size")));
  if (TYPE(val) != T_NIL) {
    if (TYPE(val) != T_FIXNUM)
      rb_raise(rb_eArgError, "recv_buf_size must be an Integer");

    if (tnt_set(conn->tnt, TNT_OPT_RECV_BUF, rb_fix2uint(val)) != 0)
      rb_raise(rb_eArgError, "invalid recv_buf_size value");
  }

  // handle send_buf_size option
  val = rb_hash_aref(args, ID2SYM(rb_intern( "send_buf_size")));
  if (TYPE(val) != T_NIL) {
    if (TYPE(val) != T_FIXNUM)
      rb_raise(rb_eArgError, "send_buf_size must be an Integer");

    if (tnt_set(conn->tnt, TNT_OPT_SEND_BUF, rb_fix2uint(val)) != 0)
      rb_raise(rb_eArgError, "invalid send_buf_size value");
  }

  // 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.

Examples:

conn.call('box.slab.info', [])

Parameters:

  • func (String)

    the tarantool function for call.

  • args (Array)

    the tarantool function arguments.

Returns:

Raises:



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.

Examples:

conn.connected?

Returns:

  • (Boolean)


274
275
276
277
278
279
280
281
282
283
284
285
# File 'ext/lwtarantool/conn.c', line 274

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;
}

#disconnectObject

Close tarantool connection.

All active requests will be terminated.

Examples:

conn.disconnect


75
76
77
78
79
# File 'lib/lwtarantool/connection.rb', line 75

def disconnect
  mutex.synchronize do
    _disconnect
  end
end

#readLWTarantool::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.

Examples:

conn.call('box.slab.info', [])

Returns:

Raises:



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