Skip to content

Commit

Permalink
feat(#234): switch to go-autumn-sync in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanHCB committed Nov 22, 2023
1 parent 2c1789e commit 45c44fa
Show file tree
Hide file tree
Showing 20 changed files with 292 additions and 372 deletions.
12 changes: 8 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/Interhyp/metadata-service

go 1.21
go 1.21.4

// exclude actually unused dependencies (mostly of pact-go, which is testing only anyway)
// because our scanner fails to understand they are not in use
Expand Down Expand Up @@ -80,6 +80,8 @@ require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/Roshick/go-autumn-kafka v0.3.1 // indirect
github.com/Roshick/go-autumn-synchronisation v0.2.0 // indirect
github.com/StephanHCB/go-autumn-acorn-registry v0.3.1 // indirect
github.com/StephanHCB/go-autumn-web-swagger-ui v0.2.3 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
Expand All @@ -90,6 +92,7 @@ require (
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/elastic/go-sysinfo v1.7.1 // indirect
github.com/elastic/go-windows v1.0.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
Expand All @@ -116,6 +119,7 @@ require (
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/redis/go-redis/v9 v9.3.0 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
Expand All @@ -129,10 +133,10 @@ require (
go.elastic.co/apm/module/apmhttp/v2 v2.4.5 // indirect
go.elastic.co/apm/v2 v2.4.5 // indirect
go.elastic.co/fastjson v1.1.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 h1:kkhsdkhsCvIsutKu5zLMgWtgh9YxGCNAw8Ad8hjwfYg=
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/Roshick/go-autumn-kafka v0.3.1 h1:wnIxUEM/2nzjUsjonrF19sYuBiBYl7L6SnuRegMwZtU=
github.com/Roshick/go-autumn-kafka v0.3.1/go.mod h1:D0MfJsHTD+2qgebdnu2BFG5BKrWrLpz20kEAQMz6g28=
github.com/Roshick/go-autumn-synchronisation v0.2.0 h1:K+uKM33SWWrBaJcCbVQ3kySmcHacXOPdTbJsaXDkoZ4=
github.com/Roshick/go-autumn-synchronisation v0.2.0/go.mod h1:Escd6Ri5m/ppyFilE2C9DcVB3A6EzqO7JOFz8p8WThQ=
github.com/StephanHCB/go-autumn-acorn-registry v0.3.1 h1:rAJlEsrSTJArQZHOt4Q6Gkc4NgL2ObSQGvxW0chiRiM=
github.com/StephanHCB/go-autumn-acorn-registry v0.3.1/go.mod h1:KB7wPWOEy2n8VGNw75H4w7wBSWSrgwNNJNmet/F+9RI=
github.com/StephanHCB/go-autumn-config-api v0.2.1 h1:t2EeTsdFpLM2xH2T7QFQtbFYI8hG5I9S+Q2o3KT6mlk=
Expand Down Expand Up @@ -53,6 +57,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/elastic/go-sysinfo v1.7.1 h1:Wx4DSARcKLllpKT2TnFVdSUJOsybqMYCNQZq1/wO+s0=
github.com/elastic/go-sysinfo v1.7.1/go.mod h1:i1ZYdU10oLNfRzq4vq62BEwD2fH8KaWh6eh0ikPT9F0=
github.com/elastic/go-windows v1.0.0/go.mod h1:TsU0Nrp7/y3+VwE82FoZF8gC/XFg/Elz6CcloAxnPgU=
Expand Down Expand Up @@ -151,6 +157,8 @@ github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO
github.com/prometheus/procfs v0.0.0-20190425082905-87a4384529e0/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI=
github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY=
github.com/redis/go-redis/v9 v9.3.0 h1:RiVDjmig62jIWp7Kk4XVLs0hzV6pI3PyTnnL0cnn0u0=
github.com/redis/go-redis/v9 v9.3.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
Expand Down Expand Up @@ -210,6 +218,8 @@ golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2Uz
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
Expand All @@ -222,6 +232,8 @@ golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -251,6 +263,8 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
Expand All @@ -259,6 +273,7 @@ golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/term v0.14.0 h1:LGK9IlZ8T9jvdy6cTdfKUCltatMFOehAQo9SRC46UQ8=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand All @@ -268,6 +283,7 @@ golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200509030707-2212a7e161a5/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
Expand Down
30 changes: 15 additions & 15 deletions internal/acorn/service/cacheint.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ type Cache interface {
// --- owner cache ---

// SetOwnerListTimestamp lets you set or update the timestamp for the last full scan of the list of aliases.
SetOwnerListTimestamp(ctx context.Context, timestamp string)
SetOwnerListTimestamp(ctx context.Context, timestamp string) error

// GetOwnerListTimestamp gives you the timestamp of the last full scan of the list of aliases.
GetOwnerListTimestamp(ctx context.Context) string
GetOwnerListTimestamp(ctx context.Context) (string, error)

// GetSortedOwnerAliases gives you a time snapshot copy of the slice of sorted owner names.
//
// This means you won't mess up the cache if you work with it in any way.
GetSortedOwnerAliases(ctx context.Context) []string
GetSortedOwnerAliases(ctx context.Context) ([]string, error)

// GetOwner gives you a time snapshot deep copy of the owner information.
//
Expand All @@ -34,25 +34,25 @@ type Cache interface {
// PutOwner creates or replaces the owner cache entry.
//
// This is an atomic operation.
PutOwner(ctx context.Context, alias string, entry openapi.OwnerDto)
PutOwner(ctx context.Context, alias string, entry openapi.OwnerDto) error

// DeleteOwner deletes the owner cache entry.
//
// This is an atomic operation.
DeleteOwner(ctx context.Context, alias string)
DeleteOwner(ctx context.Context, alias string) error

// --- service cache ---

// SetServiceListTimestamp lets you set or update the timestamp for the last full scan of the list of names.
SetServiceListTimestamp(ctx context.Context, timestamp string)
SetServiceListTimestamp(ctx context.Context, timestamp string) error

// GetServiceListTimestamp gives you the timestamp of the last full scan of the list of names.
GetServiceListTimestamp(ctx context.Context) string
GetServiceListTimestamp(ctx context.Context) (string, error)

// GetSortedServiceNames gives you a time snapshot copy of the slice of sorted service names.
//
// This means you won't mess up the cache if you work with it in any way.
GetSortedServiceNames(ctx context.Context) []string
GetSortedServiceNames(ctx context.Context) ([]string, error)

// GetService gives you a time snapshot deep copy of the service information.
//
Expand All @@ -64,25 +64,25 @@ type Cache interface {
// PutService creates or replaces the service cache entry.
//
// This is an atomic operation.
PutService(ctx context.Context, name string, entry openapi.ServiceDto)
PutService(ctx context.Context, name string, entry openapi.ServiceDto) error

// DeleteService deletes the service cache entry.
//
// This is an atomic operation.
DeleteService(ctx context.Context, name string)
DeleteService(ctx context.Context, name string) error

// --- repository cache ---

// SetRepositoryListTimestamp lets you set or update the timestamp for the last full scan of the list of keys.
SetRepositoryListTimestamp(ctx context.Context, timestamp string)
SetRepositoryListTimestamp(ctx context.Context, timestamp string) error

// GetRepositoryListTimestamp gives you the timestamp of the last full scan of the list of keys.
GetRepositoryListTimestamp(ctx context.Context) string
GetRepositoryListTimestamp(ctx context.Context) (string, error)

// GetSortedRepositoryKeys gives you a time snapshot copy of the slice of sorted repository names.
//
// This means you won't mess up the cache if you work with it in any way.
GetSortedRepositoryKeys(ctx context.Context) []string
GetSortedRepositoryKeys(ctx context.Context) ([]string, error)

// GetRepository gives you a time snapshot deep copy of the repository information.
//
Expand All @@ -94,10 +94,10 @@ type Cache interface {
// PutRepository creates or replaces the repository cache entry.
//
// This is an atomic operation.
PutRepository(ctx context.Context, key string, entry openapi.RepositoryDto)
PutRepository(ctx context.Context, key string, entry openapi.RepositoryDto) error

// DeleteRepository deletes the repository cache entry.
//
// This is an atomic operation.
DeleteRepository(ctx context.Context, key string)
DeleteRepository(ctx context.Context, key string) error
}
2 changes: 1 addition & 1 deletion internal/acorn/service/updaterint.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@ type Updater interface {
// CanMoveOrDeleteRepository checks that no service still references the repository key.
//
// Expects a current cache and you must be holding the lock.
CanMoveOrDeleteRepository(ctx context.Context, key string) bool
CanMoveOrDeleteRepository(ctx context.Context, key string) (bool, error)
}
24 changes: 17 additions & 7 deletions internal/service/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ package cache

import (
"context"
openapi "github.com/Interhyp/metadata-service/api"
"github.com/Interhyp/metadata-service/internal/acorn/service"
"github.com/Interhyp/metadata-service/internal/service/cache/cacheable"
libcache "github.com/Roshick/go-autumn-synchronisation/pkg/aucache"
auzerolog "github.com/StephanHCB/go-autumn-logging-zerolog"
librepo "github.com/StephanHCB/go-backend-service-common/acorns/repository"
"time"
)

var cacheRetention = 30 * 24 * time.Hour

type Impl struct {
Configuration librepo.Configuration
Logging librepo.Logging
Timestamp librepo.Timestamp

OwnerCache cacheable.Cacheable
ServiceCache cacheable.Cacheable
RepositoryCache cacheable.Cacheable
OwnerCache libcache.Cache[openapi.OwnerDto]
ServiceCache libcache.Cache[openapi.ServiceDto]
RepositoryCache libcache.Cache[openapi.RepositoryDto]
TimestampCache libcache.Cache[string]
}

func New(
Expand Down Expand Up @@ -47,15 +52,20 @@ func (s *Impl) Setup() error {
}

func (s *Impl) SetupCache(_ context.Context) error {
// TODO create redis instances based on configuration

// idempotent to allow mocking
if s.OwnerCache == nil {
s.OwnerCache = cacheable.New()
s.OwnerCache = libcache.NewMemoryCache[openapi.OwnerDto]()
}
if s.ServiceCache == nil {
s.ServiceCache = cacheable.New()
s.ServiceCache = libcache.NewMemoryCache[openapi.ServiceDto]()
}
if s.RepositoryCache == nil {
s.RepositoryCache = cacheable.New()
s.RepositoryCache = libcache.NewMemoryCache[openapi.RepositoryDto]()
}
if s.TimestampCache == nil {
s.TimestampCache = libcache.NewMemoryCache[string]()
}
return nil
}
82 changes: 0 additions & 82 deletions internal/service/cache/cacheable/cacheable.go

This file was deleted.

20 changes: 0 additions & 20 deletions internal/service/cache/cacheable/cacheableint.go

This file was deleted.

Loading

0 comments on commit 45c44fa

Please sign in to comment.