goridge

package module
v2.0.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2018 License: MIT Imports: 8 Imported by: 0

README

High-performance PHP-to-Golang IPC bridge

Latest Stable Version GoDoc Build Status Scrutinizer Code Quality Go Report Card

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.


Goridge relays can be used independently to ensure fast IPC connection between PHP and Go processes.

Features

  • no external dependencies or services, drop-in
  • low message footprint (9 bytes over any binary payload)
  • 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

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));

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

import (
	"fmt"
	"github.com/spiral/goridge"
	"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 defines max amount of bytes to read from connection at once.
	BufferSize = 655336
)

Variables

This section is empty.

Functions

This section is empty.

Types

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 (*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 {
	// How many bytes to write/read at once.
	BufferSize uint64
	// 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)

Receive data from the underlying process and returns associated prefix or 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 [9]byte

Prefix is always 9 bytes long and contain meta flags and length of next data package. Receive prefix by converting it into the slice.

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) 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 {
	// How many bytes to write/read at once.
	BufferSize uint64
	// 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