Giter Club home page Giter Club logo

simple_rpc's Introduction

simple_rpc

Build Status

RPC Server and Client for Crystal. Implements msgpack-rpc protocol. Designed to be reliable and stable (catch every possible protocol/socket errors). It also quite fast: benchmark performs at 160Krps for single server process and single clients process (in pool mode).

Installation

Add this to your application's shard.yml:

dependencies:
  simple_rpc:
    github: kostya/simple_rpc

Usage

Server example

To create RPC server from your class/struct, just include SimpleRpc::Proto, it adds MyRpc::Server class and also expose all public methods to the external rpc calls. Each method should define type for each argument and also return type. Types of arguments should supports MessagePack::Serializable (by default it supported by most common language types, including Unions). Instance of MyRpc created for each rpc call, so you should not use instance variables for between-request interaction.

require "simple_rpc"

struct MyRpc
  include SimpleRpc::Proto

  def sum(x1 : Int32, x2 : Float64) : Float64
    x1 + x2
  end

  record Greeting, rand : Float64, msg : String { include MessagePack::Serializable }

  def greeting(name : String) : Greeting
    Greeting.new(rand, "Hello from Crystal #{name}")
  end
end

puts "Server listen on 9000 port"
MyRpc::Server.new("127.0.0.1", 9000).run

Client example

Client simple method to use is: .request!(return_type, method_name, *args). This call can raise SimpleRpc::Errors. If you not care about return type use can use MessagePack::Any (in example below, you also can use Greeting record instead if you share that declaration). If you dont want to raise on errors you can use similar method request and process result manually.

require "simple_rpc"

client = SimpleRpc::Client.new("127.0.0.1", 9000)

p client.request!(Float64, :sum, 3, 5.5)
# => 8.5
p client.request!(MessagePack::Any, :greeting, "Vasya")
# => {"rand" => 0.7839463879734746, "msg" => "Hello from Crystal Vasya"}

MsgpackRPC is multi-language RPC, so you can call it, for example, from Ruby

# gem install msgpack-rpc
require 'msgpack/rpc'

client = MessagePack::RPC::Client.new('127.0.0.1', 9000)
p client.call(:sum, 3, 5.5)
# => 8.5
p client.call(:greeting, "Vasya")
# => {"rand"=>0.47593728045415334, "msg"=>"Hello from Crystal Vasya"}

Client modes

SimpleRpc::Client can work in multiple modes, you can choose it by argument mode:

  • :connect_per_request Create new connection for every request, after request done close connection. Quite slow (because spend time to create connection), but concurrency unlimited (only by OS). Good for slow requests. Used by default.

  • :pool Create persistent pool of connections. Much faster, but concurrency limited by pool_size (default = 20). Good for millions of very fast requests. Every request have one autoreconnection attempt (because connection in pool can be outdated).

  • :single Single persistent connection. Same as pool of size 1, you should manage concurrency by yourself. Every request have one autoreconnection attempt (because persistent connection can be outdated).

Example of client, which can handle 50 concurrent requests, and can be used in multifiber environment:

client = SimpleRpc::Client.new("127.0.0.1", 9000, mode: :pool, pool_size: 50, pool_timeout: 1.0)

simple_rpc's People

Contributors

kostya avatar

Stargazers

 avatar 克里の小跟班 avatar Roman avatar Billy.Zheng avatar Mike avatar Mugirase Emmanuel avatar Paulo Coghi avatar Pedro M. Silva avatar Math Solid avatar Anton Yordanov avatar Aurel Branzeanu avatar Lucas M. D. avatar Muhammed Yaşar avatar  avatar  avatar  avatar Martin Honermeyer avatar Cesar Marinho avatar D. Bohdan avatar GAURAV avatar Wilson avatar  avatar Arturs Krapans avatar Giorgi Kavrelishvili avatar Gabriel Aires Guedes avatar Volodymyr  avatar Daniel Huffman avatar bruno avatar 绅士喵 avatar Gabriel Cueto avatar Arnaud Berthomier avatar  avatar Medson Oliveira avatar Fabio Akita avatar Paul Geraghty avatar Zhang Kaizhao avatar Alexander ADAM avatar Alexander Kupchenko avatar Den Patin avatar Tsiry Sandratraina avatar Nob avatar unplugandplay avatar Ahmed Mohamed avatar Youssef Idmoussi avatar Cristian Șerb avatar Carlos Aguilera avatar Byungjik Roh avatar Simon George avatar Dave Goddard avatar Aaron Cruz avatar Chris Ostrowski avatar Luis Lavena avatar Tomás Pollak avatar Vlad Zarakovsky avatar Ivan Palamarchuk avatar Tongfei avatar Vlad Faust avatar Simon Micheneau avatar Oguz Bilgic avatar Sergey Fedorov avatar KevinDean avatar Jens L. Nedregård avatar 邓杰 avatar  avatar  avatar

Watchers

 avatar James Cloos avatar Aurel Branzeanu avatar Zhang Kaizhao avatar  avatar

simple_rpc's Issues

Allow to connect the client to a server behind a unix socket

Hello,

I need to connect to a Msgpack RPC server through a UNIX socket, but this shards seems to only support to connect using a host+port, using a TCP transport.

Do you have plans to allow other transports? (and specifically UNIX sockets in this case)
Thanks

msgpack overprotobuf?

What is the benefits for choosing msgpack over protobuf which u assume might be faster?

OpenSSL support

I would like to request OpenSSL support for simple RPC.

I think being able to encrypt the RPC request and response will benefit the project for use over insecure networks. OpenSSL provides authentication, data integrity, and data protection. All of which at a minimal cost of just wrapping the socket created with an OpenSSL socket and context.

Here are the docs if needed:

https://crystal-lang.org/api/1.0.0/OpenSSL.html

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.