consumer, provider 생성 방법 통일
This commit is contained in:
@ -1,9 +1,12 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
@ -78,3 +81,29 @@ func publickey_to_storagekey(pk publickey) storagekey {
|
||||
|
||||
return storagekey(hex.EncodeToString(decoded[:]))
|
||||
}
|
||||
|
||||
var errInvalidScheme = errors.New("storageAddr is not valid scheme")
|
||||
|
||||
func NewConsumer(ctx context.Context, storageAddr string, ttl time.Duration) (Consumer, error) {
|
||||
if strings.HasPrefix(storageAddr, "mongodb") {
|
||||
return newConsumerWithMongo(ctx, storageAddr, ttl)
|
||||
}
|
||||
|
||||
if strings.HasPrefix(storageAddr, "redis") {
|
||||
return newConsumerWithRedis(ctx, storageAddr, ttl)
|
||||
}
|
||||
|
||||
return nil, errInvalidScheme
|
||||
}
|
||||
|
||||
func NewProvider(ctx context.Context, storageAddr string, ttl time.Duration) (Provider, error) {
|
||||
if strings.HasPrefix(storageAddr, "mongodb") {
|
||||
return newProviderWithMongo(ctx, storageAddr, ttl)
|
||||
}
|
||||
|
||||
if strings.HasPrefix(storageAddr, "redis") {
|
||||
return newProviderWithRedis(ctx, storageAddr, ttl)
|
||||
}
|
||||
|
||||
return nil, errInvalidScheme
|
||||
}
|
||||
|
||||
@ -2,12 +2,14 @@ package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
|
||||
"repositories.action2quare.com/ayo/gocommon"
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
)
|
||||
@ -23,8 +25,18 @@ type sessionMongo struct {
|
||||
Ts primitive.DateTime `bson:"_ts"`
|
||||
}
|
||||
|
||||
func NewProviderWithMongo(ctx context.Context, mongoUrl string, dbname string, ttl time.Duration) (Provider, error) {
|
||||
mc, err := gocommon.NewMongoClient(ctx, mongoUrl, dbname)
|
||||
var errNoDatabaseNameInMongoUri = errors.New("mongo uri has no database name")
|
||||
|
||||
func newProviderWithMongo(ctx context.Context, mongoUrl string, ttl time.Duration) (Provider, error) {
|
||||
connstr, err := connstring.ParseAndValidate(mongoUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(connstr.Database) == 0 {
|
||||
return nil, errNoDatabaseNameInMongoUri
|
||||
}
|
||||
|
||||
mc, err := gocommon.NewMongoClient(ctx, mongoUrl, connstr.Database)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -92,8 +104,17 @@ type sessionPipelineDocument struct {
|
||||
Session *sessionMongo `bson:"fullDocument"`
|
||||
}
|
||||
|
||||
func NewConsumerWithMongo(ctx context.Context, mongoUrl string, dbname string, ttl time.Duration) (Consumer, error) {
|
||||
mc, err := gocommon.NewMongoClient(ctx, mongoUrl, dbname)
|
||||
func newConsumerWithMongo(ctx context.Context, mongoUrl string, ttl time.Duration) (Consumer, error) {
|
||||
connstr, err := connstring.ParseAndValidate(mongoUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(connstr.Database) == 0 {
|
||||
return nil, errNoDatabaseNameInMongoUri
|
||||
}
|
||||
|
||||
mc, err := gocommon.NewMongoClient(ctx, mongoUrl, connstr.Database)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ type provider_redis struct {
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewProviderWithRedis(ctx context.Context, redisUrl string, ttl time.Duration) (Provider, error) {
|
||||
func newProviderWithRedis(ctx context.Context, redisUrl string, ttl time.Duration) (Provider, error) {
|
||||
redisClient, err := gocommon.NewRedisClient(redisUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -92,7 +92,7 @@ type consumer_redis struct {
|
||||
redisClient *redis.Client
|
||||
}
|
||||
|
||||
func NewConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duration) (Consumer, error) {
|
||||
func newConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duration) (Consumer, error) {
|
||||
redisClient, err := gocommon.NewRedisClient(redisUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@ -11,26 +11,26 @@ import (
|
||||
)
|
||||
|
||||
func TestExpTable(t *testing.T) {
|
||||
pv, err := NewProviderWithRedis(context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
cs, err := NewConsumerWithRedis(context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// pv, err := NewProviderWithMongo(context.Background(), "mongodb://192.168.8.94:27017/?replicaSet=repl01&retrywrites=false", "maingate", 10*time.Second)
|
||||
// pv, err := NewProvider(context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second)
|
||||
// if err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
|
||||
// cs, err := NewConsumerWithMongo(context.Background(), "mongodb://192.168.8.94:27017/?replicaSet=repl01&retrywrites=false", "maingate", 10*time.Second)
|
||||
// cs, err := NewConsumer(context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second)
|
||||
// if err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
|
||||
pv, err := NewProvider(context.Background(), "mongodb://192.168.8.94:27017/maingate?replicaSet=repl01&retrywrites=false", 10*time.Second)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
cs, err := NewConsumer(context.Background(), "mongodb://192.168.8.94:27017/maingate?replicaSet=repl01&retrywrites=false", 10*time.Second)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
test := primitive.NewObjectID()
|
||||
sk := make_storagekey(test)
|
||||
pk := storagekey_to_publickey(sk)
|
||||
@ -82,7 +82,7 @@ func TestExpTable(t *testing.T) {
|
||||
cs.Touch(sk2)
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
cs2, err := NewConsumerWithRedis(context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second)
|
||||
cs2, err := NewConsumer(context.Background(), "redis://192.168.8.94:6380/1", 10*time.Second)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user