Add a locker module

This commit is contained in:
Feng Lee 2018-03-10 13:28:14 +08:00
parent 22350f9117
commit f218c6a35d
1 changed files with 41 additions and 0 deletions

41
src/emqx_locker.erl Normal file
View File

@ -0,0 +1,41 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2013-2018 EMQ Enterprise, Inc. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(emqx_locker).
-export([start_link/0]).
%% Lock/Unlock API based on canal-lock.
-export([lock/1, unlock/1]).
%% @doc Starts the lock server
-spec(start_link() -> {ok, pid()} | ignore | {error, any()}).
start_link() ->
canal_lock:start_link(?MODULE, 1).
%% @doc Lock a Key
-spec(lock(binary()) -> boolean()).
lock(Key) ->
case canal_lock:acquire(?MODULE, Key, 1, 1) of
{acquired, 1} -> true;
full -> false
end.
%% @doc Unlock a Key
-spec(unlock(binary()) -> ok).
unlock(Key) ->
canal_lock:release(?MODULE, Key, 1, 1).