All Projects → laruence → Yar

laruence / Yar

Licence: other
Light, concurrent RPC framework for PHP & C

Programming Languages

c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to Yar

Spyne
A transport agnostic sync/async RPC library that focuses on exposing services with a well-defined API using popular protocols.
Stars: ✭ 992 (-27.54%)
Mutual labels:  rpc-framework, json, msgpack
Rpc.py
A fast and powerful RPC framework based on ASGI/WSGI.
Stars: ✭ 98 (-92.84%)
Mutual labels:  rpc-framework, json, msgpack
Srsly
🦉 Modern high-performance serialization utilities for Python (JSON, MessagePack, Pickle)
Stars: ✭ 189 (-86.19%)
Mutual labels:  json, msgpack
Mashumaro
Fast and well tested serialization framework on top of dataclasses
Stars: ✭ 208 (-84.81%)
Mutual labels:  json, msgpack
Json
C++ header-only JSON library
Stars: ✭ 343 (-74.95%)
Mutual labels:  json, msgpack
Xresloader
跨平台Excel导表工具(Excel=>protobuf/msgpack/lua/javascript/json/xml)
Stars: ✭ 161 (-88.24%)
Mutual labels:  json, msgpack
Storagetapper
StorageTapper is a scalable realtime MySQL change data streaming, logical backup and logical replication service
Stars: ✭ 232 (-83.05%)
Mutual labels:  json, msgpack
Facil.io
Your high performance web application C framework
Stars: ✭ 1,393 (+1.75%)
Mutual labels:  concurrency, json
Airframe
Essential Building Blocks for Scala
Stars: ✭ 442 (-67.71%)
Mutual labels:  json, msgpack
Getty
a netty like asynchronous network I/O library based on tcp/udp/websocket; a bidirectional RPC framework based on JSON/Protobuf; a microservice framework based on zookeeper/etcd
Stars: ✭ 532 (-61.14%)
Mutual labels:  rpc-framework, json
Remarshal
Convert between CBOR, JSON, MessagePack, TOML, and YAML
Stars: ✭ 421 (-69.25%)
Mutual labels:  json, msgpack
Json
JSON for Modern C++
Stars: ✭ 27,824 (+1932.43%)
Mutual labels:  json, msgpack
Arq
Fast job queuing and RPC in python with asyncio and redis.
Stars: ✭ 695 (-49.23%)
Mutual labels:  concurrency, msgpack
Job
JOB, make your short-term command as a long-term job. 将命令行规划成任务的工具
Stars: ✭ 98 (-92.84%)
Mutual labels:  concurrency
Rki Covid Api
🦠🇩🇪📈 An API for the spread of covid-19 in Germany. Data from Robert-Koch-Institut.
Stars: ✭ 98 (-92.84%)
Mutual labels:  json
Fast Serialization
FST: fast java serialization drop in-replacement
Stars: ✭ 1,348 (-1.53%)
Mutual labels:  json
Kaizen Openapi Editor
Eclipse Editor for the Swagger-OpenAPI Description Language
Stars: ✭ 97 (-92.91%)
Mutual labels:  json
Hprose Delphi
Hprose is a cross-language RPC. This project is Hprose 2.0 for Delphi and FreePascal
Stars: ✭ 100 (-92.7%)
Mutual labels:  rpc-framework
Parse Google Docs Json
Authenticates with Google API and parse Google Docs to JSON or Markdown
Stars: ✭ 100 (-92.7%)
Mutual labels:  json
Schemer
Schema registry for CSV, TSV, JSON, AVRO and Parquet schema. Supports schema inference and GraphQL API.
Stars: ✭ 97 (-92.91%)
Mutual labels:  json

Yar - Yet Another RPC framework for PHP

Build Status Build status Build Status

Light, concurrent RPC framework for PHP(see also: Yar C framework, Yar Java framework)

Requirement

  • PHP 7.0+ (master branch))
  • PHP 5.2+ (php5 branch)
  • Curl
  • Json
  • Msgpack (Optional)

Introduction

Yar is a RPC framework which aims to provide a simple and easy way to do communication between PHP applications

It has the ability to concurrently call multiple remote services.

Features

  • Fast, Easy, Simple
  • Concurrent RPC calls
  • Multiple data packager supported (php, json, msgpack built-in)
  • Multiple transfer protocols supported (http, https, TCP)
  • Detailed debug informations

Install

Install Yar

Yar is an PECL extension, thus you can simply install it by:

pecl install yar

Compile Yar in Linux

$/path/to/phpize
$./configure --with-php-config=/path/to/php-config/
$make && make install

Available instructions to configure are

--with-curl=DIR
--enable(disable)-msgpack
--enable(disable)-epoll (require Yar 2.1.2)

Install Yar with msgpack

first you should install msgpack-ext

pecl install msgpack

or , you can get the github source here: https://github.com/msgpack/msgpack-php

then:

$phpize
$configure --with-php-config=/path/to/php-config/ --enable-msgpack
$make && make install

Runtime Configure

  • yar.timeout //default 5000 (ms)
  • yar.connect_timeout //default 1000 (ms)
  • yar.packager //default "php", when built with --enable-msgpack then default "msgpack", it should be one of "php", "json", "msgpack"
  • yar.debug //default Off
  • yar.expose_info // default On, whether output the API info for GET requests
  • yar.content_type // default "application/octet-stream"
  • yar.allow_persistent // default Off

NOTE yar.connect_time is a value in milliseconds, and was measured in seconds in 1.2.1 and before.

Constants

  • YAR_VERSION
  • YAR_OPT_PACKAGER
  • YAR_OPT_PERSISTENT
  • YAR_OPT_TIMEOUT
  • YAR_OPT_CONNECT_TIMEOUT
  • YAR_OPT_HEADER // Since 2.0.4
  • YAR_OPT_PROXY //Since 2.2.0

Server

It's very easy to setup a Yar HTTP RPC Server

<?php
class API {
    /**
     * the doc info will be generated automatically into service info page.
     * @params 
     * @return
     */
    public function some_method($parameter, $option = "foo") {
    }

    protected function client_can_not_see() {
    }
}

$service = new Yar_Server(new API());
$service->handle();
?>

Usual RPC calls will be issued as HTTP POST requests. If a HTTP GET request is issued to the uri, the service information (commented section above) will be printed on the page:

yar service info page

Client

It's very simple for a PHP client to call remote RPC:

Synchronous call

<?php
$client = new Yar_Client("http://host/api/");
/* the following setopt is optinal */
$client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, 1000);

$client->SetOpt(YAR_OPT_HEADER, array("hd1: val", "hd2: val"));  //Custom headers, Since 2.0.4

/* call remote service */
$result = $client->some_method("parameter");
?>

Concurrent call

<?php
function callback($retval, $callinfo) {
     var_dump($retval);
}

function error_callback($type, $error, $callinfo) {
    error_log($error);
}

Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback");
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"));   // if the callback is not specificed, 
                                                                               // callback in loop will be used
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", "error_callback", array(YAR_OPT_PACKAGER => "json"));
                                                                               //this server accept json packager
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", "error_callback", array(YAR_OPT_TIMEOUT=>1));
                                                                               //custom timeout 
 
Yar_Concurrent_Client::loop("callback", "error_callback"); //send the requests, 
                                                           //the error_callback is optional
?>

Persistent call

After Yar 2.1.0, if YAR_OPT_PERSISTENT is set to true, then Yar is able to use HTTP keep-alive to speedup repeated calls to a same address, the link will be released at the end of the PHP request lifecycle.

<?php
$client = new Yar_Client("http://host/api/");
$client->SetOpt(YAR_OPT_PERSISTENT, 1);

$result = $client->some_method("parameter");

/* The following calls will speed up due to keep-alive */
$result = $client->some_other_method1("parameter");
$result = $client->some_other_method2("parameter");
$result = $client->some_other_method3("parameter");
?>

Custom hostname resolving

After Yar 2.1.0, if Yar runs on HTTP protocol, YAR_OPT_RESOLVE could be used to define custom hostname resolving.

<?php
$client = new Yar_Client("http://host/api/");

$client->SetOpt(YAR_OPT_RESOLVE, "host:80:127.0.0.1");

/* call goes to 127.0.0.1 */
$result = $client->some_method("parameter");

Use http proxy

After Yar 2.2.1, if Yar runs on HTTP protocol, YAR_OPT_PROXY could be used to define http proxy , such as fidder or charles.

<?php
$client = new Yar_Client("http://host/api/");

$client->SetOpt(YAR_OPT_PROXY,"127.0.0.1:8888"); //http proxy , Since 2.2.0

/* call goes to 127.0.0.1 */
$result = $client->some_method("parameter"); 

Protocols

Yar Header

Since Yar will support multi transfer protocols, so there is a Header struct, I call it Yar Header

#ifdef PHP_WIN32
#pragma pack(push)
#pragma pack(1)
#endif
typedef struct _yar_header {
    uint32_t       id;            // transaction id
    uint16_t       version;       // protocol version
    uint32_t       magic_num;     // default is: 0x80DFEC60
    uint32_t       reserved;
    unsigned char  provider[32];  // reqeust from who
    unsigned char  token[32];     // request token, used for authentication
    uint32_t       body_len;      // request body len
}
#ifndef PHP_WIN32
__attribute__ ((packed))
#endif
yar_header_t;
#ifdef PHP_WIN32
#pragma pack(pop)
#endif

Packager Header

Since Yar also supports multi packager protocol, so there is a char[8] at the begining of body, to identicate which packager the body is packaged by.

Request

When a Client request a remote server, it will send a struct (in PHP):

<?php
array(
   "i" => '', //transaction id
   "m" => '', //the method which being called
   "p" => array(), //parameters
)

Server

When a server response a result, it will send a struct (in PHP):

<?php
array(
   "i" => '',
   "s" => '', //status
   "r" => '', //return value 
   "o" => '', //output 
   "e" => '', //error or exception
)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].