shors 0.13.0

Transport layer for cartridge + tarantool-module projects.
Documentation
//! Shors - transport layer for distributed systems built with tarantool-module and cartridge.

pub use tarantool;

use crate::tarantool::tlua;
use crate::tarantool::tlua::LuaError;

mod builtin;
pub mod transport;

pub use builtin::log;
pub use builtin::middleware;
pub use builtin::utils;

pub extern crate utoipa;
pub extern crate utoipa_swagger_ui;

/// Must be called on application start (for example in luaopen_{lib_name} function).
pub fn init_lua_functions(tlua: &tlua::StaticLua) -> Result<(), tlua::LuaError> {
    let call_shard_code = "
        function call_shard(funct, bucket_id, opts, ...)
            local vshard_group = opts.vshard_group
            local router = require('cartridge').service_get('vshard-router').get(vshard_group)

            local res, err
            if opts.balance then
                res, err = router:call(bucket_id, {mode = 'read', balance = true}, funct, { ... }, {timeout = opts.timeout})
            else
                res, err = router:callrw(bucket_id, funct, { ... }, {timeout = opts.timeout})
            end
            if err ~= nil then
                error(err)
            end

            if res == nil then
                res = box.NULL
            end

            return box.tuple.new{res}
        end";
    if let Err(e) = tlua.exec(call_shard_code) {
        return Err(LuaError::ExecutionError(
            format!("{}: {:?}", "call_shard", e).into(),
        ));
    }

    let call_shard_code_async = "
        function call_shard_async(funct, bucket_id, opts, ...)
            local vshard_group = opts.vshard_group
            local router = require('cartridge').service_get('vshard-router').get(vshard_group)
            router:callrw(bucket_id, funct, { ... }, {timeout = opts.timeout, is_async = true}):discard()
            return box.tuple.new{}
        end";
    if let Err(e) = tlua.exec(call_shard_code_async) {
        return Err(LuaError::ExecutionError(
            format!("{}: {:?}", "call_shard_code_async", e).into(),
        ));
    }

    let call_role = "
        function call_role(fn_name, role_name, opts, ...)
            local res, err = require('cartridge.rpc').call(role_name, fn_name, { ... }, {uri=opts.uri, timeout = opts.timeout, leader_only = opts.leader_only})
            if err ~= nil then
                error(err)
            end
            if res == nil then
                res = box.NULL
            end

            return box.tuple.new{res}
        end";
    if let Err(e) = tlua.exec(call_role) {
        return Err(LuaError::ExecutionError(
            format!("{}: {:?}", "call_role", e).into(),
        ));
    }

    let call_rs_code = "
        function call_rs(funct, uuid, opts,  ...)
            local vshard_group = opts.vshard_group
            local router = require('cartridge').service_get('vshard-router').get(vshard_group)

            local res, err
            if opts.prefer_replica then
                res, err = router:routeall()[uuid]:callre(funct, { ... }, { timeout = opts.timeout })
            else
                res, err = router:routeall()[uuid]:callrw(funct, { ... }, { timeout = opts.timeout })
            end
            if err ~= nil then
                error(err)
            end

            if res == nil then
                res = box.NULL
            end

            return box.tuple.new{res}
        end";
    if let Err(e) = tlua.exec(call_rs_code) {
        return Err(LuaError::ExecutionError(
            format!("{}: {:?}", "call_rs", e).into(),
        ));
    }

    Ok(())
}