2023-03-12 15:00:57 +00:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-05-27 14:06:24 +00:00
|
|
|
|
|
|
|
package federatingdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Lock takes a lock for the object at the specified id. If an error
|
|
|
|
// is returned, the lock must not have been taken.
|
|
|
|
//
|
|
|
|
// The lock must be able to succeed for an id that does not exist in
|
|
|
|
// the database. This means acquiring the lock does not guarantee the
|
|
|
|
// entry exists in the database.
|
|
|
|
//
|
|
|
|
// Locks are encouraged to be lightweight and in the Go layer, as some
|
|
|
|
// processes require tight loops acquiring and releasing locks.
|
|
|
|
//
|
|
|
|
// Used to ensure race conditions in multiple requests do not occur.
|
2022-04-28 09:18:27 +00:00
|
|
|
func (f *federatingDB) Lock(c context.Context, id *url.URL) (func(), error) {
|
2021-05-29 17:36:54 +00:00
|
|
|
if id == nil {
|
2022-04-28 09:18:27 +00:00
|
|
|
return nil, errors.New("Lock: id was nil")
|
2021-05-29 17:36:54 +00:00
|
|
|
}
|
2022-04-28 09:18:27 +00:00
|
|
|
unlock := f.locks.Lock(id.String())
|
|
|
|
return unlock, nil
|
2021-08-27 19:14:20 +00:00
|
|
|
}
|