-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add locking in connection cache
- Loading branch information
1 parent
9439185
commit 59928b6
Showing
5 changed files
with
45 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,54 @@ | ||
package mgod | ||
|
||
import "go.mongodb.org/mongo-driver/mongo" | ||
import ( | ||
"sync" | ||
|
||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
// dbConnCache is a cache of MongoDB database connections. | ||
var dbConnCache map[string]*mongo.Database | ||
var dbConnCache *connectionCache | ||
|
||
func init() { | ||
dbConnCache = make(map[string]*mongo.Database) | ||
dbConnCache = newConnectionCache() | ||
} | ||
|
||
// connectionCache is a thread safe construct to cache MongoDB database connections. | ||
type connectionCache struct { | ||
cache map[string]*mongo.Database | ||
mux sync.RWMutex | ||
} | ||
|
||
func newConnectionCache() *connectionCache { | ||
return &connectionCache{ | ||
cache: map[string]*mongo.Database{}, | ||
} | ||
} | ||
|
||
func (c *connectionCache) Get(dbName string) *mongo.Database { | ||
c.mux.RLock() | ||
defer c.mux.RUnlock() | ||
|
||
return c.cache[dbName] | ||
} | ||
|
||
func (c *connectionCache) Set(dbName string, db *mongo.Database) { | ||
c.mux.Lock() | ||
defer c.mux.Unlock() | ||
|
||
c.cache[dbName] = db | ||
} | ||
|
||
// getDBConn returns a MongoDB database connection from the cache. | ||
// If the connection is not present in the cache, it creates a new connection and adds it to the cache (Write-through policy). | ||
func getDBConn(dbName string) *mongo.Database { | ||
dbConn := dbConnCache.Get(dbName) | ||
|
||
// Initialize the cache entry if it is not present. | ||
if dbConnCache[dbName] == nil { | ||
dbConnCache[dbName] = mClient.Database(dbName) | ||
if dbConn == nil { | ||
dbConn = mClient.Database(dbName) | ||
dbConnCache.Set(dbName, dbConn) | ||
} | ||
|
||
return dbConnCache[dbName] | ||
return dbConn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters