Skip to content

Commit

Permalink
feat: move ledger lock inside core ledger (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored and flemzord committed May 12, 2023
1 parent 0dbea27 commit 21dbdaf
Show file tree
Hide file tree
Showing 24 changed files with 36 additions and 861 deletions.
19 changes: 0 additions & 19 deletions cmd/container.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"crypto/tls"
"net/http"

"github.com/formancehq/ledger/cmd/internal"
Expand All @@ -10,7 +9,6 @@ import (
"github.com/formancehq/ledger/pkg/api/routes"
"github.com/formancehq/ledger/pkg/bus"
"github.com/formancehq/ledger/pkg/ledger"
"github.com/formancehq/ledger/pkg/redis"
"github.com/formancehq/ledger/pkg/storage/sqlstorage"
"github.com/formancehq/stack/libs/go-libs/logging"
"github.com/formancehq/stack/libs/go-libs/otlp/otlptraces"
Expand Down Expand Up @@ -38,23 +36,6 @@ func resolveOptions(v *viper.Viper, userOptions ...fx.Option) []fx.Option {
// Handle OpenTelemetry
options = append(options, otlptraces.CLITracesModule(v))

switch v.GetString(lockStrategyFlag) {
case "redis":
var tlsConfig *tls.Config
if v.GetBool(lockStrategyRedisTLSEnabledFlag) {
tlsConfig = &tls.Config{}
if v.GetBool(lockStrategyRedisTLSInsecureFlag) {
tlsConfig.InsecureSkipVerify = true
}
}
options = append(options, redis.Module(redis.Config{
Url: v.GetString(lockStrategyRedisUrlFlag),
LockDuration: v.GetDuration(lockStrategyRedisDurationFlag),
LockRetry: v.GetDuration(lockStrategyRedisRetryFlag),
TLSConfig: tlsConfig,
}))
}

// Handle api part
options = append(options, api.Module(api.Config{
Version: Version,
Expand Down
30 changes: 0 additions & 30 deletions cmd/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/formancehq/ledger/pkg/bus"
"github.com/formancehq/ledger/pkg/core"
"github.com/formancehq/ledger/pkg/ledger"
"github.com/formancehq/ledger/pkg/redis"
"github.com/formancehq/ledger/pkg/storage"
"github.com/formancehq/stack/libs/go-libs/logging"
"github.com/formancehq/stack/libs/go-libs/otlp/otlptraces"
Expand Down Expand Up @@ -145,35 +144,6 @@ func TestContainers(t *testing.T) {
}),
},
},
{
name: "default-with-lock-strategy-memory",
init: func(v *viper.Viper) {
v.Set(lockStrategyFlag, "redis")
},
},
{
name: "default-with-lock-strategy-none",
init: func(v *viper.Viper) {
v.Set(lockStrategyFlag, "none")
},
},
{
name: "default-with-lock-strategy-redis",
init: func(v *viper.Viper) {
v.Set(lockStrategyFlag, "redis")
v.Set(lockStrategyRedisUrlFlag, "redis://redis:6789")
},
options: []fx.Option{
fx.Invoke(func(lc fx.Lifecycle, resolver *ledger.Resolver, locker ledger.Locker) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
require.IsType(t, locker, &redis.Lock{})
return nil
},
})
}),
},
},
{
name: "event-bus",
init: func(v *viper.Viper) {},
Expand Down
13 changes: 0 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"

"github.com/formancehq/ledger/cmd/internal"
"github.com/formancehq/ledger/pkg/redis"
_ "github.com/formancehq/ledger/pkg/storage/sqlstorage/ledger/migrates/9-add-pre-post-volumes"
"github.com/formancehq/stack/libs/go-libs/otlp/otlptraces"
"github.com/formancehq/stack/libs/go-libs/publish"
Expand All @@ -17,12 +16,6 @@ import (
const (
storagePostgresConnectionStringFlag = "storage.postgres.conn_string"
bindFlag = "bind"
lockStrategyFlag = "lock-strategy"
lockStrategyRedisUrlFlag = "lock-strategy-redis-url"
lockStrategyRedisDurationFlag = "lock-strategy-redis-duration"
lockStrategyRedisRetryFlag = "lock-strategy-redis-retry"
lockStrategyRedisTLSEnabledFlag = "lock-strategy-redis-tls-enabled"
lockStrategyRedisTLSInsecureFlag = "lock-strategy-redis-tls-insecure"

commitPolicyFlag = "commit-policy"

Expand Down Expand Up @@ -68,12 +61,6 @@ func NewRootCommand() *cobra.Command {
root.PersistentFlags().Bool(service.DebugFlag, false, "Debug mode")
root.PersistentFlags().String(storagePostgresConnectionStringFlag, "postgresql://localhost/postgres", "Postgre connection string")
root.PersistentFlags().String(bindFlag, "0.0.0.0:3068", "API bind address")
root.PersistentFlags().String(lockStrategyFlag, "memory", "Lock strategy (memory, none, redis)")
root.PersistentFlags().String(lockStrategyRedisUrlFlag, "", "Redis url when using redis locking strategy")
root.PersistentFlags().Duration(lockStrategyRedisDurationFlag, redis.DefaultLockDuration, "Lock duration")
root.PersistentFlags().Duration(lockStrategyRedisRetryFlag, redis.DefaultRetryInterval, "Retry lock period")
root.PersistentFlags().Bool(lockStrategyRedisTLSEnabledFlag, false, "Use tls on redis")
root.PersistentFlags().Bool(lockStrategyRedisTLSInsecureFlag, false, "Whether redis is trusted or not")
root.PersistentFlags().String(commitPolicyFlag, "", "Transaction commit policy (default or allow-past-timestamps)")

// 100 000 000 bytes is 100 MB
Expand Down
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ require (
github.com/formancehq/stack/libs/go-libs v0.0.0-20230222164357-55840b21a337
github.com/go-chi/chi/v5 v5.0.8
github.com/go-chi/cors v1.2.1
github.com/go-redis/redis/v8 v8.11.5
github.com/go-redis/redismock/v8 v8.0.6
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.3.0
github.com/jackc/pgx/v5 v5.3.0
Expand Down Expand Up @@ -51,7 +49,6 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/docker/cli v20.10.17+incompatible // indirect
github.com/docker/docker v20.10.17+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
Expand Down Expand Up @@ -94,7 +91,6 @@ require (
github.com/nats-io/nkeys v0.3.0 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/onsi/gomega v1.24.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.1.3 // indirect
Expand Down
Loading

0 comments on commit 21dbdaf

Please sign in to comment.