Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Feature/internal/sync map #2109

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ replace (
github.com/ajstarks/svgo => github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b
github.com/antihax/optional => github.com/antihax/optional v1.0.0
github.com/armon/go-socks5 => github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
github.com/aws/aws-sdk-go => github.com/aws/aws-sdk-go v1.44.293
github.com/aws/aws-sdk-go => github.com/aws/aws-sdk-go v1.44.294
github.com/aws/aws-sdk-go-v2 => github.com/aws/aws-sdk-go-v2 v1.18.1
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream => github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.10
github.com/aws/aws-sdk-go-v2/config => github.com/aws/aws-sdk-go-v2/config v1.18.27
Expand Down Expand Up @@ -194,7 +194,7 @@ replace (
github.com/klauspost/cpuid/v2 => github.com/klauspost/cpuid/v2 v2.2.5
github.com/kpango/fastime => github.com/kpango/fastime v1.1.9
github.com/kpango/fuid => github.com/kpango/fuid v0.0.0-20221203053508-503b5ad89aa1
github.com/kpango/gache => github.com/kpango/gache v1.2.8
github.com/kpango/gache/v2 => github.com/kpango/gache/v2 v2.0.8
github.com/kpango/glg => github.com/kpango/glg v1.6.15
github.com/kr/fs => github.com/kr/fs v0.1.0
github.com/kr/pretty => github.com/kr/pretty v0.3.1
Expand Down Expand Up @@ -244,7 +244,7 @@ replace (
github.com/quasilyte/gogrep => github.com/quasilyte/gogrep v0.5.0
github.com/quasilyte/stdinfo => github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567
github.com/rogpeppe/fastuuid => github.com/rogpeppe/fastuuid v1.2.0
github.com/rogpeppe/go-internal => github.com/rogpeppe/go-internal v1.10.0
github.com/rogpeppe/go-internal => github.com/rogpeppe/go-internal v1.11.0
github.com/rs/xid => github.com/rs/xid v1.5.0
github.com/rs/zerolog => github.com/rs/zerolog v1.29.1
github.com/russross/blackfriday/v2 => github.com/russross/blackfriday/v2 v2.1.0
Expand Down Expand Up @@ -362,8 +362,8 @@ require (
github.com/klauspost/compress v1.15.9
github.com/kpango/fastime v1.1.9
github.com/kpango/fuid v0.0.0-00010101000000-000000000000
github.com/kpango/gache v0.0.0-00010101000000-000000000000
github.com/kpango/glg v1.6.14
github.com/kpango/gache/v2 v2.0.0-00010101000000-000000000000
github.com/kpango/glg v1.6.15
github.com/leanovate/gopter v0.0.0-00010101000000-000000000000
github.com/lucasb-eyer/go-colorful v0.0.0-00010101000000-000000000000
github.com/pierrec/lz4/v3 v3.0.0-00010101000000-000000000000
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kd
github.com/apache/arrow/go/v12 v12.0.0/go.mod h1:d+tV/eHZZ7Dz7RPrFKtPK02tpr+c9/PEd/zm8mDS9Vg=
github.com/apache/thrift v0.16.0/go.mod h1:PHK3hniurgQaNMZYaCLEqXKsYK8upmhPbmdP2FXSqgU=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/aws/aws-sdk-go v1.44.293 h1:oBPrQqsyMYe61Sl/xKVvQFflXjPwYH11aKi8QR3Nhts=
github.com/aws/aws-sdk-go v1.44.293/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go v1.44.294 h1:3x7GaEth+pDU9HwFcAU0awZlEix5CEdyIZvV08SlHa8=
github.com/aws/aws-sdk-go v1.44.294/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go-v2 v1.18.1 h1:+tefE750oAb7ZQGzla6bLkOwfcQCEtC5y2RqoqCeqKo=
github.com/aws/aws-sdk-go-v2 v1.18.1/go.mod h1:uzbQtefpm44goOPmdKyAlXSNcwlRgF3ePWVW6EtJvvw=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.10 h1:dK82zF6kkPeCo8J1e+tGx4JdvDIQzj7ygIoLg8WMuGs=
Expand Down Expand Up @@ -503,8 +503,8 @@ github.com/kpango/fastime v1.1.9 h1:xVQHcqyPt5M69DyFH7g1EPRns1YQNap9d5eLhl/Jy84=
github.com/kpango/fastime v1.1.9/go.mod h1:vyD7FnUn08zxY4b/QFBZVG+9EWMYsNl+QF0uE46urD4=
github.com/kpango/fuid v0.0.0-20221203053508-503b5ad89aa1 h1:rxyM+7uaZQ35P9fbixdnld/h4AgEhODoubuy6A4nDdk=
github.com/kpango/fuid v0.0.0-20221203053508-503b5ad89aa1/go.mod h1:CAYeq6us9NfnRkSz67/xKVIR6/vaY5ZQZRe6IVcaIKg=
github.com/kpango/gache v1.2.8 h1:+OjREOmuWO4qrJksDhzWJq80o9iwHiezdVmMR1jtCG0=
github.com/kpango/gache v1.2.8/go.mod h1:UyBo0IoPFDSJypK2haDXeV6PwHEmBcXQA0BLuOYEvWg=
github.com/kpango/gache/v2 v2.0.8 h1:lzT3xtkPTvDSPFnF6pC/x4ZDDQXbhkEFlwDx0YavQwM=
github.com/kpango/gache/v2 v2.0.8/go.mod h1:5AWVWlHau1dwI9Hzf+NZc4rPTwxM3SVwJQgob/OyAjQ=
github.com/kpango/glg v1.6.15 h1:nw0xSxpSyrDIWHeb3dvnE08PW+SCbK+aYFETT75IeLA=
github.com/kpango/glg v1.6.15/go.mod h1:cmsc7Yeu8AS3wHLmN7bhwENXOpxfq+QoqxCIk2FneRk=
github.com/kpango/go-hostpool v0.0.0-20210303030322-aab80263dcd0 h1:orIEVdc68woWO1ZyYWEVOl5Kl33eDjP+kbxgbdpMwi4=
Expand Down Expand Up @@ -604,8 +604,8 @@ github.com/rakyll/embedmd v0.0.0-20171029212350-c8060a0752a2/go.mod h1:7jOTMgqac
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
2 changes: 1 addition & 1 deletion hack/go.mod.default
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ replace (
github.com/klauspost/cpuid/v2 => github.com/klauspost/cpuid/v2 latest
github.com/kpango/fastime => github.com/kpango/fastime latest
github.com/kpango/fuid => github.com/kpango/fuid latest
github.com/kpango/gache => github.com/kpango/gache latest
github.com/kpango/gache/v2 => github.com/kpango/gache/v2 latest
github.com/kpango/glg => github.com/kpango/glg latest
github.com/kr/fs => github.com/kr/fs latest
github.com/kr/pretty => github.com/kr/pretty latest
Expand Down
16 changes: 8 additions & 8 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ import (
"github.com/vdaas/vald/internal/errors"
)

type cache struct {
type cache[V any] struct {
cacher cacher.Type
expireDur time.Duration
expireCheckDur time.Duration
expiredHook func(context.Context, string)
}

// New returns the Cache instance or error.
func New(opts ...Option) (cc cacher.Cache, err error) {
c := new(cache)
for _, opt := range append(defaultOptions, opts...) {
func New[V any](opts ...Option[V]) (cc cacher.Cache[V], err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
named return "cc" with type "cacher.Cache[V]" found (nonamedreturns)

c := new(cache[V])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
variable name 'c' is too short for the scope of its usage (varnamelen)

for _, opt := range append(defaultOptions[V](), opts...) {
opt(c)
}
switch c.cacher {
case cacher.GACHE:
return gache.New(
gache.WithExpireDuration(c.expireDur),
gache.WithExpireCheckDuration(c.expireCheckDur),
gache.WithExpiredHook(c.expiredHook),
return gache.New[V](
gache.WithExpireDuration[V](c.expireDur),
gache.WithExpireCheckDuration[V](c.expireCheckDur),
gache.WithExpiredHook[V](c.expiredHook),
), nil
default:
return nil, errors.ErrInvalidCacherType
Expand Down
22 changes: 11 additions & 11 deletions internal/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ var goleakIgnoreOptions = []goleak.Option{

func TestNew(t *testing.T) {
type args struct {
opts []Option
opts []Option[any]
}
type want struct {
wantCc cacher.Cache
wantCc cacher.Cache[any]
err error
}
type test struct {
name string
args args
want want
checkFunc func(want, cacher.Cache, error) error
checkFunc func(want, cacher.Cache[any], error) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, gotCc cacher.Cache, err error) error {
defaultCheckFunc := func(w want, gotCc cacher.Cache[any], err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
}
Expand All @@ -60,9 +60,9 @@ func TestNew(t *testing.T) {
{
name: "return gache cacher",
args: args{
opts: []Option{WithType("gache")},
opts: []Option[any]{WithType[any]("gache")},
},
checkFunc: func(w want, got cacher.Cache, err error) error {
checkFunc: func(w want, got cacher.Cache[any], err error) error {
if err != nil {
return err
}
Expand All @@ -76,7 +76,7 @@ func TestNew(t *testing.T) {
{
name: "return unknown error when type is unknown",
args: args{
opts: []Option{WithType("unknown")},
opts: []Option[any]{WithType[any]("unknown")},
},
want: want{
err: errors.ErrInvalidCacherType,
Expand All @@ -85,9 +85,9 @@ func TestNew(t *testing.T) {
{
name: "return cache when type is empty",
args: args{
opts: []Option{WithType("")},
opts: []Option[any]{WithType[any]("")},
},
checkFunc: func(w want, got cacher.Cache, err error) error {
checkFunc: func(w want, got cacher.Cache[any], err error) error {
if err != nil {
return err
}
Expand All @@ -101,7 +101,7 @@ func TestNew(t *testing.T) {
{
name: "return unknown error when type is dummy string",
args: args{
opts: []Option{WithType("dummy")},
opts: []Option[any]{WithType[any]("dummy")},
},
want: want{
err: errors.ErrInvalidCacherType,
Expand All @@ -124,7 +124,7 @@ func TestNew(t *testing.T) {
checkFunc = defaultCheckFunc
}

gotCc, err := New(test.args.opts...)
gotCc, err := New[any](test.args.opts...)
if err := checkFunc(test.want, gotCc, err); err != nil {
tt.Errorf("error = %v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/cache/cacher/cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
)

// Cache represent the cache interface to store cache.
type Cache interface {
type Cache[V any] interface {
Start(context.Context)
Get(string) (interface{}, bool)
Set(string, interface{})
Get(string) (V, bool)
Set(string, V)
Delete(string)
GetAndDelete(string) (interface{}, bool)
GetAndDelete(string) (V, bool)
}

// Type represents the cacher type. Currently it support GACHE only.
Expand Down
24 changes: 12 additions & 12 deletions internal/cache/gache/gache.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ import (
"context"
"time"

"github.com/kpango/gache"
gache "github.com/kpango/gache/v2"
"github.com/vdaas/vald/internal/cache/cacher"
)

type cache struct {
gache gache.Gache
type cache[V any] struct {
gache gache.Gache[V]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
gache is unused (structcheck)

expireDur time.Duration
expireCheckDur time.Duration
expiredHook func(context.Context, string)
}

// New loads a cache model and returns a new cache struct.
func New(opts ...Option) cacher.Cache {
c := new(cache)
for _, opt := range append(defaultOptions(), opts...) {
func New[V any](opts ...Option[V]) cacher.Cache[V] {
c := new(cache[V])
for _, opt := range append(defaultOptions[V](), opts...) {
opt(c)
}
c.gache.SetDefaultExpire(c.expireDur)
Expand All @@ -48,31 +48,31 @@ func New(opts ...Option) cacher.Cache {
}

// Start calls StartExpired func of c.gache.
func (c *cache) Start(ctx context.Context) {
func (c *cache[V]) Start(ctx context.Context) {
c.gache.StartExpired(ctx, c.expireCheckDur)
}

// Get calls StartExpired func of c.gache and returns (interface{}, bool) according to key.
func (c *cache) Get(key string) (interface{}, bool) {
func (c *cache[V]) Get(key string) (V, bool) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
Get returns interface (V) (ireturn)

return c.gache.Get(key)
}

// Set calls Set func of c.gache.
func (c *cache) Set(key string, val interface{}) {
func (c *cache[V]) Set(key string, val V) {
c.gache.Set(key, val)
}

// Delete calls Delete func of c.gache.
func (c *cache) Delete(key string) {
func (c *cache[V]) Delete(key string) {
c.gache.Delete(key)
}

// GetAndDelete returns (interface{}, bool) and delete value according to key when value of key is set.
// When value of key is not set, returns (nil, false).
func (c *cache) GetAndDelete(key string) (interface{}, bool) {
func (c *cache[V]) GetAndDelete(key string) (V, bool) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
GetAndDelete returns interface (V) (ireturn)

v, ok := c.gache.Get(key)
if !ok {
return nil, false
return *new(V), false
}
c.gache.Delete(key)
return v, true
Expand Down
Loading