func (l *lock) TryLock(ctx context.Context, key string) (bool, error) {
	l.mu.Lock()
	defer l.mu.Unlock()

	if l.locked {
		return false, nil
	}

	keyInt64 := crushkey(key)

	// start transaction
	tx, err := l.db.Beginx()
	if err != nil {
		return false, err
	}

	// attempt to acquire lock
	var acquired bool

	err = tx.Get(&acquired, manifestAdvisoryLock, keyInt64)
	if err != nil {
		tx.Rollback()
		return false, fmt.Errorf("failed to issue pg_advisory lock query: %v", err)
	}

	// if acquired set lock status which unblocks caller waiting on Lock() method
	// and populate tx which will be Commited on Unlock()
	if acquired {
		l.locked = true
		// hold this tx until Unlock() is called!
		l.tx = tx
		return true, nil
	}

	// we did not acquire the lock
	tx.Rollback()
	return false, nil
}
// TryLock will begin a transaction and then attempt to acquire a pg_advisory transaction
// lock. On success it will set sl.locked = true and populate sl.tx with the transaction
// holding the lock. on any errors we rollback the transcation (unlocking any other scanLocks)
// and return the error. if this process dies a TCP reset will be sent to postgres and the transaction
// will be closed allowing other scanLocks to acquire.
TryLock will begin a transaction and then attempt to acquire a pg_advisory transaction
lock. On success it will set sl.locked = true and populate sl.tx with the transaction
holding the lock. on any errors we rollback the transcation (unlocking any other scanLocks)
and return the error. if this process dies a TCP reset will be sent to postgres and the transaction
will be closed allowing other scanLocks to acquire.
// TryLock will begin a transaction and then attempt to acquire a pg_advisory transaction // lock . On success it will set sl . locked = true and populate sl . tx with the transaction // holding the lock . on any errors we rollback the transcation ( unlocking any other scanLocks ) // and return the error . if this process dies a TCP reset will be sent to postgres and the transaction // will be closed allowing other scanLocks to acquire .
func (l *lock) TryLock(ctx context.Context, key string) (bool, error) {
	l.mu.Lock()
	defer l.mu.Unlock()
	if l.locked {
		return false, nil
	}
	keyInt64 := crushkey(key)
	tx, err := l.db.Beginx()
	if err != nil {
		return false, err
	}
	var acquired bool
	err = tx.Get(&acquired, manifestAdvisoryLock, keyInt64)
	if err != nil {
		tx.Rollback()
		return false, fmt.Errorf("failed to issue pg_advisory lock query: %v", err)
	}
	if acquired {
		l.locked = true
		l.tx = tx
		return true, nil
	}
	tx.Rollback()
	return false, nil
}
func ( l * lock ) TryLock ( ctx context . Context , key string ) ( bool , error ) { l . mu . Lock ( ) defer l . mu . Unlock ( ) if l . locked { return false , nil } keyInt64 := crushkey ( key ) tx , err := l . db . Beginx ( ) if err != nil { return false , err } var acquired bool err = tx . Get ( & acquired , manifestAdvisoryLock , keyInt64 ) if err != nil { tx . Rollback ( ) return false , fmt . Errorf ( " " , err ) } if acquired { l . locked = true l . tx = tx return true , nil } tx . Rollback ( ) return false , nil }
TryLock will begin a transaction and then attempt to acquire a pg_advisory transaction
lock.
TryLock will begin a transaction and then attempt to acquire a pg_advisory transaction lock .
// start transaction // attempt to acquire lock // if acquired set lock status which unblocks caller waiting on Lock() method // and populate tx which will be Commited on Unlock() // hold this tx until Unlock() is called! // we did not acquire the lock