mongo: configuration, sync with py args
+ tidy
This commit is contained in:
@@ -17,7 +17,18 @@ func GlobalTimeout(ctx context.Context) context.Context {
|
||||
return ctx
|
||||
}
|
||||
|
||||
func Initialize(ctx context.Context, uri string, opts *mongoOpt.ClientOptions) (*mongo.Collection, error) {
|
||||
type InitializeOptions struct {
|
||||
MaxPoolSize uint64
|
||||
CapSizeBytes int64
|
||||
ExpireAfterSeconds int64
|
||||
}
|
||||
|
||||
func Initialize(ctx context.Context, uri string, opt *InitializeOptions) (*mongo.Collection, error) {
|
||||
collectionName := "logs"
|
||||
if opt == nil {
|
||||
opt = &InitializeOptions{}
|
||||
}
|
||||
|
||||
uriParsed, err := url.ParseRequestURI(uri)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parsing URI for database name: %w", err)
|
||||
@@ -28,7 +39,7 @@ func Initialize(ctx context.Context, uri string, opts *mongoOpt.ClientOptions) (
|
||||
return nil, fmt.Errorf("URI must include database name (as database to authenticate against)")
|
||||
}
|
||||
|
||||
dbOpt := attachMetrics(opts).ApplyURI(uri)
|
||||
dbOpt := attachMetrics(mongoOpt.Client()).ApplyURI(uri).SetMaxPoolSize(opt.MaxPoolSize)
|
||||
|
||||
dbClient, err := mongo.Connect(GlobalTimeout(ctx), dbOpt)
|
||||
if err != nil {
|
||||
@@ -39,7 +50,19 @@ func Initialize(ctx context.Context, uri string, opts *mongoOpt.ClientOptions) (
|
||||
return nil, fmt.Errorf("first ping to database: %w", err)
|
||||
}
|
||||
|
||||
col := dbClient.Database(uriParsed.Path).Collection("logs")
|
||||
db := dbClient.Database(uriParsed.Path)
|
||||
|
||||
capped := opt.CapSizeBytes > 0
|
||||
|
||||
if err := db.CreateCollection(GlobalTimeout(ctx), collectionName, &mongoOpt.CreateCollectionOptions{
|
||||
Capped: &capped,
|
||||
SizeInBytes: &opt.CapSizeBytes,
|
||||
ExpireAfterSeconds: &opt.ExpireAfterSeconds,
|
||||
}); err != nil {
|
||||
return nil, fmt.Errorf("initializing collection")
|
||||
}
|
||||
|
||||
col := db.Collection(collectionName)
|
||||
|
||||
if err := InitializeIndexes(GlobalTimeout(ctx), col); err != nil {
|
||||
return nil, fmt.Errorf("initializing indexes: %w", err)
|
||||
|
Reference in New Issue
Block a user