roadrunner-server / roadrunner-plugins

📦 Home for the roadrunner plugins
MIT License
25 stars 9 forks source link

feat(plugin): tcp plugin #95

Closed rustatian closed 2 years ago

rustatian commented 2 years ago

Reason for This PR

Description of Changes

Client library

Configuration

tcp:
  servers:
    tcp_access_point_1:
      addr: tcp://127.0.0.1:7777
      delimiter: "\r\n"
    server2:
      addr: tcp://127.0.0.1:8889
      read_buf_size: 10
    server3:
      addr: tcp://127.0.0.1:8810
      delimiter: "\r\n"
      read_buf_size: 1

Where:

RR Protocol

Protocol used to have bi-directional communication channel between the PHP worker and the RR server. The protocol can be used by any third-party library and has its own client API with the RR. Our reference implementation is: https://github.com/spiral/roadrunner-tcp.

To summarize:
PHP worker sends the following headers:

RR sends:

Worker sample

<?php

require __DIR__ . '/vendor/autoload.php';

use Spiral\RoadRunner\Worker;
use Spiral\RoadRunner\Tcp\TcpWorker;

// Create new RoadRunner worker from global environment
$worker = Worker::create();

$tcpWorker = new TcpWorker($worker);

while ($request = $tcpWorker->waitRequest()) {

    try {
        if ($request->event === TcpWorker::EVENT_CONNECTED) 
            // You can close connection according your restrictions
            if ($request->remoteAddr !== '127.0.0.1') {
                $tcpWorker->close();
                continue;
            }

            // -----------------

            // Or continue read data from server
            // By default, server closes connection if a worker doesn't send CONTINUE response 
            $tcpWorker->read();

            // -----------------

            // Or send response to the TCP connection, for example, to the SMTP client
            $tcpWorker->respond("220 mailamie \r\n");

        } elseif ($request->event === TcpWorker::EVENT_DATA) {

            $body = $request->body;

            // ... handle request from TCP server [tcp_access_point_1]
            if ($request->server === 'tcp_access_point_1') {

                // Send response and close connection
                $tcpWorker->respond('Access denied', true);

            // ... handle request from TCP server [server2] 
            } elseif ($request->server === 'server2') {

                // Send response to the TCP connection and wait for the next request
                $tcpWorker->respond(json_encode([
                    'remote_addr' => $request->remoteAddr,
                    'server' => $request->server,
                    'uuid' => $request->connectionUuid,
                    'body' => $request->body,
                    'event' => $request->event
                ]));
            }

        // Handle closed connection event 
        } elseif ($request->event === TcpWorker::EVENT_CLOSED) {
            // Do something ...

            // You don't need to send response on closed connection
        }

    } catch (\Throwable $e) {
        $tcpWorker->respond("Something went wrong\r\n", true);
        $worker->error((string)$e);
    }
}

License Acceptance

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

PR Checklist

[Author TODO: Meet these criteria.] [Reviewer TODO: Verify that these criteria are met. Request changes if not]

codecov[bot] commented 2 years ago

Codecov Report

Merging #95 (d0882de) into master (e88aac4) will decrease coverage by 0.08%. The diff coverage is 67.97%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #95      +/-   ##
==========================================
- Coverage   67.80%   67.72%   -0.09%     
==========================================
  Files         123      129       +6     
  Lines       10093    10437     +344     
==========================================
+ Hits         6844     7068     +224     
- Misses       2569     2673     +104     
- Partials      680      696      +16     
Impacted Files Coverage Δ
grpc/proxy/proxy.go 60.95% <0.00%> (-1.72%) :arrow_down:
reload/plugin.go 73.78% <0.00%> (ø)
server/plugin.go 64.02% <0.00%> (-0.80%) :arrow_down:
tcp/handler/handler.go 57.43% <57.43%> (ø)
tcp/handler/helpers.go 57.89% <57.89%> (ø)
tcp/config.go 62.50% <62.50%> (ø)
tcp/plugin.go 71.30% <71.30%> (ø)
grpc/parser/parse.go 100.00% <100.00%> (ø)
http/handler/handler.go 84.95% <100.00%> (+0.06%) :arrow_up:
http/middleware/static/plugin.go 71.02% <100.00%> (+1.12%) :arrow_up:
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update e88aac4...d0882de. Read the comment docs.