承接 hx/interop 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hx/interop

Composer 安装命令:

composer require hx/interop

包简介

Make all the languages talk to each other

README 文档

README

Layers of simple inter-process messaging goodness.

The Golang implementation is the reference for the other implementations.

Protocol

Interop relies on the exchange of messages between processes. Messages are comprised of MIME-style headers, and bodies.

Content-Type: application/json
Content-Length: 14

{"foo":"bar"}

All headers are optional. A double line-break serves as a message terminator in the absense of a Content-Length header.

RPC

In a client/server pairing, a client process can make an RPC request using an ID and a Class. IDs should be unique per session. Clients are responsible for generating IDs. Classes should tell servers how to handle requests.

Interop-Rpc-Class: ping
Interop-Rpc-Id: 1

Are you there?

The server should send a response with the same ID header.

Interop-Rpc-Id: 1

Yes I am!

Servers may send other messages before sending a response (including events and/or responses to other requests), so clients should wait for a message with the same ID.

Servers may send events to clients at any time. An event is simply a message without an ID. Events should have class headers that inform clients how they are to be handled.

Implementation principles

Messages

The base type in each language is a Message, which should have headers and a body.

Headers are collections of MIME headers. They should be used in a case-insensitive way.

Bodies are raw binary, and therefore suitable for transmission of compressed data, video streams, etc.

Readers/Writers

Each language has reader and writer interfaces, which wrap around native IO primitives to allow reading/writing messages directly to/from files (e.g. STDIN/STDOUT), sockets, pipes, buffers, etc.

Connections

A connection is the combination of a reader and a writer, and should represent a process's bidirectional interface; for example, STDIO, a TCP socket, or pipes into a subprocess.

Pipes

A pipe simple allows you to read whatever messages are written to it. Pipes can block or have buffers, depending on implementation.

Closing up

Readers, writers, and connections generally can't be closed directly. Their underlying IO streams should be closed instead.

Pipes, however, do not represent IO primitives, and so can be closed directly.

RPC

RPC servers and clients wrap around connections to provide core RPC (or any other request-response) functionality.

Clients can augment regular messages with the ID and Class headers required for RPC, and wait for servers to send responses to them, matching responses with requests by ID. Clients can also listen for specific classes of event send by servers, running handlers for them.

Servers can be configured to respond to specific classes of messages with different handlers, allowing responses to be seamlessly returned to clients.

Because clients and servers are both "listening" (clients for events, and servers for requests), they have blocking behaviours that are managed differently depending on available tooling. In most cases a process will be able to wait for a client or server to close or error out.

Concurrency/parallelism

Readers and writers use mutexes to ensure they are only reading or writing one message at a time, regardless of how many threads are accessing them. As such, operations involving the movement of messages are generally thread-safe.

Client event handlers and server request handlers are generally processed asynchronously wherever possible, to minimise blocking on IO streams.

Examples

In the below examples, each language implements the same client/server combination.

Client and server processes talk to each other using two FIFOs called a and b. Clients write to a and read from b, and servers write to b and read from a.

Clients send a single RPC request, countdown, to servers, with a single header, ticks, as an integer. Servers respond to countdown by sending one tick event per second back to the client, up to the number in the request's ticks header.

Clients handle the tick event by writing Tick n to their STDOUT. After receiving their final tick, they close FIFO a, which causes the server to exit, which in turn causes the client to exit.

These examples are all runnable from the examples directory.

Go

Server

import (
    "github.com/hx/interop/interop"
    "os"
    "strconv"
    "time"
)

func main() {
    reader, _ := os.OpenFile("a", os.O_RDONLY, 0)
    writer, _ := os.OpenFile("b", os.O_WRONLY|os.O_SYNC, 0)

    server := interop.NewRpcServer(interop.BuildConn(reader, writer))

    server.HandleClassName("countdown", interop.ResponderFunc(func(request interop.Message, _ *interop.MessageBuilder) {
        num, _ := strconv.Atoi(request.GetHeader("ticks"))
        for i := 1; i <= num; i++ {
            time.Sleep(time.Second)
            event := interop.NewRpcMessage("tick")
            event.SetContent(interop.ContentTypeJSON, i)
            server.Send(event)
        }
    }))

    server.Run()
}

Client

import (
    "fmt"
    "github.com/hx/interop/interop"
    "os"
)

func main() {
    writer, _ := os.OpenFile("a", os.O_WRONLY|os.O_SYNC, 0)
    reader, _ := os.OpenFile("b", os.O_RDONLY, 0)

    client := interop.NewRpcClient(interop.BuildConn(reader, writer))

    client.Events.HandleClassName("tick", interop.HandlerFunc(func(event interop.Message) error {
        i := 0
        interop.ContentTypeJSON.DecodeTo(event, &i)
        fmt.Println("Tick", i)
        if i == 5 {
            writer.Close()
        }
        return nil
    }))

    client.Start()

    client.Send(interop.NewRpcMessage("countdown").AddHeader("ticks", "5"))

    client.Wait()
}

Ruby

Server

require 'interop'

reader = File.open('a', 'r')
writer = File.open('b', 'w')

server = Hx::Interop::RPC::Server.new(reader, writer)

server.on 'countdown' do |request|
  request['ticks'].to_i.times do |i|
    sleep 1
    server.send 'tick', Hx::Interop::ContentType::JSON.encode(i + 1)
  end
  nil
end

server.wait

Client

require 'interop'

writer = File.open('a', 'w')
reader = File.open('b', 'r')

client = Hx::Interop::RPC::Client.new(reader, writer)

client.on 'tick' do |event|
  i = event.decode
  puts "Tick #{i}"
  writer.close if i == 5
end

client.call :countdown, ticks: 5

client.wait

PHP

Server

$reader = fopen('a', 'r');
$writer = fopen('b', 'w');

$server = new Hx\Interop\RPC\Server($reader, $writer);

$server->on('countdown', function (Hx\Interop\Message $message) use($server) {
    $num = (int) $message['ticks'];
    for ($i = 1; $i <= $num; $i++) {
        sleep(1);
        $server->send('tick', Hx\Interop\Message::json($i));
    }
});

$server->wait();

Client

$writer = fopen('a', 'w');
$reader = fopen('b', 'r');

$client = new Hx\Interop\RPC\Client($reader, $writer);

$client->on('tick', function (Hx\Interop\Message $message) use ($writer) {
    $num = json_decode($message->body);
    echo "Tick $num\n";
    if ($num === 5) {
        fclose($writer);
    }
});

$client->call('countdown', ['ticks' => 5]);

$client->wait();

JavaScript

TODO

hx/interop 适用场景与选型建议

hx/interop 是一款 基于 Go 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 12 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 hx/interop 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hx/interop 我们能提供哪些服务?
定制开发 / 二次开发

基于 hx/interop 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: Go

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2020-12-10