Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Sep 28, 2023
1 parent d7c9ff7 commit 23df976
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 37 deletions.
29 changes: 12 additions & 17 deletions mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ type Client struct {
encryptedFieldsMap map[string]interface{}
}

// Connect creates a new Client and then initializes it using the Connect method. This is equivalent to calling
// NewClient followed by Client.Connect.
// Connect creates a new Client and then initializes it using the Connect method.
//
// When creating an options.ClientOptions, the order the methods are called matters. Later Set*
// methods will overwrite the values from previous Set* method invocations. This includes the
Expand All @@ -104,18 +103,18 @@ type Client struct {
// The Client.Ping method can be used to verify that the deployment is successfully connected and the
// Client was correctly configured.
func Connect(ctx context.Context, opts ...*options.ClientOptions) (*Client, error) {
c, err := NewClient(opts...)
c, err := newClient(opts...)
if err != nil {
return nil, err
}
err = c.Connect(ctx)
err = c.connect(ctx)
if err != nil {
return nil, err
}
return c, nil
}

// NewClient creates a new client to connect to a deployment specified by the uri.
// newClient creates a new client to connect to a deployment specified by the uri.
//
// When creating an options.ClientOptions, the order the methods are called matters. Later Set*
// methods will overwrite the values from previous Set* method invocations. This includes the
Expand All @@ -128,9 +127,7 @@ func Connect(ctx context.Context, opts ...*options.ClientOptions) (*Client, erro
// option fields of previous options, there is no partial overwriting. For example, if Username is
// set in the Auth field for the first option, and Password is set for the second but with no
// Username, after the merge the Username field will be empty.
//
// Deprecated: Use [Connect] instead.
func NewClient(opts ...*options.ClientOptions) (*Client, error) {
func newClient(opts ...*options.ClientOptions) (*Client, error) {
clientOpt := options.MergeClientOptions(opts...)

id, err := uuid.New()
Expand Down Expand Up @@ -235,14 +232,12 @@ func NewClient(opts ...*options.ClientOptions) (*Client, error) {
return client, nil
}

// Connect initializes the Client by starting background monitoring goroutines.
// connect initializes the Client by starting background monitoring goroutines.
// If the Client was created using the NewClient function, this method must be called before a Client can be used.
//
// Connect starts background goroutines to monitor the state of the deployment and does not do any I/O in the main
// goroutine. The Client.Ping method can be used to verify that the connection was created successfully.
//
// Deprecated: Use [mongo.Connect] instead.
func (c *Client) Connect(ctx context.Context) error {
func (c *Client) connect(ctx context.Context) error {
if connector, ok := c.deployment.(driver.Connector); ok {
err := connector.Connect()
if err != nil {
Expand All @@ -257,19 +252,19 @@ func (c *Client) Connect(ctx context.Context) error {
}

if c.internalClientFLE != nil {
if err := c.internalClientFLE.Connect(ctx); err != nil {
if err := c.internalClientFLE.connect(ctx); err != nil {
return err
}
}

if c.keyVaultClientFLE != nil && c.keyVaultClientFLE != c.internalClientFLE && c.keyVaultClientFLE != c {
if err := c.keyVaultClientFLE.Connect(ctx); err != nil {
if err := c.keyVaultClientFLE.connect(ctx); err != nil {
return err
}
}

if c.metadataClientFLE != nil && c.metadataClientFLE != c.internalClientFLE && c.metadataClientFLE != c {
if err := c.metadataClientFLE.Connect(ctx); err != nil {
if err := c.metadataClientFLE.connect(ctx); err != nil {
return err
}
}
Expand Down Expand Up @@ -515,7 +510,7 @@ func (c *Client) getOrCreateInternalClient(clientOpts *options.ClientOptions) (*
internalClientOpts.AutoEncryptionOptions = nil
internalClientOpts.SetMinPoolSize(0)
var err error
c.internalClientFLE, err = NewClient(internalClientOpts)
c.internalClientFLE, err = newClient(internalClientOpts)
return c.internalClientFLE, err
}

Expand All @@ -525,7 +520,7 @@ func (c *Client) configureKeyVaultClientFLE(clientOpts *options.ClientOptions) e
aeOpts := clientOpts.AutoEncryptionOptions
switch {
case aeOpts.KeyVaultClientOptions != nil:
c.keyVaultClientFLE, err = NewClient(aeOpts.KeyVaultClientOptions)
c.keyVaultClientFLE, err = newClient(aeOpts.KeyVaultClientOptions)
case clientOpts.MaxPoolSize != nil && *clientOpts.MaxPoolSize == 0:
c.keyVaultClientFLE = c
default:
Expand Down
18 changes: 9 additions & 9 deletions mongo/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func setupClient(opts ...*options.ClientOptions) *Client {
integtest.AddTestServerAPIVersion(clientOpts)
opts = append(opts, clientOpts)
}
client, _ := NewClient(opts...)
client, _ := newClient(opts...)
return client
}

Expand Down Expand Up @@ -183,7 +183,7 @@ func TestClient(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := NewClient(tc.opts)
_, err := newClient(tc.opts)
assert.Equal(t, tc.err, err, "expected error %v, got %v", tc.err, err)
})
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestClient(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := NewClient(tc.opts)
_, err := newClient(tc.opts)
assert.Equal(t, tc.err, err, "expected error %v, got %v", tc.err, err)
})
}
Expand All @@ -249,7 +249,7 @@ func TestClient(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
client, err := NewClient(tc.opts)
client, err := newClient(tc.opts)
if tc.expectErr {
assert.NotNil(t, err, "expected error, got nil")
return
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestClient(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
client, err := NewClient(tc.opts)
client, err := newClient(tc.opts)
if tc.expectErr {
assert.NotNil(t, err, "expected error, got nil")
return
Expand Down Expand Up @@ -412,22 +412,22 @@ func TestClient(t *testing.T) {

t.Run("success with all options", func(t *testing.T) {
serverAPIOptions := getServerAPIOptions()
client, err := NewClient(options.Client().SetServerAPIOptions(serverAPIOptions))
client, err := newClient(options.Client().SetServerAPIOptions(serverAPIOptions))
assert.Nil(t, err, "unexpected error from NewClient: %v", err)
convertedAPIOptions := topology.ConvertToDriverAPIOptions(serverAPIOptions)
assert.Equal(t, convertedAPIOptions, client.serverAPI,
"mismatch in serverAPI; expected %v, got %v", convertedAPIOptions, client.serverAPI)
})
t.Run("failure with unsupported version", func(t *testing.T) {
serverAPIOptions := options.ServerAPI("badVersion")
_, err := NewClient(options.Client().SetServerAPIOptions(serverAPIOptions))
_, err := newClient(options.Client().SetServerAPIOptions(serverAPIOptions))
assert.NotNil(t, err, "expected error from NewClient, got nil")
errmsg := `api version "badVersion" not supported; this driver version only supports API version "1"`
assert.Equal(t, errmsg, err.Error(), "expected error %v, got %v", errmsg, err.Error())
})
t.Run("cannot modify options after client creation", func(t *testing.T) {
serverAPIOptions := getServerAPIOptions()
client, err := NewClient(options.Client().SetServerAPIOptions(serverAPIOptions))
client, err := newClient(options.Client().SetServerAPIOptions(serverAPIOptions))
assert.Nil(t, err, "unexpected error from NewClient: %v", err)

expectedServerAPIOptions := getServerAPIOptions()
Expand Down Expand Up @@ -476,7 +476,7 @@ func TestClient(t *testing.T) {
extraOptions["__cryptSharedLibDisabledForTestOnly"] = true
}

_, err := NewClient(options.Client().
_, err := newClient(options.Client().
SetAutoEncryptionOptions(options.AutoEncryption().
SetKmsProviders(map[string]map[string]interface{}{
"local": {"key": make([]byte, 96)},
Expand Down
2 changes: 1 addition & 1 deletion mongo/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestDatabase(t *testing.T) {
})
t.Run("TransientTransactionError label", func(t *testing.T) {
client := setupClient(options.Client().ApplyURI("mongodb://nonexistent").SetServerSelectionTimeout(3 * time.Second))
err := client.Connect(bgCtx)
err := client.connect(bgCtx)
defer client.Disconnect(bgCtx)
assert.Nil(t, err, "expected nil, got %v", err)

Expand Down
8 changes: 0 additions & 8 deletions mongo/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,6 @@ func TestClient(t *testing.T) {
assert.Nil(mt, err, "Disconnect error: %v", err)
})
})
mt.RunOpts("watch", noClientOpts, func(mt *mtest.T) {
mt.Run("disconnected", func(mt *mtest.T) {
c, err := mongo.NewClient(options.Client().ApplyURI(mtest.ClusterURI()))
assert.Nil(mt, err, "NewClient error: %v", err)
_, err = c.Watch(context.Background(), mongo.Pipeline{})
assert.Equal(mt, mongo.ErrClientDisconnected, err, "expected error %v, got %v", mongo.ErrClientDisconnected, err)
})
})
mt.RunOpts("end sessions", mtest.NewOptions().MinServerVersion("3.6"), func(mt *mtest.T) {
_, err := mt.Client.ListDatabases(context.Background(), bson.D{})
assert.Nil(mt, err, "ListDatabases error: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions mongo/mongocryptd.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func newMongocryptdClient(opts *options.AutoEncryptionOptions) (*mongocryptdClie
}

// create client
client, err := NewClient(options.Client().ApplyURI(uri).SetServerSelectionTimeout(defaultServerSelectionTimeout))
client, err := newClient(options.Client().ApplyURI(uri).SetServerSelectionTimeout(defaultServerSelectionTimeout))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func (mc *mongocryptdClient) markCommand(ctx context.Context, dbName string, cmd

// connect connects the underlying Client instance. This must be called before performing any mark operations.
func (mc *mongocryptdClient) connect(ctx context.Context) error {
return mc.client.Connect(ctx)
return mc.client.connect(ctx)
}

// disconnect disconnects the underlying Client instance. This should be called after all operations have completed.
Expand Down

0 comments on commit 23df976

Please sign in to comment.