Skip to content

Commit

Permalink
some refactoring and remove dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
negasus committed May 22, 2023
1 parent 6bdf2f4 commit dcff814
Show file tree
Hide file tree
Showing 22 changed files with 292 additions and 168 deletions.
1 change: 0 additions & 1 deletion action/actions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package action

// TODO: Drop this type and use plain []Action.
type Actions []Action

func (actions *Actions) SetVar(scope Scope, name string, value interface{}) {
Expand Down
2 changes: 1 addition & 1 deletion action/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package action

import (
"fmt"

"github.com/negasus/haproxy-spoe-go/typeddata"
"github.com/negasus/haproxy-spoe-go/varint"
)

func (action *Action) Marshal(buf []byte) ([]byte, error) {

var nb byte

switch action.Type {
Expand Down
14 changes: 7 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ import (
"bufio"
"bytes"
"fmt"
"github.com/negasus/haproxy-spoe-go/frame"
_ "github.com/negasus/haproxy-spoe-go/request"
"io"
"net"

"github.com/negasus/haproxy-spoe-go/frame"
)

/// Client is a simple client for spop protocol, this should only be used for testing purpose
// Client is a simple client for spop protocol, this should only be used for testing purpose
type Client struct {
conn net.Conn
reader io.Reader
}

/// NewClient create a new Client for an established connection
// NewClient create a new Client for an established connection
func NewClient(conn net.Conn) Client {
return Client{conn: conn, reader: bufio.NewReader(conn)}
}

/// Init initialize the client by sending the HaproxyHello frame
// Init initialize the client by sending the HaproxyHello frame
func (c *Client) Init() error {
f := frame.AcquireFrame()
defer frame.ReleaseFrame(f)
Expand Down Expand Up @@ -70,7 +70,7 @@ func (c *Client) send(f *frame.Frame) error {
return nil
}

/// Notify send an empty Notify frame
// Notify send an empty Notify frame
func (c *Client) Notify() error {
f := frame.AcquireFrame()
defer frame.ReleaseFrame(f)
Expand All @@ -89,7 +89,7 @@ func (c *Client) Notify() error {
return nil
}

/// Stop the client by sending HaproxyDisconnect frame
// Stop the client by sending HaproxyDisconnect frame
func (c *Client) Stop() error {
f := frame.AcquireFrame()
defer frame.ReleaseFrame(f)
Expand Down
3 changes: 1 addition & 2 deletions frame/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

func (f *Frame) Encode(dest io.Writer) (n int, err error) {

buf := bytes.Buffer{}

buf.WriteByte(byte(f.Type))
Expand Down Expand Up @@ -45,7 +44,7 @@ func (f *Frame) Encode(dest io.Writer) (n int, err error) {
}
case TypeNotify:
if len(*f.Messages) > 0 {
err = fmt.Errorf("Encoding Notify frame with Message isn't handled yet")
err = fmt.Errorf("encoding Notify frame with Message isn't handled yet")
return

}
Expand Down
27 changes: 18 additions & 9 deletions frame/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package frame
import (
"bytes"
"encoding/binary"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

Expand All @@ -17,15 +15,26 @@ func TestFrame_Write(t *testing.T) {
f.KV.Add("key2", "val2")
buf := &bytes.Buffer{}
frameSize, err := f.Encode(buf)
require.Nil(t, err)
if err != nil {
t.Fatalf("expect err is nil, got %v", err)
}
bufBytes := buf.Bytes()
encodedFrameSize := int(binary.BigEndian.Uint32(bufBytes[0:4]))
assert.Equal(t, frameSize-4, encodedFrameSize, "frame size")
assert.Equal(t, "key1", string(bufBytes[13:17]))
assert.Equal(t, "val1", string(bufBytes[19:23]))
assert.Equal(t, "key2", string(bufBytes[24:28]))
assert.Equal(t, "val2", string(bufBytes[30:34]))

if frameSize-4 != encodedFrameSize {
t.Fatal("wrong frame size")
}
if string(bufBytes[13:17]) != "key1" {
t.Fatal("expect key1")
}
if string(bufBytes[19:23]) != "val1" {
t.Fatal("expect val1")
}
if string(bufBytes[24:28]) != "key2" {
t.Fatal("expect key2")
}
if string(bufBytes[30:34]) != "val2" {
t.Fatal("expect val1")
}
}

func BenchmarkFrame_Encode(b *testing.B) {
Expand Down
6 changes: 3 additions & 3 deletions frame/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func ReleaseFrame(frame *Frame) {
framePool.Put(frame)
}

//Frame describe frame struct
// Frame describe frame struct
type Frame struct {
Len uint32
Type Type
Expand Down Expand Up @@ -80,12 +80,12 @@ func (f *Frame) Reset() {
f.KV.Reset()
}

//IsFin returns true, if frame has flag 'FIN'
// IsFin returns true, if frame has flag 'FIN'
func (f *Frame) IsFin() bool {
return f.Flags&0x01 > 0
}

//IsAbort returns true, if frame has flag 'ABORT'
// IsAbort returns true, if frame has flag 'ABORT'
func (f *Frame) IsAbort() bool {
return f.Flags&0x02 > 0
}
5 changes: 3 additions & 2 deletions frame/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package frame
import (
"encoding/binary"
"fmt"
"github.com/negasus/haproxy-spoe-go/varint"
"io"

"github.com/negasus/haproxy-spoe-go/varint"
)

func (f *Frame) Read(src io.Reader) error {
Expand All @@ -23,7 +24,7 @@ func (f *Frame) Read(src io.Reader) error {
f.Type = Type(f.tmp[4])

// Drop packet that doesn't have defined frame type early, before allocating any buffers
// that way spurious connectons (say someone calling curl on port) won't cause it to
// that way spurious connections (say someone calling curl on port) won't cause it to
// allocate gigabytes of RAM
switch f.Type {
case TypeHaproxyHello, TypeHaproxyDisconnect, TypeNotify, TypeAgentHello, TypeAgentDisconnect, TypeAgentAck:
Expand Down
34 changes: 24 additions & 10 deletions frame/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package frame

import (
"bytes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"testing"
)
Expand All @@ -25,17 +23,33 @@ func TestFrame_Read(t *testing.T) {
r := bytes.NewBuffer(testFrame)
f := NewFrame()
err := f.Read(r)
require.Nil(t, err)
assert.Equal(t, 1, int(f.FrameID), "FrameID")
assert.Equal(t, 542, int(f.StreamID), "StreamID")
assert.Equal(t, TypeNotify, f.Type)
if err != nil {
t.Fatal()
}
if int(f.FrameID) != 1 {
t.Fatal("wrong FrameID")
}
if int(f.StreamID) != 542 {
t.Fatal("wrong StreamID")
}
if f.Type != TypeNotify {
t.Fatal("wrong type")
}
messages := *f.Messages
require.Len(t, messages, 1)
if len(messages) != 1 {
t.Fatal("wrong messages len")
}
host, found := messages[0].KV.Get("host")
require.True(t, found, "key host")
if !found {
t.Fatal("host not found")
}
hostString, ok := host.(string)
require.True(t, ok)
assert.Equal(t, "domain.example.com", hostString)
if !ok {
t.Fatal("error convert host to string")
}
if hostString != "domain.example.com" {
t.Fatal("wrong hostString")
}
}

func BenchmarkFrame_Read(b *testing.B) {
Expand Down
12 changes: 0 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
module github.com/negasus/haproxy-spoe-go

go 1.19

require (
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
18 changes: 0 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +0,0 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1 change: 0 additions & 1 deletion message/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
)

func (m *Messages) Decode(buf []byte) error {

for {
if len(buf) == 0 {
break
Expand Down
52 changes: 37 additions & 15 deletions message/decode_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package message

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"bytes"
"testing"
)

Expand All @@ -16,29 +15,52 @@ func TestDecode(t *testing.T) {
}

err := mess.Decode(buf)
require.NoError(t, err)
require.Equal(t, 2, mess.Len())
if err != nil {
t.Fatal("unexpected error")
}
if mess.Len() != 2 {
t.Fatalf("mess.Len must be 2, got %d", mess.Len())
}

// First message
m, err := mess.GetByIndex(0)
require.NoError(t, err)
assert.Equal(t, "Foo", m.Name)
if err != nil {
t.Fatal("unexpected error")
}
if m.Name != "Foo" {
t.Fatalf("m.Name must be Foo, got %s", m.Name)
}

v, ok := m.KV.Get("Bar")
require.True(t, ok)
require.Equal(t, []byte{0x10, 0x20, 0x30}, v)
if !ok {
t.Fatal("ok is not true")
}
if !bytes.Equal([]byte{0x10, 0x20, 0x30}, v.([]byte)) {
t.Fatal("invalid result")
}

v, ok = m.KV.Get("Vals")
require.True(t, ok)
require.Equal(t, "Baz", v)
if !ok {
t.Fatal("ok is not true")
}
if v != "Baz" {
t.Fatalf("v must be Baz, got %s", v)
}

// Second message
m, err = mess.GetByIndex(1)
require.NoError(t, err)
assert.Equal(t, "UI", m.Name)
if err != nil {
t.Fatal("unexpected error")
}
if m.Name != "UI" {
t.Fatalf("m.Name must be UI, got %s", m.Name)
}

v, ok = m.KV.Get("Fee")
require.True(t, ok)
require.Equal(t, int32(10), v)

if !ok {
t.Fatal("ok is not true")
}
if v != int32(10) {
t.Fatalf("v must be int32(10), got %d", v)
}
}
3 changes: 2 additions & 1 deletion message/message.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package message

import (
"github.com/negasus/haproxy-spoe-go/payload/kv"
"sync"

"github.com/negasus/haproxy-spoe-go/payload/kv"
)

var messagePool = sync.Pool{
Expand Down
4 changes: 3 additions & 1 deletion message/messages.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package message

import "errors"
import (
"errors"
)

var (
ErrMessageNotFound = errors.New("message not found")
Expand Down
3 changes: 2 additions & 1 deletion payload/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package kv

import (
"fmt"
"sync"

"github.com/negasus/haproxy-spoe-go/typeddata"
"github.com/negasus/haproxy-spoe-go/varint"
"sync"
)

var kvPool = sync.Pool{
Expand Down
3 changes: 1 addition & 2 deletions typeddata/typeddata.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package typeddata

import (
"errors"
"fmt"
"net"
"reflect"

"github.com/negasus/haproxy-spoe-go/varint"

"github.com/pkg/errors"
)

const (
Expand Down
Loading

0 comments on commit dcff814

Please sign in to comment.