Skip to content

Commit

Permalink
Resolves #647 - Fixes typos in Mongo advisory locking parameters (#648)
Browse files Browse the repository at this point in the history
* Resolves #647 - Fixes typos in Mongo advisory locking parameters

* PR Feedback for #647 - Error out when both params set

* Fixes lint issues with #647

* Additional PR Fixes #647

* Lint fixes again #647

* One more tweak to CONTRIBUTING.md

* One more tweak to CONTRIBUTING.md, again

Co-authored-by: Steve Ramage <commits@sjrx.net>
  • Loading branch information
SJrX and Steve Ramage committed Nov 5, 2021
1 parent efa49bc commit 4ba6957
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions database/mongodb/README.md
Expand Up @@ -15,8 +15,8 @@
| `x-transaction-mode` | `TransactionMode` | If set to `true` wrap commands in [transaction](https://docs.mongodb.com/manual/core/transactions). Available only for replica set. Driver is using [strconv.ParseBool](https://golang.org/pkg/strconv/#ParseBool) for parsing|
| `x-advisory-locking` | `true` | Feature flag for advisory locking, if set to false, disable advisory locking |
| `x-advisory-lock-collection` | `migrate_advisory_lock` | The name of the collection to use for advisory locking.|
| `x-advisory-lock-timout` | `15` | The max time in seconds that the advisory lock will wait if the db is already locked. |
| `x-advisory-lock-timout-interval` | `10` | The max timeout in seconds interval that the advisory lock will wait if the db is already locked. |
| `x-advisory-lock-timeout` | `15` | The max time in seconds that migrate will wait to acquire a lock before failing. |
| `x-advisory-lock-timeout-interval` | `10` | The max time in seconds between attempts to acquire the advisory lock, the lock is attempted to be acquired using an exponential backoff algorithm. |
| `dbname` | `DatabaseName` | The name of the database to connect to |
| `user` | | The user to sign in as. Can be omitted |
| `password` | | The user's password. Can be omitted |
Expand Down
24 changes: 20 additions & 4 deletions database/mongodb/mongodb.go
Expand Up @@ -36,8 +36,9 @@ const LockIndexName = "lock_unique_key" // the name of the inde
const contextWaitTimeout = 5 * time.Second // how long to wait for the request to mongo to block/wait for.

var (
ErrNoDatabaseName = fmt.Errorf("no database name")
ErrNilConfig = fmt.Errorf("no config")
ErrNoDatabaseName = fmt.Errorf("no database name")
ErrNilConfig = fmt.Errorf("no config")
ErrLockTimeoutConfigConflict = fmt.Errorf("both x-advisory-lock-timeout-interval and x-advisory-lock-timout-interval were specified")
)

type Mongo struct {
Expand Down Expand Up @@ -137,7 +138,22 @@ func (m *Mongo) Open(dsn string) (database.Driver, error) {
if err != nil {
return nil, err
}
maxLockingIntervals, err := parseInt(unknown.Get("x-advisory-lock-timout-interval"), DefaultLockTimeoutInterval)

lockTimeoutIntervalValue := unknown.Get("x-advisory-lock-timeout-interval")
// The initial release had a typo for this argument but for backwards compatibility sake, we will keep supporting it
// and we will error out if both values are set.
lockTimeoutIntervalValueFromTypo := unknown.Get("x-advisory-lock-timout-interval")

lockTimeout := lockTimeoutIntervalValue

if lockTimeoutIntervalValue != "" && lockTimeoutIntervalValueFromTypo != "" {
return nil, ErrLockTimeoutConfigConflict
} else if lockTimeoutIntervalValueFromTypo != "" {
lockTimeout = lockTimeoutIntervalValueFromTypo
}

maxLockCheckInterval, err := parseInt(lockTimeout, DefaultLockTimeoutInterval)

if err != nil {
return nil, err
}
Expand All @@ -157,7 +173,7 @@ func (m *Mongo) Open(dsn string) (database.Driver, error) {
CollectionName: lockCollection,
Timeout: lockingTimout,
Enabled: advisoryLockingFlag,
Interval: maxLockingIntervals,
Interval: maxLockCheckInterval,
},
})
if err != nil {
Expand Down

0 comments on commit 4ba6957

Please sign in to comment.