From fc193afe92674da716b650d89d77d2f8412c59d3 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Wed, 28 Feb 2024 03:04:38 +0000 Subject: [PATCH] refactor(daemon): switch to gob encoding --- api/api.go | 84 +++++++++++++----------------------------------- api/api_test.go | 49 ---------------------------- daemon/db/job.go | 39 ++++++++++++++++++---- 3 files changed, 54 insertions(+), 118 deletions(-) delete mode 100644 api/api_test.go diff --git a/api/api.go b/api/api.go index c1f1698..45f22c4 100644 --- a/api/api.go +++ b/api/api.go @@ -1,7 +1,8 @@ package api import ( - "encoding/json" + "bytes" + "encoding/gob" "errors" "fmt" "net" @@ -11,9 +12,7 @@ import ( "code.dumpstack.io/tools/out-of-tree/artifact" "code.dumpstack.io/tools/out-of-tree/distro" - "github.com/davecgh/go-spew/spew" "github.com/google/uuid" - "github.com/rs/zerolog/log" ) var ErrInvalid = errors.New("") @@ -106,9 +105,12 @@ type Req struct { Data []byte } -func (r *Req) SetData(data any) { +func (r *Req) SetData(data any) (err error) { r.Type = fmt.Sprintf("%v", reflect.TypeOf(data)) - r.Data = Marshal(data) + var buf bytes.Buffer + err = gob.NewEncoder(&buf).Encode(data) + r.Data = buf.Bytes() + return } func (r *Req) GetData(data any) (err error) { @@ -122,32 +124,16 @@ func (r *Req) GetData(data any) (err error) { return } - log.Trace().Msgf("unmarshal %v", string(r.Data)) - err = json.Unmarshal(r.Data, &data) - return + buf := bytes.NewBuffer(r.Data) + return gob.NewDecoder(buf).Decode(data) } -func (r Req) Encode(conn net.Conn) { - log.Debug().Msgf("encode %v", r.Command) - err := json.NewEncoder(conn).Encode(&r) - if err != nil { - log.Fatal().Msgf("encode %v", r.Command) - } +func (r *Req) Encode(conn net.Conn) (err error) { + return gob.NewEncoder(conn).Encode(r) } func (r *Req) Decode(conn net.Conn) (err error) { - err = json.NewDecoder(conn).Decode(r) - return -} - -func (r Req) Marshal() (bytes []byte) { - return Marshal(r) -} - -func (Req) Unmarshal(data []byte) (r Req, err error) { - err = json.Unmarshal(data, &r) - log.Trace().Msgf("unmarshal %v", spew.Sdump(r)) - return + return gob.NewDecoder(conn).Decode(r) } type Resp struct { @@ -166,9 +152,12 @@ func NewResp() (resp Resp) { return } -func (r *Resp) SetData(data any) { +func (r *Resp) SetData(data any) (err error) { r.Type = fmt.Sprintf("%v", reflect.TypeOf(data)) - r.Data = Marshal(data) + var buf bytes.Buffer + err = gob.NewEncoder(&buf).Encode(data) + r.Data = buf.Bytes() + return } func (r *Resp) GetData(data any) (err error) { @@ -182,48 +171,19 @@ func (r *Resp) GetData(data any) (err error) { return } - log.Trace().Msgf("unmarshal %v", string(r.Data)) - err = json.Unmarshal(r.Data, &data) - return + buf := bytes.NewBuffer(r.Data) + return gob.NewDecoder(buf).Decode(data) } -func (r *Resp) Encode(conn net.Conn) { +func (r *Resp) Encode(conn net.Conn) (err error) { if r.Err != nil && r.Err != ErrInvalid && r.Error == "" { r.Error = fmt.Sprintf("%v", r.Err) } - log.Debug().Msgf("encode %v", r.UUID) - err := json.NewEncoder(conn).Encode(r) - if err != nil { - log.Fatal().Msgf("encode %v", r.UUID) - } + return gob.NewEncoder(conn).Encode(r) } func (r *Resp) Decode(conn net.Conn) (err error) { - err = json.NewDecoder(conn).Decode(r) + err = gob.NewDecoder(conn).Decode(r) r.Err = ErrInvalid return } - -func (r *Resp) Marshal() (bytes []byte) { - if r.Err != nil && r.Err != ErrInvalid && r.Error == "" { - r.Error = fmt.Sprintf("%v", r.Err) - } - - return Marshal(r) -} - -func (Resp) Unmarshal(data []byte) (r Resp, err error) { - err = json.Unmarshal(data, &r) - log.Trace().Msgf("unmarshal %v", spew.Sdump(r)) - r.Err = ErrInvalid - return -} - -func Marshal(data any) (bytes []byte) { - bytes, err := json.Marshal(data) - if err != nil { - log.Fatal().Err(err).Msgf("marshal %v", data) - } - log.Trace().Msgf("marshal %v", string(bytes)) - return -} diff --git a/api/api_test.go b/api/api_test.go deleted file mode 100644 index 544a5ae..0000000 --- a/api/api_test.go +++ /dev/null @@ -1,49 +0,0 @@ -package api - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestReq(t *testing.T) { - req := Req{} - - req.Command = ListRepos - req.SetData(&Job{ID: 999, RepoName: "test"}) - - bytes := req.Marshal() - - req2, err := Req{}.Unmarshal(bytes) - assert.Nil(t, err) - - assert.Equal(t, req, req2) - - job := Job{} - err = req2.GetData(&job) - assert.Nil(t, err) - - assert.Equal(t, req2.Type, "*api.Job") -} - -func TestResp(t *testing.T) { - resp := Resp{} - - resp.Error = "abracadabra" - resp.SetData(&[]Repo{{}, {}}) - - bytes := resp.Marshal() - - resp2, err := Resp{}.Unmarshal(bytes) - assert.Nil(t, err) - - resp2.Err = nil // non-marshallable - - assert.Equal(t, resp, resp2) - - var repos []Repo - err = resp2.GetData(&repos) - assert.Nil(t, err) - - assert.Equal(t, resp2.Type, "*[]api.Repo") -} diff --git a/daemon/db/job.go b/daemon/db/job.go index 28e7d9d..7e5dd8d 100644 --- a/daemon/db/job.go +++ b/daemon/db/job.go @@ -1,8 +1,9 @@ package db import ( + "bytes" "database/sql" - "encoding/json" + "encoding/gob" "time" "code.dumpstack.io/tools/out-of-tree/api" @@ -37,8 +38,19 @@ func AddJob(db *sql.DB, job *api.Job) (err error) { defer stmt.Close() - config := api.Marshal(job.Artifact) - target := api.Marshal(job.Target) + var abuf bytes.Buffer + err = gob.NewEncoder(&abuf).Encode(job.Artifact) + if err != nil { + return + } + config := abuf.Bytes() + + var tbuf bytes.Buffer + err = gob.NewEncoder(&tbuf).Encode(job.Target) + if err != nil { + return + } + target := tbuf.Bytes() res, err := stmt.Exec(time.Now().Unix(), job.UUID, job.Group, job.RepoName, job.Commit, config, target, @@ -65,8 +77,19 @@ func UpdateJob(db *sql.DB, job *api.Job) (err error) { } defer stmt.Close() - config := api.Marshal(job.Artifact) - target := api.Marshal(job.Target) + var abuf bytes.Buffer + err = gob.NewEncoder(&abuf).Encode(job.Artifact) + if err != nil { + return + } + config := abuf.Bytes() + + var tbuf bytes.Buffer + err = gob.NewEncoder(&tbuf).Encode(job.Target) + if err != nil { + return + } + target := tbuf.Bytes() _, err = stmt.Exec(time.Now().Unix(), job.UUID, job.Group, job.RepoName, job.Commit, @@ -86,12 +109,14 @@ func scanJob(scan func(dest ...any) error) (job api.Job, err error) { return } - err = json.Unmarshal(config, &job.Artifact) + abuf := bytes.NewBuffer(config) + err = gob.NewDecoder(abuf).Decode(&job.Artifact) if err != nil { return } - err = json.Unmarshal(target, &job.Target) + tbuf := bytes.NewBuffer(target) + err = gob.NewDecoder(tbuf).Decode(&job.Target) if err != nil { return }