@@ -3,7 +3,6 @@ package app
33import (
44 "context"
55 "crypto/rand"
6- "errors"
76 "time"
87
98 "github.com/dan13ram/wpokt-validator/models"
@@ -28,18 +27,17 @@ type Database interface {
2827 UpdateOne (collection string , filter interface {}, update interface {}) error
2928 UpsertOne (collection string , filter interface {}, update interface {}) error
3029
31- XLock (collection string , resourceId string ) (string , error )
32- SLock (collection string , resourceId string ) (string , error )
33- Unlock (collection string , lockId string ) error
30+ XLock (resourceId string ) (string , error )
31+ SLock (resourceId string ) (string , error )
32+ Unlock (lockId string ) error
3433}
3534
3635// mongoDatabase is a wrapper around the mongo database
3736type mongoDatabase struct {
3837 db * mongo.Database
39- timeout time.Duration
4038 uri string
4139 database string
42- lockers map [ string ] * lock.Client
40+ locker * lock.Client
4341}
4442
4543var (
4947// Connect connects to the database
5048func (d * mongoDatabase ) Connect () error {
5149 log .Debug ("[DB] Connecting to database" )
52- wcMajority := writeconcern .New (writeconcern .WMajority (), writeconcern .WTimeout (d . timeout ))
50+ wcMajority := writeconcern .New (writeconcern .WMajority (), writeconcern .WTimeout (time . Duration ( Config . MongoDB . TimeoutMillis ) * time . Millisecond ))
5351
54- ctx , cancel := context .WithTimeout (context .Background (), d . timeout )
52+ ctx , cancel := context .WithTimeout (context .Background (), time . Duration ( Config . MongoDB . TimeoutMillis ) * time . Millisecond )
5553 defer cancel ()
5654
5755 client , err := mongo .Connect (ctx , options .Client ().ApplyURI (d .uri ).SetWriteConcern (wcMajority ))
@@ -67,36 +65,14 @@ func (d *mongoDatabase) Connect() error {
6765// SetupLocker sets up the locker
6866func (d * mongoDatabase ) SetupLockers () error {
6967 log .Debug ("[DB] Setting up locker" )
70- d .lockers = make (map [string ]* lock.Client )
7168 var locker * lock.Client
7269
73- ctx , cancel := context .WithTimeout (context .Background (), d . timeout )
70+ ctx , cancel := context .WithTimeout (context .Background (), time . Duration ( Config . MongoDB . TimeoutMillis ) * time . Millisecond )
7471 defer cancel ()
7572
76- locker = lock .NewClient (d .db .Collection (models . CollectionMints ))
73+ locker = lock .NewClient (d .db .Collection ("locks" ))
7774 locker .CreateIndexes (ctx )
78- d .lockers [models .CollectionMints ] = locker
79-
80- ctx , cancel = context .WithTimeout (context .Background (), d .timeout )
81- defer cancel ()
82-
83- locker = lock .NewClient (d .db .Collection (models .CollectionInvalidMints ))
84- locker .CreateIndexes (ctx )
85- d .lockers [models .CollectionInvalidMints ] = locker
86-
87- ctx , cancel = context .WithTimeout (context .Background (), d .timeout )
88- defer cancel ()
89-
90- locker = lock .NewClient (d .db .Collection (models .CollectionBurns ))
91- locker .CreateIndexes (ctx )
92- d .lockers [models .CollectionBurns ] = locker
93-
94- ctx , cancel = context .WithTimeout (context .Background (), d .timeout )
95- defer cancel ()
96-
97- locker = lock .NewClient (d .db .Collection (models .CollectionHealthChecks ))
98- locker .CreateIndexes (ctx )
99- d .lockers [models .CollectionHealthChecks ] = locker
75+ d .locker = locker
10076
10177 log .Info ("[DB] Locker setup" )
10278 return nil
@@ -113,46 +89,31 @@ func randomString(n int) string {
11389}
11490
11591// XLock locks a resource for exclusive access
116- func (d * mongoDatabase ) XLock (collection string , resourceId string ) (string , error ) {
117- locker := d .lockers [collection ]
118- if locker == nil {
119- return "" , errors .New ("Locker not found" )
120- }
121-
122- ctx , cancel := context .WithTimeout (context .Background (), d .timeout )
92+ func (d * mongoDatabase ) XLock (resourceId string ) (string , error ) {
93+ ctx , cancel := context .WithTimeout (context .Background (), time .Duration (Config .MongoDB .TimeoutMillis )* time .Millisecond )
12394 defer cancel ()
12495
12596 lockId := randomString (32 )
126- err := locker .XLock (ctx , resourceId , lockId , lock.LockDetails {})
97+ err := d . locker .XLock (ctx , resourceId , lockId , lock.LockDetails {})
12798 return lockId , err
12899}
129100
130101// SLock locks a resource for shared access
131- func (d * mongoDatabase ) SLock (collection string , resourceId string ) (string , error ) {
132- locker := d .lockers [collection ]
133- if locker == nil {
134- return "" , errors .New ("Locker not found" )
135- }
136-
137- ctx , cancel := context .WithTimeout (context .Background (), d .timeout )
102+ func (d * mongoDatabase ) SLock (resourceId string ) (string , error ) {
103+ ctx , cancel := context .WithTimeout (context .Background (), time .Duration (Config .MongoDB .TimeoutMillis )* time .Millisecond )
138104 defer cancel ()
139105
140106 lockId := randomString (32 )
141- err := locker .SLock (ctx , resourceId , lockId , lock.LockDetails {}, - 1 )
107+ err := d . locker .SLock (ctx , resourceId , lockId , lock.LockDetails {}, - 1 )
142108 return lockId , err
143109}
144110
145111// Unlock unlocks a resource
146- func (d * mongoDatabase ) Unlock (collection string , lockId string ) error {
147- locker := d .lockers [collection ]
148- if locker == nil {
149- return errors .New ("Locker not found" )
150- }
151-
152- ctx , cancel := context .WithTimeout (context .Background (), d .timeout )
112+ func (d * mongoDatabase ) Unlock (lockId string ) error {
113+ ctx , cancel := context .WithTimeout (context .Background (), time .Duration (Config .MongoDB .TimeoutMillis )* time .Millisecond )
153114 defer cancel ()
154115
155- _ , err := locker .Unlock (ctx , lockId )
116+ _ , err := d . locker .Unlock (ctx , lockId )
156117 return err
157118}
158119
@@ -162,7 +123,7 @@ func (d *mongoDatabase) SetupIndexes() error {
162123
163124 // setup unique index for mints
164125 log .Debug ("[DB] Setting up indexes for mints" )
165- ctx , cancel := context .WithTimeout (context .Background (), d . timeout )
126+ ctx , cancel := context .WithTimeout (context .Background (), time . Duration ( Config . MongoDB . TimeoutMillis ) * time . Millisecond )
166127 defer cancel ()
167128 _ , err := d .db .Collection (models .CollectionMints ).Indexes ().CreateOne (ctx , mongo.IndexModel {
168129 Keys : bson.D {{Key : "transaction_hash" , Value : 1 }},
@@ -174,7 +135,7 @@ func (d *mongoDatabase) SetupIndexes() error {
174135
175136 // setup unique index for invalid mints
176137 log .Debug ("[DB] Setting up indexes for invalid mints" )
177- ctx , cancel = context .WithTimeout (context .Background (), d . timeout )
138+ ctx , cancel = context .WithTimeout (context .Background (), time . Duration ( Config . MongoDB . TimeoutMillis ) * time . Millisecond )
178139 defer cancel ()
179140 _ , err = d .db .Collection (models .CollectionInvalidMints ).Indexes ().CreateOne (ctx , mongo.IndexModel {
180141 Keys : bson.D {{Key : "transaction_hash" , Value : 1 }},
@@ -186,7 +147,7 @@ func (d *mongoDatabase) SetupIndexes() error {
186147
187148 // setup unique index for burns
188149 log .Debug ("[DB] Setting up indexes for burns" )
189- ctx , cancel = context .WithTimeout (context .Background (), d . timeout )
150+ ctx , cancel = context .WithTimeout (context .Background (), time . Duration ( Config . MongoDB . TimeoutMillis ) * time . Millisecond )
190151 defer cancel ()
191152 _ , err = d .db .Collection (models .CollectionBurns ).Indexes ().CreateOne (ctx , mongo.IndexModel {
192153 Keys : bson.D {{Key : "transaction_hash" , Value : 1 }, {Key : "log_index" , Value : 1 }},
@@ -198,7 +159,7 @@ func (d *mongoDatabase) SetupIndexes() error {
198159
199160 // setup unique index for healthchecks
200161 log .Debug ("[DB] Setting up indexes for healthchecks" )
201- ctx , cancel = context .WithTimeout (context .Background (), d . timeout )
162+ ctx , cancel = context .WithTimeout (context .Background (), time . Duration ( Config . MongoDB . TimeoutMillis ) * time . Millisecond )
202163 defer cancel ()
203164 _ , err = d .db .Collection (models .CollectionHealthChecks ).Indexes ().CreateOne (ctx , mongo.IndexModel {
204165 Keys : bson.D {{Key : "validator_id" , Value : 1 }, {Key : "hostname" , Value : 1 }},
@@ -216,7 +177,7 @@ func (d *mongoDatabase) SetupIndexes() error {
216177// Disconnect disconnects from the database
217178func (d * mongoDatabase ) Disconnect () error {
218179 log .Debug ("[DB] Disconnecting from database" )
219- ctx , cancel := context .WithTimeout (context .Background (), d . timeout )
180+ ctx , cancel := context .WithTimeout (context .Background (), time . Duration ( Config . MongoDB . TimeoutMillis ) * time . Millisecond )
220181 defer cancel ()
221182 err := d .db .Client ().Disconnect (ctx )
222183 log .Info ("[DB] Disconnected from database" )
@@ -225,23 +186,23 @@ func (d *mongoDatabase) Disconnect() error {
225186
226187// method for insert single value in a collection
227188func (d * mongoDatabase ) InsertOne (collection string , data interface {}) error {
228- ctx , cancel := context .WithTimeout (context .Background (), d . timeout )
189+ ctx , cancel := context .WithTimeout (context .Background (), time . Duration ( Config . MongoDB . TimeoutMillis ) * time . Millisecond )
229190 defer cancel ()
230191 _ , err := d .db .Collection (collection ).InsertOne (ctx , data )
231192 return err
232193}
233194
234195// method for find single value in a collection
235196func (d * mongoDatabase ) FindOne (collection string , filter interface {}, result interface {}) error {
236- ctx , cancel := context .WithTimeout (context .Background (), d . timeout )
197+ ctx , cancel := context .WithTimeout (context .Background (), time . Duration ( Config . MongoDB . TimeoutMillis ) * time . Millisecond )
237198 defer cancel ()
238199 err := d .db .Collection (collection ).FindOne (ctx , filter ).Decode (result )
239200 return err
240201}
241202
242203// method for find multiple values in a collection
243204func (d * mongoDatabase ) FindMany (collection string , filter interface {}, result interface {}) error {
244- ctx , cancel := context .WithTimeout (context .Background (), d . timeout )
205+ ctx , cancel := context .WithTimeout (context .Background (), time . Duration ( Config . MongoDB . TimeoutMillis ) * time . Millisecond )
245206 defer cancel ()
246207 cursor , err := d .db .Collection (collection ).Find (ctx , filter )
247208 if err != nil {
@@ -253,15 +214,15 @@ func (d *mongoDatabase) FindMany(collection string, filter interface{}, result i
253214
254215//method for update single value in a collection
255216func (d * mongoDatabase ) UpdateOne (collection string , filter interface {}, update interface {}) error {
256- ctx , cancel := context .WithTimeout (context .Background (), d . timeout )
217+ ctx , cancel := context .WithTimeout (context .Background (), time . Duration ( Config . MongoDB . TimeoutMillis ) * time . Millisecond )
257218 defer cancel ()
258219 _ , err := d .db .Collection (collection ).UpdateOne (ctx , filter , update )
259220 return err
260221}
261222
262223//method for upsert single value in a collection
263224func (d * mongoDatabase ) UpsertOne (collection string , filter interface {}, update interface {}) error {
264- ctx , cancel := context .WithTimeout (context .Background (), d . timeout )
225+ ctx , cancel := context .WithTimeout (context .Background (), time . Duration ( Config . MongoDB . TimeoutMillis ) * time . Millisecond )
265226 defer cancel ()
266227
267228 opts := options .Update ().SetUpsert (true )
@@ -272,7 +233,6 @@ func (d *mongoDatabase) UpsertOne(collection string, filter interface{}, update
272233// InitDB creates a new database wrapper
273234func InitDB () {
274235 DB = & mongoDatabase {
275- timeout : time .Millisecond * time .Duration (Config .MongoDB .TimeoutMillis ),
276236 uri : Config .MongoDB .URI ,
277237 database : Config .MongoDB .Database ,
278238 }
0 commit comments