Skip to content

Commit

Permalink
merge stream and newenc into encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
fionera committed Sep 1, 2023
1 parent 0c4a144 commit 0c5d4ad
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 112 deletions.
27 changes: 13 additions & 14 deletions pkg/newenc/actionwriter.go → pkg/encoding/actionwriter.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package newenc
package encoding

import (
"fmt"
"github.com/fionera/haproxy-go/pkg/encoding"
"net/netip"
"sync"
)
Expand Down Expand Up @@ -84,7 +83,7 @@ func (aw *ActionWriter) actionHeader(t actionType, s varScope, name []byte) erro
aw.data[aw.off] = byte(s)
aw.off++

n, err := encoding.PutBytes(aw.data[aw.off:], name)
n, err := PutBytes(aw.data[aw.off:], name)
if err != nil {
return err
}
Expand All @@ -102,10 +101,10 @@ func (aw *ActionWriter) SetStringBytes(s varScope, name string, v []byte) error
return err
}

aw.data[aw.off] = byte(dataTypeString)
aw.data[aw.off] = byte(DataTypeString)
aw.off++

n, err := encoding.PutBytes(aw.data[aw.off:], v)
n, err := PutBytes(aw.data[aw.off:], v)
if err != nil {
return err
}
Expand All @@ -122,10 +121,10 @@ func (aw *ActionWriter) SetBinary(s varScope, name string, v []byte) error {
return err
}

aw.data[aw.off] = byte(dataTypeBinary)
aw.data[aw.off] = byte(DataTypeBinary)
aw.off++

n, err := encoding.PutBytes(aw.data[aw.off:], v)
n, err := PutBytes(aw.data[aw.off:], v)
if err != nil {
return err
}
Expand All @@ -139,7 +138,7 @@ func (aw *ActionWriter) SetNull(s varScope, name string) error {
return err
}

aw.data[aw.off] = byte(dataTypeNull)
aw.data[aw.off] = byte(DataTypeNull)
aw.off++

return nil
Expand All @@ -149,7 +148,7 @@ func (aw *ActionWriter) SetBool(s varScope, name string, v bool) error {
return err
}

aw.data[aw.off] = byte(dataTypeBool)
aw.data[aw.off] = byte(DataTypeBool)
if v {
aw.data[aw.off] |= dataFlagTrue
}
Expand All @@ -171,10 +170,10 @@ func (aw *ActionWriter) SetInt64(s varScope, name string, v int64) error {
return err
}

aw.data[aw.off] = byte(dataTypeInt64)
aw.data[aw.off] = byte(DataTypeInt64)
aw.off++

n, err := encoding.PutVarint(aw.data[aw.off:], v)
n, err := PutVarint(aw.data[aw.off:], v)
if err != nil {
return err
}
Expand All @@ -193,15 +192,15 @@ func (aw *ActionWriter) SetAddr(s varScope, name string, v netip.Addr) error {

switch {
case v.Is6():
aw.data[aw.off] = byte(dataTypeIPV6)
aw.data[aw.off] = byte(DataTypeIPV6)
case v.Is4():
aw.data[aw.off] = byte(dataTypeIPV4)
aw.data[aw.off] = byte(DataTypeIPV4)
default:
return fmt.Errorf("invalid address")
}
aw.off++

n, err := encoding.PutAddr(aw.data[aw.off:], v)
n, err := PutAddr(aw.data[aw.off:], v)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package newenc
package encoding

import (
"fmt"
Expand Down
18 changes: 18 additions & 0 deletions pkg/encoding/other.go → pkg/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ import (
"net/netip"
)

type DataType byte

const (
DataTypeNull DataType = 0
DataTypeBool DataType = 1
DataTypeInt32 DataType = 2
DataTypeUInt32 DataType = 3
DataTypeInt64 DataType = 4
DataTypeUInt64 DataType = 5
DataTypeIPV4 DataType = 6
DataTypeIPV6 DataType = 7
DataTypeString DataType = 8
DataTypeBinary DataType = 9

dataTypeMask byte = 0x0F
dataFlagTrue byte = 0x10
)

func PutBytes(b []byte, v []byte) (int, error) {
l := len(v)
n, err := PutVarint(b, int64(l))
Expand Down
30 changes: 5 additions & 25 deletions pkg/stream/kv.go → pkg/encoding/kvscanner.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package stream
package encoding

import (
"fmt"
"net"
"net/netip"
"sync"

"github.com/fionera/haproxy-go/pkg/encoding"
)

var kvEntryPool = sync.Pool{
Expand Down Expand Up @@ -144,24 +142,6 @@ func (k *KVEntry) Value() any {
}
}

type DataType byte

const (
DataTypeNull DataType = 0
DataTypeBool DataType = 1
DataTypeInt32 DataType = 2
DataTypeUInt32 DataType = 3
DataTypeInt64 DataType = 4
DataTypeUInt64 DataType = 5
DataTypeIPV4 DataType = 6
DataTypeIPV6 DataType = 7
DataTypeString DataType = 8
DataTypeBinary DataType = 9

dataTypeMask byte = 0x0F
dataFlagTrue byte = 0x10
)

func (k *KVScanner) Next(e *KVEntry) bool {
if len(k.buf) == 0 {
return false
Expand All @@ -172,7 +152,7 @@ func (k *KVScanner) Next(e *KVEntry) bool {
}
k.left--

nameLen, n, err := encoding.Varint(k.buf)
nameLen, n, err := Varint(k.buf)
if err != nil {
k.lastErr = err
return false
Expand All @@ -193,7 +173,7 @@ func (k *KVScanner) Next(e *KVEntry) bool {

case DataTypeInt32, DataTypeInt64,
DataTypeUInt32, DataTypeUInt64:
e.intVal, n, k.lastErr = encoding.Varint(k.buf)
e.intVal, n, k.lastErr = Varint(k.buf)
if k.lastErr != nil {
return false
}
Expand All @@ -209,7 +189,7 @@ func (k *KVScanner) Next(e *KVEntry) bool {
k.buf = k.buf[net.IPv6len:]

case DataTypeString:
nameLen, n, err := encoding.Varint(k.buf)
nameLen, n, err := Varint(k.buf)
if err != nil {
k.lastErr = err
return false
Expand All @@ -220,7 +200,7 @@ func (k *KVScanner) Next(e *KVEntry) bool {
k.buf = k.buf[nameLen:]

case DataTypeBinary:
valLen, n, err := encoding.Varint(k.buf)
valLen, n, err := Varint(k.buf)
if err != nil {
k.lastErr = err
return false
Expand Down
35 changes: 17 additions & 18 deletions pkg/newenc/kvwriter.go → pkg/encoding/kvwriter.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package newenc
package encoding

import (
"fmt"
"github.com/fionera/haproxy-go/pkg/encoding"
"net/netip"
"sync"
)
Expand Down Expand Up @@ -47,7 +46,7 @@ func (aw *KVWriter) Bytes() []byte {
}

func (aw *KVWriter) writeKey(name []byte) error {
n, err := encoding.PutBytes(aw.data[aw.off:], name)
n, err := PutBytes(aw.data[aw.off:], name)
if err != nil {
return err
}
Expand All @@ -61,10 +60,10 @@ func (aw *KVWriter) SetString(name string, v string) error {
return err
}

aw.data[aw.off] = byte(dataTypeString)
aw.data[aw.off] = byte(DataTypeString)
aw.off++

n, err := encoding.PutBytes(aw.data[aw.off:], []byte(v))
n, err := PutBytes(aw.data[aw.off:], []byte(v))
if err != nil {
return err
}
Expand All @@ -78,10 +77,10 @@ func (aw *KVWriter) SetBinary(name string, v []byte) error {
return err
}

aw.data[aw.off] = byte(dataTypeBinary)
aw.data[aw.off] = byte(DataTypeBinary)
aw.off++

n, err := encoding.PutBytes(aw.data[aw.off:], v)
n, err := PutBytes(aw.data[aw.off:], v)
if err != nil {
return err
}
Expand All @@ -95,7 +94,7 @@ func (aw *KVWriter) SetNull(name string) error {
return err
}

aw.data[aw.off] = byte(dataTypeNull)
aw.data[aw.off] = byte(DataTypeNull)
aw.off++

return nil
Expand All @@ -105,7 +104,7 @@ func (aw *KVWriter) SetBool(name string, v bool) error {
return err
}

aw.data[aw.off] = byte(dataTypeBool)
aw.data[aw.off] = byte(DataTypeBool)
if v {
aw.data[aw.off] |= dataFlagTrue
}
Expand All @@ -115,22 +114,22 @@ func (aw *KVWriter) SetBool(name string, v bool) error {
}

func (aw *KVWriter) SetUInt32(name string, v uint32) error {
return aw.setInt(name, dataTypeUInt32, int64(v))
return aw.setInt(name, DataTypeUInt32, int64(v))
}

func (aw *KVWriter) SetInt32(name string, v int32) error {
return aw.setInt(name, dataTypeInt32, int64(v))
return aw.setInt(name, DataTypeInt32, int64(v))
}

func (aw *KVWriter) setInt(name string, d dataType, v int64) error {
func (aw *KVWriter) setInt(name string, d DataType, v int64) error {
if err := aw.writeKey([]byte(name)); err != nil {
return err
}

aw.data[aw.off] = byte(d)
aw.off++

n, err := encoding.PutVarint(aw.data[aw.off:], v)
n, err := PutVarint(aw.data[aw.off:], v)
if err != nil {
return err
}
Expand All @@ -140,10 +139,10 @@ func (aw *KVWriter) setInt(name string, d dataType, v int64) error {
}

func (aw *KVWriter) SetInt64(name string, v int64) error {
return aw.setInt(name, dataTypeInt64, v)
return aw.setInt(name, DataTypeInt64, v)
}
func (aw *KVWriter) SetUInt64(name string, v uint64) error {
return aw.setInt(name, dataTypeUInt64, int64(v))
return aw.setInt(name, DataTypeUInt64, int64(v))
}

func (aw *KVWriter) SetAddr(name string, v netip.Addr) error {
Expand All @@ -153,15 +152,15 @@ func (aw *KVWriter) SetAddr(name string, v netip.Addr) error {

switch {
case v.Is6():
aw.data[aw.off] = byte(dataTypeIPV6)
aw.data[aw.off] = byte(DataTypeIPV6)
case v.Is4():
aw.data[aw.off] = byte(dataTypeIPV4)
aw.data[aw.off] = byte(DataTypeIPV4)
default:
return fmt.Errorf("invalid address")
}
aw.off++

n, err := encoding.PutAddr(aw.data[aw.off:], v)
n, err := PutAddr(aw.data[aw.off:], v)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package newenc
package encoding

import (
"fmt"
Expand Down
6 changes: 2 additions & 4 deletions pkg/stream/message.go → pkg/encoding/messagescanner.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package stream
package encoding

import (
"sync"

"github.com/fionera/haproxy-go/pkg/encoding"
)

var messagePool = sync.Pool{
Expand Down Expand Up @@ -83,7 +81,7 @@ func (s *MessageScanner) Next(m *Message) bool {
return false
}

nameLen, n, err := encoding.Varint(s.buf)
nameLen, n, err := Varint(s.buf)
if err != nil {
s.lastErr = err
return false
Expand Down
19 changes: 0 additions & 19 deletions pkg/newenc/kv.go

This file was deleted.

9 changes: 4 additions & 5 deletions spop/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"bufio"
"encoding/binary"
"fmt"
"github.com/fionera/haproxy-go/pkg/newenc"
"github.com/fionera/haproxy-go/pkg/stream"
"github.com/fionera/haproxy-go/pkg/encoding"
"log"
"net"
)
Expand All @@ -19,12 +18,12 @@ const (
)

type Handler interface {
HandleSPOE(*newenc.ActionWriter, *stream.Message)
HandleSPOE(*encoding.ActionWriter, *encoding.Message)
}

type HandlerFunc func(*newenc.ActionWriter, *stream.Message)
type HandlerFunc func(*encoding.ActionWriter, *encoding.Message)

func (h HandlerFunc) HandleSPOE(w *newenc.ActionWriter, m *stream.Message) {
func (h HandlerFunc) HandleSPOE(w *encoding.ActionWriter, m *encoding.Message) {
h(w, m)
}

Expand Down
Loading

0 comments on commit 0c5d4ad

Please sign in to comment.