Skip to content

Commit

Permalink
refactor: Swap out JSON only in agent client and server subsystems
Browse files Browse the repository at this point in the history
Signed-off-by: Felicitas Pojtinger <felicitas@pojtinger.com>
  • Loading branch information
pojntfx committed Dec 22, 2024
1 parent 0ec9815 commit fe11abc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ toolchain go1.23.2
require (
github.com/coreos/go-iptables v0.8.0
github.com/freddierice/go-losetup/v2 v2.0.1
github.com/fxamacker/cbor/v2 v2.7.0
github.com/klauspost/compress v1.17.11
github.com/lithammer/shortuuid/v4 v4.2.0
github.com/loopholelabs/goroutine-manager v0.1.1
Expand Down Expand Up @@ -51,6 +52,7 @@ require (
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/redis/go-redis/v9 v9.7.0 // indirect
github.com/rs/xid v1.6.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/freddierice/go-losetup/v2 v2.0.1 h1:wPDx/Elu9nDV8y/CvIbEDz5Xi5Zo80y4h7MKbi3XaAI=
github.com/freddierice/go-losetup/v2 v2.0.1/go.mod h1:TEyBrvlOelsPEhfWD5rutNXDmUszBXuFnwT1kIQF4J8=
github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E=
github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
Expand Down Expand Up @@ -191,6 +193,8 @@ github.com/vishvananda/netlink v1.3.0/go.mod h1:i6NetklAujEcC6fK0JPjT8qSwWyO0HLn
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
github.com/vishvananda/netns v0.0.5 h1:DfiHV+j8bA32MFM7bfEunvT8IAqQ/NzSJHtcmW5zdEY=
github.com/vishvananda/netns v0.0.5/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
Expand Down
22 changes: 11 additions & 11 deletions pkg/ipc/agent_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package ipc

import (
"context"
"encoding/json"
"errors"
"sync"

"github.com/fxamacker/cbor/v2"
"github.com/loopholelabs/drafter/internal/vsock"
"github.com/loopholelabs/goroutine-manager/pkg/manager"
"github.com/pojntfx/panrpc/go/pkg/rpc"
Expand Down Expand Up @@ -139,7 +139,7 @@ func StartAgentClient[L *AgentClientLocal[G], R AgentClientRemote, G any](
}
})

registry := rpc.NewRegistry[R, json.RawMessage](
registry := rpc.NewRegistry[R, cbor.RawMessage](
agentClientLocal,

&rpc.RegistryHooks{
Expand All @@ -157,29 +157,29 @@ func StartAgentClient[L *AgentClientLocal[G], R AgentClientRemote, G any](
// We don't `defer conn.Close` here since Firecracker handles resetting active VSock connections for us
defer cancelLinkCtx(nil)

encoder := json.NewEncoder(conn)
decoder := json.NewDecoder(conn)
encoder := cbor.NewEncoder(conn)
decoder := cbor.NewDecoder(conn)

if err := registry.LinkStream(
linkCtx,

func(v rpc.Message[json.RawMessage]) error {
func(v rpc.Message[cbor.RawMessage]) error {
return encoder.Encode(v)
},
func(v *rpc.Message[json.RawMessage]) error {
func(v *rpc.Message[cbor.RawMessage]) error {
return decoder.Decode(v)
},

func(v any) (json.RawMessage, error) {
b, err := json.Marshal(v)
func(v any) (cbor.RawMessage, error) {
b, err := cbor.Marshal(v)
if err != nil {
return nil, errors.Join(ErrCouldNotMarshalJSON, err)
}

return json.RawMessage(b), nil
return cbor.RawMessage(b), nil
},
func(data json.RawMessage, v any) error {
if err := json.Unmarshal([]byte(data), v); err != nil {
func(data cbor.RawMessage, v any) error {
if err := cbor.Unmarshal([]byte(data), v); err != nil {
return errors.Join(ErrCouldNotUnmarshalJSON, err)
}

Expand Down
22 changes: 11 additions & 11 deletions pkg/ipc/agent_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package ipc

import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"os"
"sync"

"github.com/fxamacker/cbor/v2"
"github.com/loopholelabs/goroutine-manager/pkg/manager"
"github.com/pojntfx/panrpc/go/pkg/rpc"
)
Expand Down Expand Up @@ -197,7 +197,7 @@ func (agentServer *AgentServer[L, R, G]) Accept(
}
})

registry := rpc.NewRegistry[R, json.RawMessage](
registry := rpc.NewRegistry[R, cbor.RawMessage](
agentServer.agentServerLocal,

&rpc.RegistryHooks{
Expand All @@ -215,29 +215,29 @@ func (agentServer *AgentServer[L, R, G]) Accept(
// We don't `defer conn.Close` here since Firecracker handles resetting active VSock connections for us
defer cancelLinkCtx(nil)

encoder := json.NewEncoder(conn)
decoder := json.NewDecoder(conn)
encoder := cbor.NewEncoder(conn)
decoder := cbor.NewDecoder(conn)

if err := registry.LinkStream(
linkCtx,

func(v rpc.Message[json.RawMessage]) error {
func(v rpc.Message[cbor.RawMessage]) error {
return encoder.Encode(v)
},
func(v *rpc.Message[json.RawMessage]) error {
func(v *rpc.Message[cbor.RawMessage]) error {
return decoder.Decode(v)
},

func(v any) (json.RawMessage, error) {
b, err := json.Marshal(v)
func(v any) (cbor.RawMessage, error) {
b, err := cbor.Marshal(v)
if err != nil {
return nil, err
}

return json.RawMessage(b), nil
return cbor.RawMessage(b), nil
},
func(data json.RawMessage, v any) error {
return json.Unmarshal([]byte(data), v)
func(data cbor.RawMessage, v any) error {
return cbor.Unmarshal([]byte(data), v)
},

nil,
Expand Down

0 comments on commit fe11abc

Please sign in to comment.