diff --git a/infra/logger/mongo.go b/infra/logger/mongo.go index 9db2b8e..71a5004 100644 --- a/infra/logger/mongo.go +++ b/infra/logger/mongo.go @@ -5,14 +5,21 @@ import ( "fmt" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" + "strconv" "time" ) var ( - collection *mongo.Collection + collectionMap map[string]*mongo.Collection ) func NewMongoDriver(config db, level int) (driver, error) { + if collectionMap == nil { + collectionMap = make(map[string]*mongo.Collection) + } + var collection *mongo.Collection + mKey := config.Host + strconv.Itoa(config.Port) + config.Database + config.Table + collection = collectionMap[mKey] if collection == nil { ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) client, err := mongo.Connect(ctx, options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%d", config.Host, config.Port))) @@ -24,7 +31,9 @@ func NewMongoDriver(config db, level int) (driver, error) { return nil, err } collection = client.Database(config.Database).Collection(config.Table) + collectionMap[mKey] = collection } + return &mongoLogger{ level: level, collection: collection,