goridge

package module
v2.4.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2020 License: MIT Imports: 13 Imported by: 10

README

High-performance PHP-to-Golang IPC bridge

Latest Stable Version GoDoc CI Scrutinizer Code Quality Go Report Card Codecov

PHPClasses Innovation Award

Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package. The library allows you to call Go service methods from PHP with minimal footprint, structures and []byte support.


See https://github.com/spiral/roadrunner - High-performance PHP application server, load-balancer and process manager written in Golang

Features

  • no external dependencies or services, drop-in (64bit PHP version required)
  • low message footprint (17 bytes over any binary payload), binary error detection
  • sockets over TCP or Unix (ext-sockets is required), standard pipes
  • very fast (300k calls per second on Ryzen 1700X over 20 threads)
  • native net/rpc integration, ability to connect to existed application(s)
  • standalone protocol usage
  • structured data transfer using json
  • []byte transfer, including big payloads
  • service, message and transport level error handling
  • hackable
  • works on Windows
  • unix sockets powered (also on Windows)

Installation

$ go get "github.com/spiral/goridge"
$ composer require spiral/goridge

Example

<?php
use Spiral\Goridge;
require "vendor/autoload.php";

$rpc = new Goridge\RPC(new Goridge\SocketRelay("127.0.0.1", 6001));
//or, using factory:
$tcpRPC = Goridge\Relay::create('tcp://127.0.0.1:6001');
$unixRPC = Goridge\Relay::create('unix:///tmp/rpc.sock');
$streamRPC = Goridge\Relay::create('pipes://stdin:stdout');

echo $rpc->call("App.Hi", "Antony");

Factory applies the next format: <protocol>://<arg1>:<arg2>

package main

import (
	"fmt"
	"github.com/spiral/goridge/v2"
	"net"
	"net/rpc"
)

type App struct{}

func (s *App) Hi(name string, r *string) error {
	*r = fmt.Sprintf("Hello, %s!", name)
	return nil
}

func main() {
	ln, err := net.Listen("tcp", ":6001")
	if err != nil {
		panic(err)
	}

	rpc.Register(new(App))

	for {
		conn, err := ln.Accept()
		if err != nil {
			continue
		}
		go rpc.ServeCodec(goridge.NewCodec(conn))
	}
}

Check this libraries in order to find suitable socket manager:

License

The MIT License (MIT). Please see LICENSE for more information.

Documentation

Index

Constants

View Source
const (
	// PayloadEmpty must be set when no data to be sent.
	PayloadEmpty byte = 2

	// PayloadRaw must be set when data binary data.
	PayloadRaw byte = 4

	// PayloadError must be set when data is error string or structure.
	PayloadError byte = 8

	// PayloadControl defines that associated data must be treated as control data.
	PayloadControl byte = 16
)
View Source
const BufferSize = 10485760 // 10 Mb
View Source
const Uint64Size = 8

Uint64Size is standard size of uint on 64 bit platforms Size in bytes of uint64 https://golang.org/ref/spec#Size_and_alignment_guarantees

Variables

This section is empty.

Functions

func MemAvail added in v2.4.3

func MemAvail() uint64

Types

type ClientCodec

type ClientCodec struct {
	// contains filtered or unexported fields
}

ClientCodec is codec for goridge connection.

func NewClientCodec

func NewClientCodec(rwc io.ReadWriteCloser) *ClientCodec

NewClientCodec initiates new server rpc codec over socket connection.

func (*ClientCodec) Close

func (c *ClientCodec) Close() error

Close closes the client connection.

func (*ClientCodec) ReadResponseBody

func (c *ClientCodec) ReadResponseBody(out interface{}) error

ReadResponseBody response from the connection.

func (*ClientCodec) ReadResponseHeader

func (c *ClientCodec) ReadResponseHeader(r *rpc.Response) error

ReadResponseHeader reads response from the connection.

func (*ClientCodec) WriteRequest

func (c *ClientCodec) WriteRequest(r *rpc.Request, body interface{}) error

WriteRequest writes request to the connection. Sequential.

type Codec

type Codec struct {
	// contains filtered or unexported fields
}

Codec represent net/rpc bridge over Goridge socket relay.

func NewCodec

func NewCodec(rwc io.ReadWriteCloser) *Codec

NewCodec initiates new server rpc codec over socket connection.

func NewCodecWithRelay added in v2.3.2

func NewCodecWithRelay(relay Relay) *Codec

NewCodecWithRelay initiates new server rpc codec with a relay of choice.

func (*Codec) Close

func (c *Codec) Close() error

Close underlying socket.

func (*Codec) ReadRequestBody

func (c *Codec) ReadRequestBody(out interface{}) error

ReadRequestBody fetches prefixed body data and automatically unmarshal it as json. RawBody flag will populate []byte lice argument for rpc method.

func (*Codec) ReadRequestHeader

func (c *Codec) ReadRequestHeader(r *rpc.Request) error

ReadRequestHeader receives

func (*Codec) WriteResponse

func (c *Codec) WriteResponse(r *rpc.Response, body interface{}) error

WriteResponse marshals response, byte slice or error to remote party.

type PipeRelay

type PipeRelay struct {
	// contains filtered or unexported fields
}

PipeRelay communicate with underlying process using standard streams (STDIN, STDOUT). Attention, use TCP alternative for Windows as more reliable option. This relay closes automatically with the process.

func NewPipeRelay

func NewPipeRelay(in io.ReadCloser, out io.WriteCloser) *PipeRelay

NewPipeRelay creates new pipe based data relay.

func (*PipeRelay) Close

func (rl *PipeRelay) Close() error

Close the connection. Pipes are closed automatically with the underlying process.

func (*PipeRelay) Receive

func (rl *PipeRelay) Receive() (data []byte, p Prefix, err error)

func (*PipeRelay) Send

func (rl *PipeRelay) Send(data []byte, flags byte) (err error)

Send signed (prefixed) data to underlying process.

type Prefix

type Prefix [17]byte

Prefix is always 17 bytes long and contain meta flags and length of next data package. Receive prefix by converting it into the slice. Prefix duplicates size using reverse bytes order to detect possible transmission errors.

func NewPrefix

func NewPrefix() Prefix

NewPrefix creates new empty prefix with no flags and size.

func (Prefix) Flags

func (p Prefix) Flags() byte

Flags describe transmission behaviour and data data type.

func (Prefix) HasFlag

func (p Prefix) HasFlag(flag byte) bool

HasFlag returns true if prefix has given flag.

func (Prefix) HasPayload

func (p Prefix) HasPayload() bool

HasPayload returns true if data is not empty.

func (Prefix) Size

func (p Prefix) Size() uint64

Size returns following data size in bytes.

func (Prefix) String

func (p Prefix) String() string

String represents prefix as string

func (Prefix) Valid

func (p Prefix) Valid() bool

Valid returns true if prefix is valid.

func (Prefix) WithFlag

func (p Prefix) WithFlag(flag byte) Prefix

WithFlag unites given value with flag byte and returns new instance of prefix.

func (Prefix) WithFlags

func (p Prefix) WithFlags(flags byte) Prefix

WithFlags overwrites all flags and returns new instance of prefix.

func (Prefix) WithSize

func (p Prefix) WithSize(size uint64) Prefix

WithSize returns new prefix with given size.

type Relay

type Relay interface {
	// Send signed (prefixed) data to PHP process.
	Send(data []byte, flags byte) (err error)

	// Receive data from the underlying process and returns associated prefix or error.
	Receive() (data []byte, p Prefix, err error)

	// Close the connection.
	Close() error
}

Relay provide IPC over signed payloads.

type SocketRelay

type SocketRelay struct {
	// contains filtered or unexported fields
}

SocketRelay communicates with underlying process using sockets (TPC or Unix).

func NewSocketRelay

func NewSocketRelay(rwc io.ReadWriteCloser) *SocketRelay

NewSocketRelay creates new socket based data relay.

func (*SocketRelay) Close

func (rl *SocketRelay) Close() error

Close the connection.

func (*SocketRelay) Receive

func (rl *SocketRelay) Receive() (data []byte, p Prefix, err error)

Receive data from the underlying process and returns associated prefix or error.

func (*SocketRelay) Send

func (rl *SocketRelay) Send(data []byte, flags byte) (err error)

Send signed (prefixed) data to PHP process.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL