Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
mkdir ./coverage-ci
go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage-ci/frame.out -covermode=atomic ./pkg/frame
go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage-ci/pipe.out -covermode=atomic ./pkg/pipe
go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage-ci/rpc.out -covermode=atomic ./pkg/rpc
go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage-ci/socket.out -covermode=atomic ./pkg/socket
go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage-ci/internal.out -covermode=atomic ./internal
- name: Run fuzz tests
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ jobs:
run: |
go test -v -race -cover -tags=debug -coverpkg=./... -covermode=atomic ./pkg/frame
go test -v -race -cover -tags=debug -coverpkg=./... -covermode=atomic ./pkg/pipe
go test -v -race -cover -tags=debug -coverpkg=./... -covermode=atomic ./pkg/rpc
go test -v -race -cover -tags=debug -coverpkg=./... -covermode=atomic ./pkg/socket
go test -v -race -cover -tags=debug -coverpkg=./... -covermode=atomic ./internal
1 change: 1 addition & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ jobs:
run: |
go test -v -race -tags=debug ./pkg/frame
go test -v -race -tags=debug ./pkg/pipe
go test -v -race -tags=debug ./pkg/rpc
go test -v -race -tags=debug ./pkg/socket
go test -v -race -tags=debug ./internal
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ test:
go test -v -race -cover -tags=debug ./internal
go test -v -race -cover -tags=debug ./pkg/frame
go test -v -race -cover -tags=debug ./pkg/pipe
go test -v -race -cover -tags=debug ./pkg/rpc
go test -v -race -cover -tags=debug ./pkg/socket

.PHONY: fuzz
Expand Down
83 changes: 33 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
High-performance PHP-to-Golang IPC transport
High-performance PHP-to-Golang IPC bridge
=================================================
[![GoDoc](https://godoc.org/github.com/roadrunner-server/goridge/v4?status.svg)](https://godoc.org/github.com/roadrunner-server/goridge/v4)
![Linux](https://github.com/roadrunner-server/goridge/workflows/Linux/badge.svg)
Expand All @@ -10,7 +10,8 @@ High-performance PHP-to-Golang IPC transport

<img src="https://files.phpclasses.org/graphics/phpclasses/innovation-award-logo.png" height="90px" alt="PHPClasses Innovation Award" align="left"/>

Goridge is a binary frame protocol with pipe and socket transports for inter-process communication between PHP and Go (or between any two processes that speak the goridge frame format). It exposes a 12-byte CRC-checked framing header, a small `Relay` interface, and ready-made pipe and TCP/Unix-socket implementations.
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 a minimal footprint, structures and `[]byte` support.
PHP source code can be found in this repository: [goridge-php](https://github.com/roadrunner-php/goridge)

<br/>
Expand All @@ -20,83 +21,65 @@ See https://github.com/roadrunner-server/roadrunner - High-performance PHP appli
Features
--------

- no external dependencies, drop-in (64-bit PHP required for the PHP side)
- no external dependencies or services, drop-in (64bit PHP version required)
- low message footprint (12 bytes over any binary payload), binary error detection
- CRC32 header verification
- sockets over TCP or Unix domain, standard pipes
- standalone protocol usage; bring your own RPC layer or none at all
- structured data transfer (json / msgpack / proto / gob / raw via codec flags)
- `[]byte` transfer, including large payloads
- 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 (named pipes, Unix-domain sockets via AF_UNIX)
- works on Windows
- unix sockets powered (also on Windows)

Installation
------------

```bash
go get github.com/roadrunner-server/goridge/v4
```go
GO111MODULE=on go get github.com/roadrunner-server/goridge/v4
```

Sample usage
------------

A minimal echo server using the socket relay:

### Sample of usage
```go
package main

import (
"log"
"fmt"
"net"
"net/rpc"

"github.com/roadrunner-server/goridge/v4/pkg/frame"
"github.com/roadrunner-server/goridge/v4/pkg/socket"
goridgeRpc "github.com/roadrunner-server/goridge/v4/pkg/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", "127.0.0.1:6001")
ln, err := net.Listen("tcp", ":6001")
if err != nil {
log.Fatal(err)
}
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("accept: %v", err)
return
}
go serve(conn)
panic(err)
}
}

func serve(conn net.Conn) {
relay := socket.NewSocketRelay(conn)
defer func() { _ = relay.Close() }()
_ = rpc.Register(new(App))

for {
in := frame.NewFrame()
if err := relay.Receive(in); err != nil {
return
}

// Mirror the incoming header so the response preserves the client's
// version, codec and any options/stream bits — a faithful echo.
out := frame.NewFrame()
out.WriteVersion(out.Header(), in.ReadVersion(in.Header()))
out.WriteFlags(out.Header(), in.ReadFlags())
out.WritePayload(in.Payload())
out.WritePayloadLen(out.Header(), uint32(len(in.Payload())))
out.WriteCRC(out.Header())

if err := relay.Send(out); err != nil {
return
conn, err := ln.Accept()
if err != nil {
continue
}
Comment thread
rustatian marked this conversation as resolved.
_ = conn
go rpc.ServeCodec(goridgeRpc.NewCodec(conn))
}
}
```

For the pipe-based variant, swap `socket.NewSocketRelay(conn)` for `pipe.NewPipeRelay(in, out)` where `in` is an `io.ReadCloser` and `out` is an `io.WriteCloser` — typically `os.Stdin`/`os.Stdout` on a child process.

License
-------

Expand Down
136 changes: 136 additions & 0 deletions benchmarks/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package main

import (
"context"
"net"
"net/http"
"net/http/pprof"
"net/rpc"
"time"

goridgeRpc "github.com/roadrunner-server/goridge/v4/pkg/rpc"
"github.com/roadrunner-server/goridge/v4/tests"
)

func main() {
s := NewServer()
go func() {
_ = s.Start("localhost:6061")
}()
defer func() {
_ = s.Stop(context.Background())
}()

time.Sleep(time.Second * 5)
// create an pprof server
server()
time.Sleep(time.Second * 1)

client()
}

// testService sample
type testService struct{}

func (s *testService) ProtoMessage(payload *tests.Payload, item *tests.Item) error {
(*item).Key = payload.Items[0].Key
return nil
}

func client() {
err := rpc.RegisterName("testbench", new(testService))
if err != nil {
panic(err)
}

conn, err := net.Dial("tcp", "127.0.0.1:18321")
if err != nil {
panic(err)
}

client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn))
defer func() {
err := client.Close()
if err != nil {
panic(err)
}
}()

tt := time.Now().String()

keysP := &tests.Payload{
Storage: "memory-rr",
Items: []*tests.Item{
{
Key: "a",
Value: "hhhhhhhhhhhhhhhhheeeeeeeeeeeeeeeeeeeeeeeeeeeelllllllllllllllllllllllllllllllllloooooooooooooooooooooooooooooo",
Timeout: tt,
},
{
Key: "b",
Value: "hhhhhhhhhhhhhhhhheeeeeeeeeeeeeeeeeeeeeeeeeeeelllllllllllllllllllllllllllllllllloooooooooooooooooooooooooooooo",
Timeout: tt,
},
{
Key: "c",
Value: "hhhhhhhhhhhhhhhhheeeeeeeeeeeeeeeeeeeeeeeeeeeelllllllllllllllllllllllllllllllllloooooooooooooooooooooooooooooo",
Timeout: tt,
},
},
}

item := &tests.Item{}
for range 1000000 {
err = client.Call("testbench.ProtoMessage", keysP, item)
if err != nil {
panic(err)
}
}
}

func server() {
ln, err := net.Listen("tcp", "127.0.0.1:18321")
if err != nil {
panic(err)
}

go func() {
for {
conn, err2 := ln.Accept()
if err2 != nil {
panic(err2)
}
rpc.ServeCodec(goridgeRpc.NewCodec(conn))
}
}()
}

// Server is a HTTP server for debugging.
type Server struct {
srv *http.Server
}

// NewServer creates new HTTP server for debugging.
func NewServer() Server {
mux := http.NewServeMux()

mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)

return Server{srv: &http.Server{Handler: mux}}
}

// Start debug server.
func (s *Server) Start(addr string) error {
s.srv.Addr = addr

return s.srv.ListenAndServe()
}

// Stop debug server.
func (s *Server) Stop(ctx context.Context) error {
return s.srv.Shutdown(ctx)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ toolchain go1.26.4
require (
github.com/roadrunner-server/errors v1.5.0
github.com/stretchr/testify v1.11.1
google.golang.org/protobuf v1.36.11
)

require (
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
Expand All @@ -17,6 +19,8 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
Expand Down
Loading
Loading