diff --git a/mongodb/README.md b/mongodb/README.md index 31d64b22..6a63b3a4 100644 --- a/mongodb/README.md +++ b/mongodb/README.md @@ -89,11 +89,6 @@ type Config struct { // // Optional. Default is false Clear bool - - // Time before deleting expired keys - // - // Optional. Default is 10 * time.Second - GCInterval time.Duration } ``` @@ -104,7 +99,6 @@ var ConfigDefault = Config{ Port: 27017, Database: "fiber", Collection: "fiber_storage", - Clear: false, - GCInterval: 10 * time.Second, + Clear: false } ``` diff --git a/mongodb/config.go b/mongodb/config.go index 361a7494..48c5b072 100644 --- a/mongodb/config.go +++ b/mongodb/config.go @@ -1,9 +1,5 @@ package mongodb -import ( - "time" -) - // Config defines the config for storage. type Config struct { // Host name where the DB is hosted @@ -36,15 +32,10 @@ type Config struct { // Optional. Default is "fiber_storage" Collection string - // Clear any existing keys in existing Table + // Clear any existing keys in existing collection // // Optional. Default is false Clear bool - - // Time before deleting expired keys - // - // Optional. Default is 10 * time.Second - GCInterval time.Duration } // ConfigDefault is the default config @@ -54,7 +45,6 @@ var ConfigDefault = Config{ Database: "fiber", Collection: "fiber_storage", Clear: false, - GCInterval: 10 * time.Second, } // Helper function to set default values diff --git a/mongodb/mongodb.go b/mongodb/mongodb.go index 4312772a..ec10f840 100644 --- a/mongodb/mongodb.go +++ b/mongodb/mongodb.go @@ -37,7 +37,7 @@ func New(config ...Config) *Storage { cfg := configDefault(config...) // Create data source name - var dsn string = "mongodb://" + var dsn = "mongodb://" if cfg.Username != "" { dsn += url.QueryEscape(cfg.Username) } @@ -65,10 +65,21 @@ func New(config ...Config) *Storage { panic(err) } + // verify that the client can connect + if err = client.Ping(context.Background(), nil); err != nil { + panic(err) + } + // Get collection from database db := client.Database(cfg.Database) col := db.Collection(cfg.Collection) + if cfg.Clear { + if err = col.Drop(context.Background()); err != nil { + panic(err) + } + } + // expired data may exist for some time beyond the 60 second period between runs of the background task. // more on https://docs.mongodb.com/manual/core/index-ttl/ indexModel := mongo.IndexModel{