Skip to content

Commit 573812e

Browse files
committed
handle sub-millisecond granularity
1 parent 1eb7c89 commit 573812e

File tree

4 files changed

+7
-3
lines changed

4 files changed

+7
-3
lines changed

mongo/options/autoencryptionoptions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ func (a *AutoEncryptionOptions) SetBypassQueryAnalysis(bypass bool) *AutoEncrypt
168168
}
169169

170170
// SetKeyExpiration specifies duration for the key expiration. 0 or negative value means "never expire".
171+
// The granularity is in milliseconds. Any sub-millisecond fraction will be rounded up.
171172
func (a *AutoEncryptionOptions) SetKeyExpiration(expiration time.Duration) *AutoEncryptionOptions {
172173
a.KeyExpiration = &expiration
173174

mongo/options/clientencryptionoptions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func (c *ClientEncryptionOptionsBuilder) SetTLSConfig(cfg map[string]*tls.Config
8383
}
8484

8585
// SetKeyExpiration specifies duration for the key expiration. 0 or negative value means "never expire".
86+
// The granularity is in milliseconds. Any sub-millisecond fraction will be rounded up.
8687
func (c *ClientEncryptionOptionsBuilder) SetKeyExpiration(expiration time.Duration) *ClientEncryptionOptionsBuilder {
8788
c.Opts = append(c.Opts, func(opts *ClientEncryptionOptions) error {
8889
opts.KeyExpiration = &expiration

x/mongo/driver/mongocrypt/mongocrypt.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"errors"
2121
"fmt"
2222
"net/http"
23+
"time"
2324
"unsafe"
2425

2526
"go.mongodb.org/mongo-driver/v2/bson"
@@ -91,11 +92,11 @@ func NewMongoCrypt(opts *options.MongoCryptOptions) (*MongoCrypt, error) {
9192

9293
var keyExpirationMs uint64 = 60_000 // 60,000 ms
9394
if opts.KeyExpiration != nil {
94-
expirationMs := opts.KeyExpiration.Milliseconds()
95-
if expirationMs < 0 {
95+
if *opts.KeyExpiration <= 0 {
9696
keyExpirationMs = 0
9797
} else {
98-
keyExpirationMs = uint64(expirationMs)
98+
// find the ceiling integer millisecond for the expiration
99+
keyExpirationMs = uint64((*opts.KeyExpiration + time.Millisecond - 1) / time.Millisecond)
99100
}
100101
}
101102
C.mongocrypt_setopt_key_expiration(crypt.wrapped, C.uint64_t(keyExpirationMs))

x/mongo/driver/mongocrypt/options/mongocrypt_options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func (mo *MongoCryptOptions) SetHTTPClient(httpClient *http.Client) *MongoCryptO
7474
}
7575

7676
// SetKeyExpiration sets the key expiration duration. 0 means "never expire".
77+
// The granularity is in milliseconds. Any sub-millisecond fraction will be rounded up.
7778
func (mo *MongoCryptOptions) SetKeyExpiration(expiration *time.Duration) *MongoCryptOptions {
7879
mo.KeyExpiration = expiration
7980
return mo

0 commit comments

Comments
 (0)