diff options
Diffstat (limited to 'src/mongo.go')
-rw-r--r-- | src/mongo.go | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/src/mongo.go b/src/mongo.go index 54f9533..3a23131 100644 --- a/src/mongo.go +++ b/src/mongo.go | |||
@@ -2,6 +2,8 @@ package main | |||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "context" | 4 | "context" |
5 | "fmt" | ||
6 | "sync" | ||
5 | "time" | 7 | "time" |
6 | 8 | ||
7 | "go.mongodb.org/mongo-driver/bson" | 9 | "go.mongodb.org/mongo-driver/bson" |
@@ -13,6 +15,8 @@ type mongoClient struct { | |||
13 | dbName, colName string | 15 | dbName, colName string |
14 | client *mongo.Client | 16 | client *mongo.Client |
15 | col *mongo.Collection | 17 | col *mongo.Collection |
18 | Mutex sync.Mutex | ||
19 | // Attention: 这把锁是否有必要? | ||
16 | } | 20 | } |
17 | 21 | ||
18 | func (mc *mongoClient) init(dbName, colName string) error { | 22 | func (mc *mongoClient) init(dbName, colName string) error { |
@@ -89,3 +93,12 @@ func (mc *mongoClient) Disconnect() error { | |||
89 | mc.colName = "" | 93 | mc.colName = "" |
90 | return nil | 94 | return nil |
91 | } | 95 | } |
96 | |||
97 | func (mc *mongoClient) GetLock() { | ||
98 | for i := 0; i < 20000; { | ||
99 | if !mc.Mutex.TryLock() { | ||
100 | i++ | ||
101 | } | ||
102 | } | ||
103 | fmt.Printf("Die...\n") | ||
104 | } | ||