Skip to content

Commit 593253b

Browse files
committed
use labix/mgo instead of mongo driver
1 parent fc21876 commit 593253b

File tree

3 files changed

+33
-137
lines changed

3 files changed

+33
-137
lines changed

Gopkg.lock

+15-119
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
version = "0.8.6"
4747

4848
[[constraint]]
49-
name = "github.com/mongodb/mongo-go-driver"
50-
version = "0.0.17"
49+
name = "github.com/globalsign/mgo"
50+
version = "r2018.06.15"
5151

5252
[[constraint]]
5353
name = "github.com/nsqio/go-nsq"

sink/mongodb.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package sink
22

33
import (
4-
"context"
54
"strconv"
65
"time"
76

87
"os"
98

109
"fmt"
11-
"encoding/json"
1210

1311
log "github.com/sirupsen/logrus"
1412

15-
mongo "github.com/mongodb/mongo-go-driver/mongo"
13+
"github.com/globalsign/mgo"
14+
"github.com/globalsign/mgo/bson"
1615
)
1716

1817
// MongodbSink ...
1918
type MongodbSink struct {
20-
conn *mongo.Client
19+
session *mgo.Session
2120
database string
2221
collection string
2322
workerCount int
@@ -32,7 +31,7 @@ func NewMongodb() (*MongodbSink, error) {
3231
return nil, fmt.Errorf("[sink/mongodb] Missing SINK_MONGODB_CONNECTION (example: mongodb://foo:bar@localhost:27017)")
3332
}
3433

35-
database := os.Getenv("SINK_MONGODB_DATABASE")
34+
database := os.Getenv("SINK_MONGODB_DATABASE")
3635
if database == "" {
3736
return nil, fmt.Errorf("[sink/mongodb] Mising SINK_MONGODB_DATABASE")
3837
}
@@ -46,24 +45,24 @@ func NewMongodb() (*MongodbSink, error) {
4645
if workerCountStr == "" {
4746
workerCountStr = "1"
4847
}
48+
4949
workerCount, err := strconv.Atoi(workerCountStr)
5050
if err != nil {
5151
return nil, fmt.Errorf("Invalid SINK_MONGODB_WORKERS, must be an integer")
5252
}
5353

54-
conn, err := mongo.NewClient(connStr)
5554
if err != nil {
56-
return nil, fmt.Errorf("[sink/mongodb] Invalid to connect to string: %s", err)
55+
return nil, fmt.Errorf("Invalid SINK_MONGODB_WORKERS, must be an integer")
5756
}
5857

59-
err = conn.Connect(context.Background())
58+
session, err := mgo.Dial(connStr)
6059
if err != nil {
6160
return nil, fmt.Errorf("[sink/mongodb] failed to connect to string: %s", err)
6261
}
6362

6463

6564
return &MongodbSink{
66-
conn: conn,
65+
session: session,
6766
database: database,
6867
collection: collection,
6968
workerCount: workerCount,
@@ -103,7 +102,7 @@ func (s *MongodbSink) Stop() {
103102
}
104103

105104
close(s.stopCh)
106-
defer s.conn.Disconnect(context.Background())
105+
defer s.session.Close()
107106
}
108107

109108
// Put ..
@@ -116,19 +115,20 @@ func (s *MongodbSink) Put(data []byte) error {
116115
func (s *MongodbSink) write(id int) {
117116
log.Infof("[sink/mongodb/%d] Starting writer", id)
118117

119-
collection := s.conn.Database(s.database).Collection(s.collection)
118+
c := s.session.DB(s.database).C(s.collection)
120119

121120
for {
122121
select {
123122
case data := <-s.putCh:
124-
m := make(map[string]interface{})
125-
err := json.Unmarshal(data, &m)
126-
127-
if err != nil {
123+
var record bson.M
124+
err := bson.UnmarshalJSON(data, &record)
125+
if (err != nil) {
128126
log.Errorf("[sink/mongodb/%d] %s", id, err)
129127
continue
130128
}
131-
_, err = collection.InsertOne(context.Background(), m)
129+
idRecord := bson.M{}
130+
update := bson.M{"$set": record}
131+
_, err = c.Upsert(idRecord, update)
132132
if err != nil {
133133
log.Errorf("[sink/mongodb/%d] %s", id, err)
134134
} else {

0 commit comments

Comments
 (0)