From d1b303fe7eef14cb07ce4400b2132ffd6af8646a Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 29 Sep 2020 17:25:49 +0800 Subject: [PATCH] export cache package, add client interceptor customization --- core/stores/{internal => cache}/cache.go | 2 +- core/stores/{internal => cache}/cache_test.go | 2 +- core/stores/cache/cacheconf.go | 4 +-- core/stores/{internal => cache}/cachenode.go | 2 +- .../{internal => cache}/cachenode_test.go | 2 +- core/stores/cache/cacheopt.go | 36 +++++++++++++++---- core/stores/{internal => cache}/cachestat.go | 2 +- core/stores/{internal => cache}/cleaner.go | 2 +- core/stores/{internal => cache}/config.go | 2 +- core/stores/{internal => cache}/util.go | 2 +- core/stores/internal/cacheopt.go | 33 ----------------- core/stores/kv/config.go | 6 ++-- core/stores/kv/store.go | 4 +-- core/stores/kv/store_test.go | 4 +-- core/stores/mongoc/cachedcollection.go | 8 ++--- core/stores/mongoc/cachedcollection_test.go | 10 +++--- core/stores/mongoc/cachedmodel.go | 9 +++-- core/stores/sqlc/cachedsql.go | 9 +++-- zrpc/client.go | 10 +++--- 19 files changed, 70 insertions(+), 79 deletions(-) rename core/stores/{internal => cache}/cache.go (99%) rename core/stores/{internal => cache}/cache_test.go (99%) rename core/stores/{internal => cache}/cachenode.go (99%) rename core/stores/{internal => cache}/cachenode_test.go (99%) rename core/stores/{internal => cache}/cachestat.go (98%) rename core/stores/{internal => cache}/cleaner.go (98%) rename core/stores/{internal => cache}/config.go (90%) rename core/stores/{internal => cache}/util.go (94%) delete mode 100644 core/stores/internal/cacheopt.go diff --git a/core/stores/internal/cache.go b/core/stores/cache/cache.go similarity index 99% rename from core/stores/internal/cache.go rename to core/stores/cache/cache.go index fee65456..27c9958c 100644 --- a/core/stores/internal/cache.go +++ b/core/stores/cache/cache.go @@ -1,4 +1,4 @@ -package internal +package cache import ( "fmt" diff --git a/core/stores/internal/cache_test.go b/core/stores/cache/cache_test.go similarity index 99% rename from core/stores/internal/cache_test.go rename to core/stores/cache/cache_test.go index e5050f2a..3bc5e291 100644 --- a/core/stores/internal/cache_test.go +++ b/core/stores/cache/cache_test.go @@ -1,4 +1,4 @@ -package internal +package cache import ( "encoding/json" diff --git a/core/stores/cache/cacheconf.go b/core/stores/cache/cacheconf.go index a7ff98cb..36ba1171 100644 --- a/core/stores/cache/cacheconf.go +++ b/core/stores/cache/cacheconf.go @@ -1,5 +1,3 @@ package cache -import "github.com/tal-tech/go-zero/core/stores/internal" - -type CacheConf = internal.ClusterConf +type CacheConf = ClusterConf diff --git a/core/stores/internal/cachenode.go b/core/stores/cache/cachenode.go similarity index 99% rename from core/stores/internal/cachenode.go rename to core/stores/cache/cachenode.go index 723a20fd..b456879a 100644 --- a/core/stores/internal/cachenode.go +++ b/core/stores/cache/cachenode.go @@ -1,4 +1,4 @@ -package internal +package cache import ( "encoding/json" diff --git a/core/stores/internal/cachenode_test.go b/core/stores/cache/cachenode_test.go similarity index 99% rename from core/stores/internal/cachenode_test.go rename to core/stores/cache/cachenode_test.go index 72dd128e..b144568a 100644 --- a/core/stores/internal/cachenode_test.go +++ b/core/stores/cache/cachenode_test.go @@ -1,4 +1,4 @@ -package internal +package cache import ( "errors" diff --git a/core/stores/cache/cacheopt.go b/core/stores/cache/cacheopt.go index 0e8db961..9ceff415 100644 --- a/core/stores/cache/cacheopt.go +++ b/core/stores/cache/cacheopt.go @@ -1,21 +1,45 @@ package cache -import ( - "time" +import "time" - "github.com/tal-tech/go-zero/core/stores/internal" +const ( + defaultExpiry = time.Hour * 24 * 7 + defaultNotFoundExpiry = time.Minute ) -type Option = internal.Option +type ( + Options struct { + Expiry time.Duration + NotFoundExpiry time.Duration + } + + Option func(o *Options) +) + +func newOptions(opts ...Option) Options { + var o Options + for _, opt := range opts { + opt(&o) + } + + if o.Expiry <= 0 { + o.Expiry = defaultExpiry + } + if o.NotFoundExpiry <= 0 { + o.NotFoundExpiry = defaultNotFoundExpiry + } + + return o +} func WithExpiry(expiry time.Duration) Option { - return func(o *internal.Options) { + return func(o *Options) { o.Expiry = expiry } } func WithNotFoundExpiry(expiry time.Duration) Option { - return func(o *internal.Options) { + return func(o *Options) { o.NotFoundExpiry = expiry } } diff --git a/core/stores/internal/cachestat.go b/core/stores/cache/cachestat.go similarity index 98% rename from core/stores/internal/cachestat.go rename to core/stores/cache/cachestat.go index ff2b3d78..a089f8fe 100644 --- a/core/stores/internal/cachestat.go +++ b/core/stores/cache/cachestat.go @@ -1,4 +1,4 @@ -package internal +package cache import ( "sync/atomic" diff --git a/core/stores/internal/cleaner.go b/core/stores/cache/cleaner.go similarity index 98% rename from core/stores/internal/cleaner.go rename to core/stores/cache/cleaner.go index 5d4b8656..54798f13 100644 --- a/core/stores/internal/cleaner.go +++ b/core/stores/cache/cleaner.go @@ -1,4 +1,4 @@ -package internal +package cache import ( "fmt" diff --git a/core/stores/internal/config.go b/core/stores/cache/config.go similarity index 90% rename from core/stores/internal/config.go rename to core/stores/cache/config.go index eb440c97..a5466a07 100644 --- a/core/stores/internal/config.go +++ b/core/stores/cache/config.go @@ -1,4 +1,4 @@ -package internal +package cache import "github.com/tal-tech/go-zero/core/stores/redis" diff --git a/core/stores/internal/util.go b/core/stores/cache/util.go similarity index 94% rename from core/stores/internal/util.go rename to core/stores/cache/util.go index bf77e81a..390d20cc 100644 --- a/core/stores/internal/util.go +++ b/core/stores/cache/util.go @@ -1,4 +1,4 @@ -package internal +package cache import "strings" diff --git a/core/stores/internal/cacheopt.go b/core/stores/internal/cacheopt.go deleted file mode 100644 index 908c0bc0..00000000 --- a/core/stores/internal/cacheopt.go +++ /dev/null @@ -1,33 +0,0 @@ -package internal - -import "time" - -const ( - defaultExpiry = time.Hour * 24 * 7 - defaultNotFoundExpiry = time.Minute -) - -type ( - Options struct { - Expiry time.Duration - NotFoundExpiry time.Duration - } - - Option func(o *Options) -) - -func newOptions(opts ...Option) Options { - var o Options - for _, opt := range opts { - opt(&o) - } - - if o.Expiry <= 0 { - o.Expiry = defaultExpiry - } - if o.NotFoundExpiry <= 0 { - o.NotFoundExpiry = defaultNotFoundExpiry - } - - return o -} diff --git a/core/stores/kv/config.go b/core/stores/kv/config.go index 0fb4cc76..dc86ee9f 100644 --- a/core/stores/kv/config.go +++ b/core/stores/kv/config.go @@ -1,5 +1,7 @@ package kv -import "github.com/tal-tech/go-zero/core/stores/internal" +import ( + "github.com/tal-tech/go-zero/core/stores/cache" +) -type KvConf = internal.ClusterConf +type KvConf = cache.ClusterConf diff --git a/core/stores/kv/store.go b/core/stores/kv/store.go index 95570b96..debed1d6 100644 --- a/core/stores/kv/store.go +++ b/core/stores/kv/store.go @@ -6,7 +6,7 @@ import ( "github.com/tal-tech/go-zero/core/errorx" "github.com/tal-tech/go-zero/core/hash" - "github.com/tal-tech/go-zero/core/stores/internal" + "github.com/tal-tech/go-zero/core/stores/cache" "github.com/tal-tech/go-zero/core/stores/redis" ) @@ -81,7 +81,7 @@ type ( ) func NewStore(c KvConf) Store { - if len(c) == 0 || internal.TotalWeights(c) <= 0 { + if len(c) == 0 || cache.TotalWeights(c) <= 0 { log.Fatal("no cache nodes") } diff --git a/core/stores/kv/store_test.go b/core/stores/kv/store_test.go index a6385dab..c9b212b4 100644 --- a/core/stores/kv/store_test.go +++ b/core/stores/kv/store_test.go @@ -6,7 +6,7 @@ import ( "github.com/alicebob/miniredis" "github.com/stretchr/testify/assert" - "github.com/tal-tech/go-zero/core/stores/internal" + "github.com/tal-tech/go-zero/core/stores/cache" "github.com/tal-tech/go-zero/core/stores/redis" "github.com/tal-tech/go-zero/core/stringx" ) @@ -478,7 +478,7 @@ func runOnCluster(t *testing.T, fn func(cluster Store)) { s1.FlushAll() s2.FlushAll() - store := NewStore([]internal.NodeConf{ + store := NewStore([]cache.NodeConf{ { RedisConf: redis.RedisConf{ Host: s1.Addr(), diff --git a/core/stores/mongoc/cachedcollection.go b/core/stores/mongoc/cachedcollection.go index 62ce3c25..e8e0c8c2 100644 --- a/core/stores/mongoc/cachedcollection.go +++ b/core/stores/mongoc/cachedcollection.go @@ -2,7 +2,7 @@ package mongoc import ( "github.com/globalsign/mgo" - "github.com/tal-tech/go-zero/core/stores/internal" + "github.com/tal-tech/go-zero/core/stores/cache" "github.com/tal-tech/go-zero/core/stores/mongo" "github.com/tal-tech/go-zero/core/syncx" ) @@ -12,7 +12,7 @@ var ( // can't use one SharedCalls per conn, because multiple conns may share the same cache key. sharedCalls = syncx.NewSharedCalls() - stats = internal.NewCacheStat("mongoc") + stats = cache.NewCacheStat("mongoc") ) type ( @@ -20,11 +20,11 @@ type ( cachedCollection struct { collection mongo.Collection - cache internal.Cache + cache cache.Cache } ) -func newCollection(collection mongo.Collection, c internal.Cache) *cachedCollection { +func newCollection(collection mongo.Collection, c cache.Cache) *cachedCollection { return &cachedCollection{ collection: collection, cache: c, diff --git a/core/stores/mongoc/cachedcollection_test.go b/core/stores/mongoc/cachedcollection_test.go index a214d073..cebd8368 100644 --- a/core/stores/mongoc/cachedcollection_test.go +++ b/core/stores/mongoc/cachedcollection_test.go @@ -16,7 +16,7 @@ import ( "github.com/globalsign/mgo/bson" "github.com/stretchr/testify/assert" "github.com/tal-tech/go-zero/core/stat" - "github.com/tal-tech/go-zero/core/stores/internal" + "github.com/tal-tech/go-zero/core/stores/cache" "github.com/tal-tech/go-zero/core/stores/mongo" "github.com/tal-tech/go-zero/core/stores/redis" ) @@ -33,7 +33,7 @@ func TestStat(t *testing.T) { } r := redis.NewRedis(s.Addr(), redis.NodeType) - cach := internal.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound) + cach := cache.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound) c := newCollection(dummyConn{}, cach) for i := 0; i < 10; i++ { @@ -56,7 +56,7 @@ func TestStatCacheFails(t *testing.T) { defer log.SetOutput(os.Stdout) r := redis.NewRedis("localhost:59999", redis.NodeType) - cach := internal.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound) + cach := cache.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound) c := newCollection(dummyConn{}, cach) for i := 0; i < 20; i++ { @@ -79,7 +79,7 @@ func TestStatDbFails(t *testing.T) { } r := redis.NewRedis(s.Addr(), redis.NodeType) - cach := internal.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound) + cach := cache.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound) c := newCollection(dummyConn{}, cach) for i := 0; i < 20; i++ { @@ -103,7 +103,7 @@ func TestStatFromMemory(t *testing.T) { } r := redis.NewRedis(s.Addr(), redis.NodeType) - cach := internal.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound) + cach := cache.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound) c := newCollection(dummyConn{}, cach) var all sync.WaitGroup diff --git a/core/stores/mongoc/cachedmodel.go b/core/stores/mongoc/cachedmodel.go index c2482f7b..3b0effc7 100644 --- a/core/stores/mongoc/cachedmodel.go +++ b/core/stores/mongoc/cachedmodel.go @@ -5,14 +5,13 @@ import ( "github.com/globalsign/mgo" "github.com/tal-tech/go-zero/core/stores/cache" - "github.com/tal-tech/go-zero/core/stores/internal" "github.com/tal-tech/go-zero/core/stores/mongo" "github.com/tal-tech/go-zero/core/stores/redis" ) type Model struct { *mongo.Model - cache internal.Cache + cache cache.Cache generateCollection func(*mgo.Session) *cachedCollection } @@ -35,14 +34,14 @@ func MustNewModel(url, collection string, c cache.CacheConf, opts ...cache.Optio } func NewNodeModel(url, collection string, rds *redis.Redis, opts ...cache.Option) (*Model, error) { - c := internal.NewCacheNode(rds, sharedCalls, stats, mgo.ErrNotFound, opts...) + c := cache.NewCacheNode(rds, sharedCalls, stats, mgo.ErrNotFound, opts...) return createModel(url, collection, c, func(collection mongo.Collection) *cachedCollection { return newCollection(collection, c) }) } func NewModel(url, collection string, conf cache.CacheConf, opts ...cache.Option) (*Model, error) { - c := internal.NewCache(conf, sharedCalls, stats, mgo.ErrNotFound, opts...) + c := cache.NewCache(conf, sharedCalls, stats, mgo.ErrNotFound, opts...) return createModel(url, collection, c, func(collection mongo.Collection) *cachedCollection { return newCollection(collection, c) }) @@ -224,7 +223,7 @@ func (mm *Model) pipe(fn func(c *cachedCollection) mongo.Pipe) (mongo.Pipe, erro return fn(mm.GetCollection(session)), nil } -func createModel(url, collection string, c internal.Cache, +func createModel(url, collection string, c cache.Cache, create func(mongo.Collection) *cachedCollection) (*Model, error) { model, err := mongo.NewModel(url, collection) if err != nil { diff --git a/core/stores/sqlc/cachedsql.go b/core/stores/sqlc/cachedsql.go index 70efdd21..b6dc9992 100644 --- a/core/stores/sqlc/cachedsql.go +++ b/core/stores/sqlc/cachedsql.go @@ -5,7 +5,6 @@ import ( "time" "github.com/tal-tech/go-zero/core/stores/cache" - "github.com/tal-tech/go-zero/core/stores/internal" "github.com/tal-tech/go-zero/core/stores/redis" "github.com/tal-tech/go-zero/core/stores/sqlx" "github.com/tal-tech/go-zero/core/syncx" @@ -19,7 +18,7 @@ var ( // can't use one SharedCalls per conn, because multiple conns may share the same cache key. exclusiveCalls = syncx.NewSharedCalls() - stats = internal.NewCacheStat("sqlc") + stats = cache.NewCacheStat("sqlc") ) type ( @@ -30,21 +29,21 @@ type ( CachedConn struct { db sqlx.SqlConn - cache internal.Cache + cache cache.Cache } ) func NewNodeConn(db sqlx.SqlConn, rds *redis.Redis, opts ...cache.Option) CachedConn { return CachedConn{ db: db, - cache: internal.NewCacheNode(rds, exclusiveCalls, stats, sql.ErrNoRows, opts...), + cache: cache.NewCacheNode(rds, exclusiveCalls, stats, sql.ErrNoRows, opts...), } } func NewConn(db sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) CachedConn { return CachedConn{ db: db, - cache: internal.NewCache(c, exclusiveCalls, stats, sql.ErrNoRows, opts...), + cache: cache.NewCache(c, exclusiveCalls, stats, sql.ErrNoRows, opts...), } } diff --git a/zrpc/client.go b/zrpc/client.go index 5e47be60..700f5619 100644 --- a/zrpc/client.go +++ b/zrpc/client.go @@ -16,6 +16,8 @@ var ( ) type ( + ClientOption = internal.ClientOption + Client interface { AddInterceptor(interceptor grpc.UnaryClientInterceptor) Conn() *grpc.ClientConn @@ -26,7 +28,7 @@ type ( } ) -func MustNewClient(c RpcClientConf, options ...internal.ClientOption) Client { +func MustNewClient(c RpcClientConf, options ...ClientOption) Client { cli, err := NewClient(c, options...) if err != nil { log.Fatal(err) @@ -35,8 +37,8 @@ func MustNewClient(c RpcClientConf, options ...internal.ClientOption) Client { return cli } -func NewClient(c RpcClientConf, options ...internal.ClientOption) (Client, error) { - var opts []internal.ClientOption +func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) { + var opts []ClientOption if c.HasCredential() { opts = append(opts, WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{ App: c.App, @@ -75,7 +77,7 @@ func NewClientNoAuth(c discov.EtcdConf) (Client, error) { }, nil } -func NewClientWithTarget(target string, opts ...internal.ClientOption) (Client, error) { +func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) { return internal.NewClient(target, opts...) }