diff options
author | Gabriel Russell <gabriel.russell@mongodb.com> | 2016-03-03 14:30:27 -0500 |
---|---|---|
committer | Gabriel Russell <gabriel.russell@mongodb.com> | 2016-03-04 12:20:57 -0500 |
commit | 53877d3804edb5ef88b9d07a581f8e4a208239c3 (patch) | |
tree | c1c24046d349ba7b2a4012498ae83babf745884d | |
parent | 07635e851d8cbd3beb834c8cd68460220a7eadc4 (diff) | |
download | mongo-53877d3804edb5ef88b9d07a581f8e4a208239c3.tar.gz |
TOOLS-1078 specify dial timeout on the command line
This reverts commit 07635e851d8cbd3beb834c8cd68460220a7eadc4.
-rw-r--r-- | common/db/connector.go | 8 | ||||
-rw-r--r-- | common/db/connector_test.go | 3 | ||||
-rw-r--r-- | common/db/db.go | 7 | ||||
-rw-r--r-- | common/db/kerberos/gssapi.go | 8 | ||||
-rw-r--r-- | common/db/openssl/openssl.go | 11 | ||||
-rw-r--r-- | common/options/options.go | 34 |
6 files changed, 45 insertions, 26 deletions
diff --git a/common/db/connector.go b/common/db/connector.go index 24f55286a9b..b4b267818e1 100644 --- a/common/db/connector.go +++ b/common/db/connector.go @@ -1,6 +1,8 @@ package db import ( + "time" + "github.com/mongodb/mongo-tools/common/options" "github.com/mongodb/mongo-tools/common/util" "gopkg.in/mgo.v2" @@ -27,10 +29,14 @@ func (self *VanillaDBConnector) Configure(opts options.ToolOptions) error { // create the addresses to be used to connect connectionAddrs := util.CreateConnectionAddrs(opts.Host, opts.Port) + timeout := time.Duration(options.DefaultDialTimeoutSeconds) * time.Second + if opts.HiddenOptions != nil && opts.HiddenOptions.DialTimeoutSeconds != nil { + timeout = time.Duration(*opts.HiddenOptions.DialTimeoutSeconds) * time.Second + } // set up the dial info self.dialInfo = &mgo.DialInfo{ Addrs: connectionAddrs, - Timeout: DefaultDialTimeout, + Timeout: timeout, Direct: opts.Direct, ReplicaSetName: opts.ReplicaSetName, Username: opts.Auth.Username, diff --git a/common/db/connector_test.go b/common/db/connector_test.go index 8d5ff9a361b..13ceb9a6c53 100644 --- a/common/db/connector_test.go +++ b/common/db/connector_test.go @@ -6,6 +6,7 @@ import ( . "github.com/smartystreets/goconvey/convey" "gopkg.in/mgo.v2" "testing" + "time" ) func TestVanillaDBConnector(t *testing.T) { @@ -31,7 +32,7 @@ func TestVanillaDBConnector(t *testing.T) { So(connector.Configure(opts), ShouldBeNil) So(connector.dialInfo.Addrs, ShouldResemble, []string{"host1:20000", "host2:20000"}) - So(connector.dialInfo.Timeout, ShouldResemble, DefaultDialTimeout) + So(connector.dialInfo.Timeout, ShouldResemble, time.Duration(options.DefaultDialTimeoutSeconds)*time.Second) }) diff --git a/common/db/db.go b/common/db/db.go index e100ef42de1..8d617b8ff10 100644 --- a/common/db/db.go +++ b/common/db/db.go @@ -3,15 +3,15 @@ package db import ( - "fmt" "github.com/mongodb/mongo-tools/common/options" "github.com/mongodb/mongo-tools/common/password" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" + + "fmt" "io" "strings" "sync" - "time" ) type ( @@ -51,8 +51,7 @@ const ( ) var ( - DefaultDialTimeout = time.Second * 3 - GetConnectorFuncs = []GetConnectorFunc{} + GetConnectorFuncs = []GetConnectorFunc{} ) // Used to manage database sessions diff --git a/common/db/kerberos/gssapi.go b/common/db/kerberos/gssapi.go index 51f531fab1b..e774a56cad1 100644 --- a/common/db/kerberos/gssapi.go +++ b/common/db/kerberos/gssapi.go @@ -12,7 +12,6 @@ import ( ) const ( - KERBEROS_DIAL_TIMEOUT = time.Second * 3 KERBEROS_AUTHENTICATION_MECHANISM = "GSSAPI" ) @@ -27,10 +26,15 @@ func (self *KerberosDBConnector) Configure(opts options.ToolOptions) error { // create the addresses to be used to connect connectionAddrs := util.CreateConnectionAddrs(opts.Host, opts.Port) + timeout := time.Duration(options.DefaultDialTimeoutSeconds) * time.Second + if opts.HiddenOptions != nil && opts.HiddenOptions.DialTimeoutSeconds != nil { + timeout = time.Duration(*opts.HiddenOptions.DialTimeoutSeconds) * time.Second + } + // set up the dial info self.dialInfo = &mgo.DialInfo{ Addrs: connectionAddrs, - Timeout: KERBEROS_DIAL_TIMEOUT, + Timeout: timeout, Direct: opts.Direct, ReplicaSetName: opts.ReplicaSetName, diff --git a/common/db/openssl/openssl.go b/common/db/openssl/openssl.go index 36a788de027..313365efb22 100644 --- a/common/db/openssl/openssl.go +++ b/common/db/openssl/openssl.go @@ -13,10 +13,6 @@ import ( "github.com/spacemonkeygo/openssl" ) -var ( - DefaultSSLDialTimeout = time.Second * 3 -) - // For connecting to the database over ssl type SSLDBConnector struct { dialInfo *mgo.DialInfo @@ -50,10 +46,15 @@ func (self *SSLDBConnector) Configure(opts options.ToolOptions) error { return conn, err } + timeout := time.Duration(options.DefaultDialTimeoutSeconds) * time.Second + if opts.HiddenOptions != nil && opts.HiddenOptions.DialTimeoutSeconds != nil { + timeout = time.Duration(*opts.HiddenOptions.DialTimeoutSeconds) * time.Second + } + // set up the dial info self.dialInfo = &mgo.DialInfo{ Addrs: connectionAddrs, - Timeout: DefaultSSLDialTimeout, + Timeout: timeout, Direct: opts.Direct, ReplicaSetName: opts.ReplicaSetName, DialServer: dialer, diff --git a/common/options/options.go b/common/options/options.go index 49ab99e354d..b6866f9ef8c 100644 --- a/common/options/options.go +++ b/common/options/options.go @@ -3,9 +3,10 @@ package options import ( - "fmt" "github.com/jessevdk/go-flags" "github.com/mongodb/mongo-tools/common/log" + + "fmt" "os" "regexp" "runtime" @@ -13,9 +14,9 @@ import ( "strings" ) -const ( - VersionStr = "3.3.3-pre-" -) +const VersionStr = "3.3.3-pre-" + +const DefaultDialTimeoutSeconds = 3 // Gitspec that the tool was built with. Needs to be set using -ldflags var ( @@ -66,8 +67,9 @@ type HiddenOptions struct { // Deprecated flag for csv writing in mongoexport CSVOutputType bool - TempUsersColl *string - TempRolesColl *string + TempUsersColl *string + TempRolesColl *string + DialTimeoutSeconds *int } type Namespace struct { @@ -150,8 +152,10 @@ func parseVal(val string) int { // Ask for a new instance of tool options func New(appName, usageStr string, enabled EnabledOptions) *ToolOptions { + var timeout = DefaultDialTimeoutSeconds hiddenOpts := &HiddenOptions{ - BulkBufferSize: 10000, + BulkBufferSize: 10000, + DialTimeoutSeconds: &timeout, } opts := &ToolOptions{ @@ -186,9 +190,7 @@ func New(appName, usageStr string, enabled EnabledOptions) *ToolOptions { } } - opts.parser.UnknownOptionHandler = func(option string, arg flags.SplitArgument, args []string) ([]string, error) { - return parseHiddenOption(hiddenOpts, option, arg, args) - } + opts.parser.UnknownOptionHandler = hiddenOpts.parseHiddenOption if _, err := opts.parser.AddGroup("general options", "", opts.General); err != nil { panic(fmt.Errorf("couldn't register general options: %v", err)) @@ -294,10 +296,10 @@ func (o *ToolOptions) Parse() ([]string, error) { return o.parser.Parse() } -func parseHiddenOption(opts *HiddenOptions, option string, arg flags.SplitArgument, args []string) ([]string, error) { +func (opts *HiddenOptions) parseHiddenOption(option string, arg flags.SplitArgument, args []string) ([]string, error) { if option == "dbpath" || option == "directoryperdb" || option == "journal" { - return args, fmt.Errorf(`--dbpath and related flags are not supported in 3.0 tools. -See http://dochub.mongodb.org/core/tools-dbpath-deprecated for more information`) + return args, fmt.Errorf("--dbpath and related flags are not supported in 3.0 tools.\n" + + "See http://dochub.mongodb.org/core/tools-dbpath-deprecated for more information") } if option == "csv" { @@ -338,6 +340,12 @@ See http://dochub.mongodb.org/core/tools-dbpath-deprecated for more information` opts.BulkBufferSize = optionValue case "numDecodingWorkers": opts.NumDecodingWorkers = optionValue + case "dialTimeout": + if optionValue >= 0 { + opts.DialTimeoutSeconds = &optionValue + } else { + return args, fmt.Errorf("invalid negative value for --dialTimeout") + } default: return args, fmt.Errorf(`unknown option "%v"`, option) } |