Skip to content

Commit

Permalink
Moved connection to connect package, introduced overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
seroukhov committed Apr 3, 2021
1 parent 0be2b06 commit 82a0739
Show file tree
Hide file tree
Showing 19 changed files with 116 additions and 5,345 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# <img src="https://uploads-ssl.webflow.com/5ea5d3315186cf5ec60c3ee4/5edf1c94ce4c859f2b188094_logo.svg" alt="Pip.Services Logo" width="200"> <br/> MongoDB components for Golang Changelog

## <a name="1.1.0"></a> 1.1.0 (2021-04-03)

### Breaking changes
* Moved MongoDbConnection to connect package
* Added IMongoDbPersistenceOverrides to persistence constructors to support virtual methods

## <a name="1.0.5"></a> 1.0.5 (2020-12-11)

### Features
Expand Down
15 changes: 7 additions & 8 deletions build/DefaultMongoDbFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@ package build
import (
cref "github.com/pip-services3-go/pip-services3-commons-go/refer"
cbuild "github.com/pip-services3-go/pip-services3-components-go/build"
cmngpersist "github.com/pip-services3-go/pip-services3-mongodb-go/persistence"
conn "github.com/pip-services3-go/pip-services3-mongodb-go/connect"
)

//DefaultMongoDbFactory helps creates MongoDb components by their descriptors.
//See Factory
//See MongoDbConnection
type DefaultMongoDbFactory struct {
cbuild.Factory
Descriptor *cref.Descriptor
MongoDbConnectionDescriptor *cref.Descriptor
}

// NewDefaultMongoDbFactory are create a new instance of the factory.
// Return *DefaultMongoDbFactory
func NewDefaultMongoDbFactory() *DefaultMongoDbFactory {
mongoDBFactory := DefaultMongoDbFactory{}
mongoDBFactory.Descriptor = cref.NewDescriptor("pip-services", "factory", "rpc", "default", "1.0")
mongoDBFactory.MongoDbConnectionDescriptor = cref.NewDescriptor("pip-services", "connection", "mongodb", "*", "1.0")
mongoDBFactory.RegisterType(mongoDBFactory.MongoDbConnectionDescriptor, cmngpersist.NewMongoDbConnection)
return &mongoDBFactory
c := DefaultMongoDbFactory{}

mongoDbConnectionDescriptor := cref.NewDescriptor("pip-services", "connection", "mongodb", "*", "1.0")

c.RegisterType(mongoDbConnectionDescriptor, persist.NewMongoDbConnection)
return &c
}
34 changes: 15 additions & 19 deletions persistence/MongoDbConnection.go → connect/MongoDbConnection.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package persistence
package connect

import (
"context"
Expand All @@ -8,7 +8,6 @@ import (
cerror "github.com/pip-services3-go/pip-services3-commons-go/errors"
crefer "github.com/pip-services3-go/pip-services3-commons-go/refer"
clog "github.com/pip-services3-go/pip-services3-components-go/log"
mcon "github.com/pip-services3-go/pip-services3-mongodb-go/connect"
mongodrv "go.mongodb.org/mongo-driver/mongo"
mongoclopt "go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
Expand Down Expand Up @@ -57,7 +56,7 @@ type MongoDbConnection struct {
// The logger.
Logger *clog.CompositeLogger
// The connection resolver.
ConnectionResolver *mcon.MongoDbConnectionResolver
ConnectionResolver *MongoDbConnectionResolver
// The configuration options.
Options *cconf.ConfigParams
// The MongoDB connection object.
Expand All @@ -70,8 +69,8 @@ type MongoDbConnection struct {

// NewMongoDbConnection are creates a new instance of the connection component.
// Returns *MongoDbConnection with default config
func NewMongoDbConnection() (c *MongoDbConnection) {
mc := MongoDbConnection{
func NewMongoDbConnection() *MongoDbConnection {
c := MongoDbConnection{
defaultConfig: cconf.NewConfigParamsFromTuples(
"options.max_pool_size", "2",
"options.keep_alive", "0",
Expand All @@ -81,11 +80,11 @@ func NewMongoDbConnection() (c *MongoDbConnection) {
//The logger.
Logger: clog.NewCompositeLogger(),
//The connection resolver.
ConnectionResolver: mcon.NewMongoDbConnectionResolver(),
ConnectionResolver: NewMongoDbConnectionResolver(),
// The configuration options.
Options: cconf.NewEmptyConfigParams(),
}
return &mc
return &c
}

// Configure is configures component by passing configuration parameters.
Expand Down Expand Up @@ -114,17 +113,13 @@ func (c *MongoDbConnection) IsOpen() bool {
}

func (c *MongoDbConnection) composeSettings(settings *mongoclopt.ClientOptions) {
var maxPoolSize uint64
maxPoolSize = (uint64)(c.Options.GetAsInteger("max_pool_size"))
var MaxConnIdleTime time.Duration
maxPoolSize := (uint64)(c.Options.GetAsInteger("max_pool_size"))
keepAlive := c.Options.GetAsInteger("keep_alive")
MaxConnIdleTime = (time.Duration)(keepAlive) * time.Millisecond
var ConnectTimeout time.Duration
MaxConnIdleTime := (time.Duration)(keepAlive) * time.Millisecond
connectTimeoutMS := c.Options.GetAsInteger("connect_timeout")
ConnectTimeout = (time.Duration)(connectTimeoutMS) * time.Millisecond
var SocketTimeout time.Duration
ConnectTimeout := (time.Duration)(connectTimeoutMS) * time.Millisecond
socketTimeoutMS := c.Options.GetAsInteger("socket_timeout")
SocketTimeout = (time.Duration)(socketTimeoutMS) * time.Millisecond
SocketTimeout := (time.Duration)(socketTimeoutMS) * time.Millisecond

replicaSet := c.Options.GetAsNullableString("replica_set")
authSource := c.Options.GetAsString("auth_source")
Expand All @@ -148,10 +143,11 @@ func (c *MongoDbConnection) composeSettings(settings *mongoclopt.ClientOptions)

// Auth params
if authSource != "" && authUser != "" && authPassword != "" {
var authParams mongoclopt.Credential
authParams.AuthSource = authSource
authParams.Username = authUser
authParams.Password = authPassword
authParams := mongoclopt.Credential{
AuthSource: authSource,
Username: authUser,
Password: authPassword,
}
settings.SetAuth(authParams)
}
}
Expand Down
Loading

0 comments on commit 82a0739

Please sign in to comment.