From b734f1a314d1f73a2f77f2bd3396bd91bdeb6801 Mon Sep 17 00:00:00 2001 From: ascandone Date: Wed, 16 Oct 2024 10:51:04 +0200 Subject: [PATCH] refactor: removed compiler.go --- internal/controller/ledger/compiler.go | 75 ------------------- .../ledger/compiler_generated_test.go | 51 ------------- .../controller/ledger/numscript_parser.go | 63 +++++++++++++--- internal/controller/system/controller.go | 10 +-- internal/controller/system/module.go | 11 +-- 5 files changed, 65 insertions(+), 145 deletions(-) delete mode 100644 internal/controller/ledger/compiler.go delete mode 100644 internal/controller/ledger/compiler_generated_test.go diff --git a/internal/controller/ledger/compiler.go b/internal/controller/ledger/compiler.go deleted file mode 100644 index cb63d1e09..000000000 --- a/internal/controller/ledger/compiler.go +++ /dev/null @@ -1,75 +0,0 @@ -package ledger - -import ( - "crypto/sha256" - "encoding/base64" - - "github.com/bluele/gcache" - "github.com/formancehq/ledger/internal/machine/script/compiler" - "github.com/formancehq/ledger/internal/machine/vm/program" -) - -//go:generate mockgen -write_source_comment=false -write_package_comment=false -source compiler.go -destination compiler_generated_test.go -package ledger . Compiler - -// Compiler can return following errors: -// - ErrCompilationFailed -type Compiler interface { - Compile(script string) (*program.Program, error) -} -type CompilerFn func(script string) (*program.Program, error) - -func (fn CompilerFn) Compile(script string) (*program.Program, error) { - return fn(script) -} - -func NewDefaultCompiler() CompilerFn { - return func(script string) (*program.Program, error) { - ret, err := compiler.Compile(script) - if err != nil { - return nil, newErrCompilationFailed(err) - } - return ret, nil - } -} - -type CacheConfiguration struct { - MaxCount uint -} - -type CachedCompiler struct { - underlying Compiler - cache gcache.Cache -} - -func (c *CachedCompiler) Compile(script string) (*program.Program, error) { - - digest := sha256.New() - _, err := digest.Write([]byte(script)) - if err != nil { - return nil, err - } - - cacheKey := base64.StdEncoding.EncodeToString(digest.Sum(nil)) - v, err := c.cache.Get(cacheKey) - if err == nil { - return v.(*program.Program), nil - } - - program, err := c.underlying.Compile(script) - if err != nil { - return nil, err - } - - _ = c.cache.Set(cacheKey, program) - - return program, nil -} - -func NewCachedCompiler(compiler Compiler, configuration CacheConfiguration) *CachedCompiler { - return &CachedCompiler{ - underlying: compiler, - cache: gcache.New(int(configuration.MaxCount)).LFU().Build(), - } -} - -var _ Compiler = (*CachedCompiler)(nil) diff --git a/internal/controller/ledger/compiler_generated_test.go b/internal/controller/ledger/compiler_generated_test.go deleted file mode 100644 index a6632141e..000000000 --- a/internal/controller/ledger/compiler_generated_test.go +++ /dev/null @@ -1,51 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// -// Generated by this command: -// -// mockgen -write_source_comment=false -write_package_comment=false -source compiler.go -destination compiler_generated_test.go -package ledger . Compiler -package ledger - -import ( - reflect "reflect" - - program "github.com/formancehq/ledger/internal/machine/vm/program" - gomock "go.uber.org/mock/gomock" -) - -// MockCompiler is a mock of Compiler interface. -type MockCompiler struct { - ctrl *gomock.Controller - recorder *MockCompilerMockRecorder -} - -// MockCompilerMockRecorder is the mock recorder for MockCompiler. -type MockCompilerMockRecorder struct { - mock *MockCompiler -} - -// NewMockCompiler creates a new mock instance. -func NewMockCompiler(ctrl *gomock.Controller) *MockCompiler { - mock := &MockCompiler{ctrl: ctrl} - mock.recorder = &MockCompilerMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockCompiler) EXPECT() *MockCompilerMockRecorder { - return m.recorder -} - -// Compile mocks base method. -func (m *MockCompiler) Compile(script string) (*program.Program, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Compile", script) - ret0, _ := ret[0].(*program.Program) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Compile indicates an expected call of Compile. -func (mr *MockCompilerMockRecorder) Compile(script any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Compile", reflect.TypeOf((*MockCompiler)(nil).Compile), script) -} diff --git a/internal/controller/ledger/numscript_parser.go b/internal/controller/ledger/numscript_parser.go index 8b247d585..a06d2e081 100644 --- a/internal/controller/ledger/numscript_parser.go +++ b/internal/controller/ledger/numscript_parser.go @@ -1,5 +1,13 @@ package ledger +import ( + "crypto/sha256" + "encoding/base64" + + "github.com/bluele/gcache" + "github.com/formancehq/ledger/internal/machine/script/compiler" +) + //go:generate mockgen -write_source_comment=false -write_package_comment=false -source numscript_parser.go -destination numscript_parser_generated_test.go -package ledger . NumscriptParser type NumscriptParser interface { @@ -8,22 +16,59 @@ type NumscriptParser interface { Parse(script string) (NumscriptRuntime, error) } -type DefaultNumscriptParser struct { - compiler Compiler -} +type DefaultNumscriptParser struct{} func (d *DefaultNumscriptParser) Parse(script string) (NumscriptRuntime, error) { - ret, err := d.compiler.Compile(script) + ret, err := compiler.Compile(script) if err != nil { - return nil, err + return nil, newErrCompilationFailed(err) } return NewMachineNumscriptRuntimeAdapter(*ret), nil } -func NewDefaultNumscriptParser(compiler Compiler) *DefaultNumscriptParser { - return &DefaultNumscriptParser{ - compiler: compiler, - } +func NewDefaultNumscriptParser() *DefaultNumscriptParser { + return &DefaultNumscriptParser{} } var _ NumscriptParser = (*DefaultNumscriptParser)(nil) + +type CacheConfiguration struct { + MaxCount uint +} + +type CachedParser struct { + underlying NumscriptParser + cache gcache.Cache +} + +func (c *CachedParser) Parse(script string) (NumscriptRuntime, error) { + digest := sha256.New() + _, err := digest.Write([]byte(script)) + if err != nil { + return nil, err + } + + cacheKey := base64.StdEncoding.EncodeToString(digest.Sum(nil)) + v, err := c.cache.Get(cacheKey) + if err == nil { + return v.(NumscriptRuntime), nil + } + + program, err := c.underlying.Parse(script) + if err != nil { + return nil, err + } + + _ = c.cache.Set(cacheKey, program) + + return program, nil +} + +func NewCachedNumscriptParser(parser NumscriptParser, configuration CacheConfiguration) *CachedParser { + return &CachedParser{ + underlying: parser, + cache: gcache.New(int(configuration.MaxCount)).LFU().Build(), + } +} + +var _ NumscriptParser = (*CachedParser)(nil) diff --git a/internal/controller/system/controller.go b/internal/controller/system/controller.go index 7a2a87c60..53ff6c978 100644 --- a/internal/controller/system/controller.go +++ b/internal/controller/system/controller.go @@ -32,7 +32,7 @@ type Controller interface { type DefaultController struct { store Store listener ledgercontroller.Listener - compiler ledgercontroller.Compiler + parser ledgercontroller.NumscriptParser registry *ledgercontroller.StateRegistry databaseRetryConfiguration DatabaseRetryConfiguration @@ -50,7 +50,7 @@ func (ctrl *DefaultController) GetLedgerController(ctx context.Context, name str var ledgerController ledgercontroller.Controller = ledgercontroller.NewDefaultController( *l, store, - ledgercontroller.NewDefaultNumscriptParser(ctrl.compiler), + ledgercontroller.NewDefaultNumscriptParser(), ledgercontroller.WithMeter(ctrl.meter), ) @@ -132,9 +132,9 @@ func NewDefaultController(store Store, listener ledgercontroller.Listener, opts type Option func(ctrl *DefaultController) -func WithCompiler(compiler ledgercontroller.Compiler) Option { +func WithParser(parser ledgercontroller.NumscriptParser) Option { return func(ctrl *DefaultController) { - ctrl.compiler = compiler + ctrl.parser = parser } } @@ -157,7 +157,7 @@ func WithTracer(t trace.Tracer) Option { } var defaultOptions = []Option{ - WithCompiler(ledgercontroller.NewDefaultCompiler()), + WithParser(ledgercontroller.NewDefaultNumscriptParser()), WithMeter(noopmetrics.Meter{}), WithTracer(nooptracer.Tracer{}), } diff --git a/internal/controller/system/module.go b/internal/controller/system/module.go index 286a9c15c..31bb01299 100644 --- a/internal/controller/system/module.go +++ b/internal/controller/system/module.go @@ -1,20 +1,21 @@ package system import ( + "time" + ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/trace" "go.uber.org/fx" - "time" ) type DatabaseRetryConfiguration struct { MaxRetry int - Delay time.Duration + Delay time.Duration } type ModuleConfiguration struct { - NSCacheConfiguration ledgercontroller.CacheConfiguration + NSCacheConfiguration ledgercontroller.CacheConfiguration DatabaseRetryConfiguration DatabaseRetryConfiguration } @@ -31,8 +32,8 @@ func NewFXModule(configuration ModuleConfiguration) fx.Option { ) *DefaultController { options := make([]Option, 0) if configuration.NSCacheConfiguration.MaxCount != 0 { - options = append(options, WithCompiler(ledgercontroller.NewCachedCompiler( - ledgercontroller.NewDefaultCompiler(), + options = append(options, WithParser(ledgercontroller.NewCachedNumscriptParser( + ledgercontroller.NewDefaultNumscriptParser(), configuration.NSCacheConfiguration, ))) }