Skip to content

Commit

Permalink
feat: add faster json encoder
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Nov 14, 2022
1 parent eac5c48 commit cea9291
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
3 changes: 1 addition & 2 deletions cmd/go-lnmetrics.reporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ func main() {
}
}()

// FIXME: I can remove the Plugin?
metricsPlugin = metrics.MetricsPlugin{Plugin: nil,
metricsPlugin = metrics.MetricsPlugin{
Metrics: make(map[int]metrics.Metric), Rpc: nil}

plugin, err := metrics.ConfigureCLNPlugin[*metrics.MetricsPlugin](&metricsPlugin)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (

require (
github.com/elastic/go-windows v1.0.1 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/elastic/go-sysinfo v1.8.1/go.mod h1:JfllUnzoQV/JRYymbH3dO1yggI3mV2oTK
github.com/elastic/go-windows v1.0.1 h1:AlYZOldA+UJ0/2nBuqWdo90GFCgG9xuyw9SYzGUtJm0=
github.com/elastic/go-windows v1.0.1/go.mod h1:FoVvqWSun28vaDQPbj2Elfc0JahhPB7WQEGa3c814Ss=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
Expand Down
2 changes: 2 additions & 0 deletions internal/plugin/cln4go_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"time"

"github.com/LNOpenMetrics/go-lnmetrics.reporter/internal/cache"
jsonv2 "github.com/LNOpenMetrics/go-lnmetrics.reporter/pkg/json"
"github.com/LNOpenMetrics/lnmetrics.utils/log"
"github.com/elastic/go-sysinfo"
cln4go "github.com/vincenzopalazzo/cln4go/plugin"
)

func ConfigureCLNPlugin[T MetricsPluginState](state T) (*cln4go.Plugin[T], error) {
plugin := cln4go.New(state, false, OnInit[T])
plugin.SetEncoder(&jsonv2.FastJSON{})

plugin.RegisterOption("lnmetrics-urls", "string", "", "URLs of remote servers", false)
plugin.RegisterOption("lnmetrics-noproxy", "bool", "false",
Expand Down
4 changes: 2 additions & 2 deletions internal/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import (

cron "github.com/robfig/cron/v3"
cln4go "github.com/vincenzopalazzo/cln4go/client"
"github.com/vincenzopalazzo/glightning/glightning"

"github.com/LNOpenMetrics/go-lnmetrics.reporter/internal/db"
"github.com/LNOpenMetrics/go-lnmetrics.reporter/pkg/graphql"
"github.com/LNOpenMetrics/go-lnmetrics.reporter/pkg/json"
"github.com/LNOpenMetrics/lnmetrics.utils/log"
)

// FIXME: move this to a generics to set the Client
// in this way we could support different implementation
type MetricsPlugin struct {
Plugin *glightning.Plugin
Metrics map[int]Metric
Rpc *cln4go.UnixRPC
Cron *cron.Cron
Expand All @@ -34,6 +33,7 @@ func (self *MetricsPlugin) NewClient(path string) error {
if err != nil {
return err
}
rpc.SetEncoder(&json.FastJSON{})
self.Rpc = rpc
return nil
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/json/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package json

import json "github.com/goccy/go-json"

type FastJSON struct{}

func (self *FastJSON) EncodeToByte(obj any) ([]byte, error) {
return json.Marshal(obj)
}

func (self *FastJSON) EncodeToString(obj any) (*string, error) {
jsonByte, err := json.Marshal(obj)
if err != nil {
return nil, err
}
jsonStr := string(jsonByte)
return &jsonStr, nil
}

func (self *FastJSON) DecodeFromString(jsonStr *string, obj any) error {
return json.Unmarshal([]byte(*jsonStr), &obj)
}

func (self *FastJSON) DecodeFromBytes(jsonByte []byte, obj any) error {
return json.Unmarshal(jsonByte, &obj)
}

0 comments on commit cea9291

Please sign in to comment.