Skip to content

Commit

Permalink
foo
Browse files Browse the repository at this point in the history
  • Loading branch information
fionera committed Aug 22, 2024
1 parent 9f70b80 commit c47f66f
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 149 deletions.
8 changes: 8 additions & 0 deletions spop/benchmarks/benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func (Dispatcher) ServeDropMorePacket(w *zapi.ActionWriter, m *zapi.Message) {
k := zapi.AcquireKVEntry()
defer zapi.ReleaseKVEntry(k)

//err := m.KV.Iterate(func(entry *zapi.KVEntry) error {
// zapi.ReleaseKVEntry(entry)
// return nil
//})
//if err != nil {
// panic(err)
//}

for m.KV.Next(k) {

}
Expand Down
147 changes: 147 additions & 0 deletions spop/zapi/kventry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package zapi

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

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

type KVEntry struct {
name []byte

dataType DataType

byteVal []byte
intVal int64
boolVar bool
}

func (k *KVEntry) Read(buf []byte) (int, error) {
var offset int

vLen, n, err := encoding.Varint(buf)
offset += n
if err != nil {
return offset, err
}

k.name = buf[offset : offset+int(vLen)]
offset += int(vLen)

k.dataType = DataType(buf[offset] & DataTypeMask)
// just always decode the boolVar even tho its wrong.
k.boolVar = buf[offset]&DataFlagTrue > 0
offset++

switch k.dataType {
case DataTypeNull, DataTypeBool:
// noop

case DataTypeInt32, DataTypeInt64,
DataTypeUInt32, DataTypeUInt64:
var v uint64
v, n, err = encoding.Varint(buf[offset:])
offset += n
if err != nil {
return offset, err
}
k.intVal = int64(v)

case DataTypeIPV4:
k.byteVal = buf[offset : offset+net.IPv4len]
offset += net.IPv4len

case DataTypeIPV6:
k.byteVal = buf[offset : offset+net.IPv6len]
offset += net.IPv6len

case DataTypeString, DataTypeBinary:
vLen, n, err = encoding.Varint(buf[offset:])
offset += n
if err != nil {
return offset, err
}

k.byteVal = buf[offset : offset+int(vLen)]
offset += int(vLen)

default:
return offset, fmt.Errorf("unknown data type: %x", k.dataType)
}

return offset, nil
}

func (k *KVEntry) NameBytes() []byte {
return k.name
}

func (k *KVEntry) ValueBytes() []byte {
return k.byteVal
}

func (k *KVEntry) ValueInt() int64 {
return k.intVal
}

func (k *KVEntry) ValueBool() bool {
return k.boolVar
}

func (k *KVEntry) ValueAddr() netip.Addr {
addr, ok := netip.AddrFromSlice(k.byteVal)
if !ok {
panic("invalid addr decode")
}
return addr
}

func (k *KVEntry) NameEquals(s string) bool {
// bytes.Equal describes this operation as alloc free
return string(k.name) == s
}

func (k *KVEntry) Type() DataType {
return k.dataType
}

// Value returns the typed value for the KVEntry. It can allocate memory
// which is why assertions and direct type access is recommended.
func (k *KVEntry) Value() any {
switch k.dataType {
case DataTypeNull:
return nil
case DataTypeBool:
return k.boolVar
case DataTypeInt32:
return int32(k.intVal)
case DataTypeInt64:
return k.intVal
case DataTypeUInt32:
return uint32(k.intVal)
case DataTypeUInt64:
return uint64(k.intVal)
case DataTypeIPV4, DataTypeIPV6:
addr, ok := netip.AddrFromSlice(k.byteVal)
if !ok {
panic("invalid addr decode")
}
return addr
case DataTypeString:
return string(k.byteVal)
case DataTypeBinary:
return k.byteVal
default:
panic("unknown datatype")
}
}

func (k *KVEntry) Reset() {
k.name = nil
k.dataType = 0
k.byteVal = nil
k.boolVar = false
k.intVal = 0
}
Loading

0 comments on commit c47f66f

Please sign in to comment.