consumer, provider 생성 방법 통일

This commit is contained in:
2023-08-31 20:39:00 +09:00
parent cde46e6a5f
commit 57b518562e
4 changed files with 69 additions and 19 deletions

View File

@ -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
}