Skip to content

Commit

Permalink
Refactor and CI update
Browse files Browse the repository at this point in the history
  • Loading branch information
rustatian committed Dec 20, 2020
1 parent 42b3cbd commit 8a57c5d
Show file tree
Hide file tree
Showing 24 changed files with 5,585 additions and 282 deletions.
44 changes: 35 additions & 9 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ name: CI
on:
push:
pull_request:
branches:
# Branches from forks have the form 'user:branch-name' so we only run
# this job on pull_request events for branches that look like fork
# branches. Without this we would end up running this job twice for non
# forked PRs, once for the push and then once for opening the PR.
- '**:**'

jobs:
build:
name: Build (Go ${{ matrix.go }})
name: Build (Go ${{ matrix.go }} OS ${{ matrix.os }})
runs-on: ubuntu-20.04
timeout-minutes: 10
strategy:
fail-fast: false
matrix:
go: [ 1.14, 1.15 ]
os: [ ubuntu-20.04, windows-latest, macos-latest ]
steps:
- name: Set up Go ${{ matrix.go }}
uses: actions/setup-go@v2
Expand All @@ -32,23 +39,42 @@ jobs:
- name: Install Go dependencies
run: go mod download

- name: Run tests
run: go test -v -race -cover -coverprofile=goridge.txt -covermode=atomic

- name: Run golang tests on Windows without codecov
if: ${{ matrix.os == 'windows-latest' }}
run: |
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
- name: Run golang tests on MacOS or Linux with codecov
if: ${{ matrix.os == 'windows-latest' }}
run: |
mkdir ./coverage-ci
go test -v -race -cover -tags=debug -coverprofile=./coverage-ci/frame.txt -covermode=atomic ./pkg/frame
go test -v -race -cover -tags=debug -coverprofile=./coverage-ci/pipe.txt -covermode=atomic ./pkg/pipe
go test -v -race -cover -tags=debug -coverprofile=./coverage-ci/rpc.txt -covermode=atomic ./pkg/rpc
go test -v -race -cover -tags=debug -coverprofile=./coverage-ci/socket.txt -covermode=atomic ./pkg/socket
cat ./coverage-ci/*.txt > ./coverage-ci/summary.txt
- uses: codecov/codecov-action@v1 # Docs: <https://github.com/codecov/codecov-action>
if: ${{ matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest' }}
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./goridge.txt
file: ./coverage-ci/summary.txt
fail_ci_if_error: false

golangci-check:
golangci-lint:
name: Golang-CI (lint)
runs-on: ubuntu-20.04
steps:
- name: Check out code
uses: actions/checkout@v1
uses: actions/checkout@v2

- name: golangci-lint
uses: reviewdog/action-golangci-lint@v1 # action page: <https://github.com/reviewdog/action-golangci-lint>
- name: Run linter
uses: golangci/golangci-lint-action@v2 # Action page: <https://github.com/golangci/golangci-lint-action>
with:
github_token: ${{ secrets.github_token }}
version: v1.33 # without patch version
only-new-issues: false # show only new issues if it's a pull request
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
test:
go test -v -race -cover

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
8 changes: 5 additions & 3 deletions relay.go → interfaces/relay/relay.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package goridge
package relay

import "github.com/spiral/goridge/v3/pkg/frame"

// Relay provide IPC over signed payloads.
type Relay interface {
// Send signed (prefixed) data to PHP process.
Send(frame *Frame) error
Send(frame *frame.Frame) error

// Receive data from the underlying process and returns associated prefix or error.
Receive(frame *Frame) error
Receive(frame *frame.Frame) error

// Close the connection.
Close() error
Expand Down
16 changes: 7 additions & 9 deletions receive.go → internal/receive.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package goridge
package internal

import (
"io"

"github.com/spiral/errors"
"github.com/spiral/goridge/v3/pkg/frame"
)

func receiveFrame(relay io.Reader, frame *Frame) error {
func ReceiveFrame(relay io.Reader, fr *frame.Frame) error {
const op = errors.Op("pipes frame receive")
// header bytes
hb := make([]byte, 12)
Expand All @@ -16,11 +17,11 @@ func receiveFrame(relay io.Reader, frame *Frame) error {
}

// Read frame header
header := ReadHeader(hb)
header := frame.ReadHeader(hb)
// we have options
if header.readHL() > 3 {
if header.ReadHL() > 3 {
// we should read the options
optsLen := (header.readHL() - 3) * WORD
optsLen := (header.ReadHL() - 3) * frame.WORD
opts := make([]byte, optsLen)
_, err := io.ReadFull(relay, opts)
if err != nil {
Expand All @@ -41,10 +42,7 @@ func receiveFrame(relay io.Reader, frame *Frame) error {
return errors.E(op, err)
}

*frame = Frame{
payload: pb,
header: header.header,
}
*fr = *frame.From(header.Header(), pb)

return nil
}
110 changes: 0 additions & 110 deletions pipe_test.go

This file was deleted.

19 changes: 11 additions & 8 deletions frame.go → pkg/frame/frame.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goridge
package frame

import "hash/crc32"

Expand Down Expand Up @@ -55,8 +55,11 @@ func NewFrame() *Frame {
}

// MergeHeader merge header from other frame with original payload
func (f *Frame) MergeHeader(frame *Frame) {
f.header = frame.header
func From(header []byte, payload []byte) *Frame {
return &Frame{
payload: payload,
header: header,
}
}

// To read version, we should return our 4 upper bits to their original place
Expand All @@ -81,7 +84,7 @@ func (f *Frame) WriteVersion(version Version) {
// The lower 4 bits of the 0th octet occupies our header len data.
// We should erase upper 4 bits, which contain information about Version
// To erase, we applying bitwise AND to the upper 4 bits and returning result
func (f *Frame) readHL() byte {
func (f *Frame) ReadHL() byte {
_ = f.header[0]
// 0101_1111 0000_1111
return f.header[0] & 0x0F
Expand All @@ -96,7 +99,7 @@ func (f *Frame) writeHl(hl byte) {

func (f *Frame) incrementHL() {
_ = f.header[0]
hl := f.readHL()
hl := f.ReadHL()
if hl > 15 {
panic("header len should be less than 15")
}
Expand Down Expand Up @@ -129,7 +132,7 @@ func (f *Frame) WriteOptions(options ...uint32) {
panic("header options limited by 40 bytes")
}

hl := f.readHL()
hl := f.ReadHL()
// check before writing. we can't handle more than 15*4 bytes of HL (3 for header and 12 for options)
if hl == 15 {
panic("header len could not be more than 15")
Expand Down Expand Up @@ -160,11 +163,11 @@ const lb = 12
// extra WORDS will add extra 32bits to the options (4 bytes)
func (f *Frame) ReadOptions() []uint32 {
// we can read options, if there are no options
if f.readHL() <= 3 {
if f.ReadHL() <= 3 {
return nil
}
// Get the options len
optionLen := f.readHL() - 3 // 3 is the default
optionLen := f.ReadHL() - 3 // 3 is the default
// slice in place
options := make([]uint32, 0, optionLen)

Expand Down
2 changes: 1 addition & 1 deletion frame.md → pkg/frame/frame.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Overlapping flags are just bit flags. They might be combined with bitwise OR and checked with bitwise AND. Non-overlapping flags
can't be used with other flags. In means, that if you have non-overlapping flag in 1-st byte, you can't use other flags.

3. `(2, 3, 4, 5)` bytes contain payload length and presented by unsigned long 32bit integer (up to 4Gb in payload).
3. `(2, 3, 4, 5)` bytes contain payload length and represented by unsigned long 32bit integer (up to 4Gb in payload).
4. `(6, 7, 8, 9)` bytes contain header `CRC32` checksum. CRC32 calculated only for `0-5` (including) bytes.
5. `(10, 11)` bytes are padding and currently reserved for future use. For example as `sequence Id` for async operations.
6. `(12..52)` bytes contain options. Options are optional. As an example of usage, in `goridge` in case of pipes or sockets
Expand Down
2 changes: 1 addition & 1 deletion frame_flags.go → pkg/frame/frame_flags.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goridge
package frame

type FrameFlag byte

Expand Down
2 changes: 1 addition & 1 deletion frame_test.go → pkg/frame/frame_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goridge
package frame

import (
"hash/crc32"
Expand Down
19 changes: 11 additions & 8 deletions pipe.go → pkg/pipe/pipe.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package goridge
package pipe

import (
"io"

"github.com/spiral/errors"
"github.com/spiral/goridge/v3/interfaces/relay"
"github.com/spiral/goridge/v3/internal"
"github.com/spiral/goridge/v3/pkg/frame"
)

// 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.
type PipeRelay struct {
type Relay struct {
in io.ReadCloser
out io.WriteCloser
}

// NewPipeRelay creates new pipe based data relay.
func NewPipeRelay(in io.ReadCloser, out io.WriteCloser) Relay {
return &PipeRelay{in: in, out: out}
func NewPipeRelay(in io.ReadCloser, out io.WriteCloser) relay.Relay {
return &Relay{in: in, out: out}
}

// Send signed (prefixed) data to underlying process.
func (rl *PipeRelay) Send(frame *Frame) error {
func (rl *Relay) Send(frame *frame.Frame) error {
const op = errors.Op("pipes frame send")
_, err := rl.out.Write(frame.Bytes())
if err != nil {
Expand All @@ -28,11 +31,11 @@ func (rl *PipeRelay) Send(frame *Frame) error {
return nil
}

func (rl *PipeRelay) Receive(frame *Frame) error {
return receiveFrame(rl.in, frame)
func (rl *Relay) Receive(frame *frame.Frame) error {
return internal.ReceiveFrame(rl.in, frame)
}

// Close the connection. Pipes are closed automatically with the underlying process.
func (rl *PipeRelay) Close() error {
func (rl *Relay) Close() error {
return nil
}
Loading

0 comments on commit 8a57c5d

Please sign in to comment.