Skip to content

Client Socket Pool Repository

Sudheer edited this page Mar 11, 2023 · 15 revisions

A set of functions to manage connections in a connection pool. It is a repository of connection pools,
{ (repos_name, pool_repos) }

where pool_repos is a collection of connections and associated functions to manage the collection
{ (name, [ss_ptr]) }

One of the special features of a pool in the repository is that it is possible to share the same connection by multiple threads.

Synopsis

Initialization of the repository

local ffi = require('ffi');

local repos_loader = package.loadlib("libevpoolrepos.so","luaopen_libevpoolrepos");
local repos_lib = repos_loader();

ffi.cdef[[
void * pin_loaded_so(const char *);
]]
local lib = ffi.C.pin_loaded_so("libevpoolrepos.so");

The repository is maintained per instance of evlua/evluaserver and will not get reinitialized if already initialized once (ffi is the foreign function interface to lua) The function pin_loaded_so ensures that the loaded dll remains in the process and will not get removed once the dlclose function is called, this is necessary to retain some of the static variables initialized in libevpoolrepos.so.

Creating new pool

local connection_pool = repos_lib.new(poolname);

Operations on the connection pool

connection_pool.add_to_pool(connection_pool, name, ss_ptr);

Adds a given connection handle (ss_ptr) to the connection pool of name "name".

Parameters:
    connection_pool: handle to connection pool repository
    name: Name of the connection pool
    ss_ptr : streamsocket
Return:
    none

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

connection_pool.get_from_pool(connection_pool, name);

Removes the connection from the pool and returns the handle, nil if there is no entry

Parameters:
    connection_pool: handle to connection pool repository
    name: Name of the connection pool
Return:
    ss_ptr: streamsocket or nil

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

connection_pool.share_from_pool(connection_pool, name);

Fetches a connection from the pool if the pool is marked sharable

Parameters:
    connection_pool: handle to connection pool repository
    name: Name of the connection pool
Return:
    ss_ptr: streamsocket or nil

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall