Skip to content

Commit

Permalink
handle sub-millisecond granularity
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Mar 7, 2025
1 parent 1eb7c89 commit 874f318
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions mongo/options/autoencryptionoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func (a *AutoEncryptionOptions) SetBypassQueryAnalysis(bypass bool) *AutoEncrypt
}

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

Expand Down
7 changes: 4 additions & 3 deletions x/mongo/driver/mongocrypt/mongocrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"net/http"
"time"
"unsafe"

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

var keyExpirationMs uint64 = 60_000 // 60,000 ms
if opts.KeyExpiration != nil {
expirationMs := opts.KeyExpiration.Milliseconds()
if expirationMs < 0 {
if opts.KeyExpiration <= 0 {
keyExpirationMs = 0
} else {
keyExpirationMs = uint64(expirationMs)
// find the ceiling integer millisecond for the expiration
keyExpirationMs = uint64((opts.KeyExpiration + time.Millisecond - 1) / time.Millisecond)
}
}
C.mongocrypt_setopt_key_expiration(crypt.wrapped, C.uint64_t(keyExpirationMs))
Expand Down
1 change: 1 addition & 0 deletions x/mongo/driver/mongocrypt/options/mongocrypt_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (mo *MongoCryptOptions) SetHTTPClient(httpClient *http.Client) *MongoCryptO
}

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

0 comments on commit 874f318

Please sign in to comment.