复制项目
This commit is contained in:
14
pkg/common/storage/versionctx/rpc.go
Normal file
14
pkg/common/storage/versionctx/rpc.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package versionctx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
func EnableVersionCtx() grpc.ServerOption {
|
||||
return grpc.ChainUnaryInterceptor(enableVersionCtxInterceptor)
|
||||
}
|
||||
|
||||
func enableVersionCtxInterceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
|
||||
return handler(WithVersionLog(ctx), req)
|
||||
}
|
||||
49
pkg/common/storage/versionctx/version.go
Normal file
49
pkg/common/storage/versionctx/version.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package versionctx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
tablerelation "git.imall.cloud/openim/open-im-server-deploy/pkg/common/storage/model"
|
||||
)
|
||||
|
||||
type Collection struct {
|
||||
Name string
|
||||
Doc *tablerelation.VersionLog
|
||||
}
|
||||
|
||||
type versionKey struct{}
|
||||
|
||||
func WithVersionLog(ctx context.Context) context.Context {
|
||||
return context.WithValue(ctx, versionKey{}, &VersionLog{})
|
||||
}
|
||||
|
||||
func GetVersionLog(ctx context.Context) *VersionLog {
|
||||
if v, ok := ctx.Value(versionKey{}).(*VersionLog); ok {
|
||||
return v
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type VersionLog struct {
|
||||
lock sync.Mutex
|
||||
data []Collection
|
||||
}
|
||||
|
||||
func (v *VersionLog) Append(data ...Collection) {
|
||||
if v == nil || len(data) == 0 {
|
||||
return
|
||||
}
|
||||
v.lock.Lock()
|
||||
defer v.lock.Unlock()
|
||||
v.data = append(v.data, data...)
|
||||
}
|
||||
|
||||
func (v *VersionLog) Get() []Collection {
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
v.lock.Lock()
|
||||
defer v.lock.Unlock()
|
||||
return v.data
|
||||
}
|
||||
Reference in New Issue
Block a user