diff options
author | Francesco Mazzoli <francesco@rabbitmq.com> | 2012-01-17 15:04:04 +0000 |
---|---|---|
committer | Francesco Mazzoli <francesco@rabbitmq.com> | 2012-01-17 15:04:04 +0000 |
commit | e824250c89a1203b9d51868afd4df925e494b1ae (patch) | |
tree | 1061e0e647e0999c1d17dec43debefd709506d16 /src/rabbit_guid.erl | |
parent | 065f57944a56b7d8183dfa04761247b5b760fdef (diff) | |
download | rabbitmq-server-e824250c89a1203b9d51868afd4df925e494b1ae.tar.gz |
Change naming in rabbit_guid, explicit guid passing in string/0 and binary/0.
Specifically:
fast_guid() -> gen()
guid() -> secure_gen()
string_guid(Prefix) -> string(Guid, Prefix)
binstring_guid(Prefix) -> binary(Guid, Prefix)
Diffstat (limited to 'src/rabbit_guid.erl')
-rw-r--r-- | src/rabbit_guid.erl | 59 |
1 files changed, 29 insertions, 30 deletions
diff --git a/src/rabbit_guid.erl b/src/rabbit_guid.erl index 9a464139..5068ad1f 100644 --- a/src/rabbit_guid.erl +++ b/src/rabbit_guid.erl @@ -19,7 +19,7 @@ -behaviour(gen_server). -export([start_link/0]). --export([guid/0, fast_guid/0, string_guid/1, binstring_guid/1]). +-export([gen/0, gen_secure/0, string/2, binary/2]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). @@ -38,10 +38,10 @@ -type(guid() :: binary()). -spec(start_link/0 :: () -> rabbit_types:ok_pid_or_error()). --spec(guid/0 :: () -> guid()). --spec(fast_guid/0 :: () -> guid()). --spec(string_guid/1 :: (any()) -> string()). --spec(binstring_guid/1 :: (any()) -> binary()). +-spec(gen/0 :: () -> guid()). +-spec(gen_secure/0 :: () -> guid()). +-spec(string/2 :: (guid(), any()) -> string()). +-spec(binary/2 :: (guid(), any()) -> binary()). -endif. @@ -66,9 +66,8 @@ update_disk_serial() -> end, Serial. -%% Generate an un-hashed guid. To be used when one is hashed in the process -%% dictionary. -fresh_guid() -> +%% Generate an un-hashed guid. +fresh() -> %% We don't use erlang:now() here because a) it may return %% duplicates when the system clock has been rewound prior to a %% restart, or ids were generated at a high rate (which causes @@ -81,50 +80,50 @@ fresh_guid() -> Serial = gen_server:call(?SERVER, serial, infinity), {Serial, node(), make_ref()}. +%% generate a GUID. This function should be used when performance is a +%% priority and predictability is not an issue. Otherwise, use gen_secure/0. +gen() -> + %% We hash a fresh GUID and then XOR it with a process-local counter + %% to get a new GUID each time. + {S, I} = + case get(guid) of + undefined -> G = fresh(), + <<S0:128/integer>> = erlang:md5(term_to_binary(G)), + {S0, 0}; + {S0, I0} -> {S0, I0+1} + end, + put(guid, {S, I}), + <<(S bxor I):128>>. + %% generate a non-predictable GUID. %% %% The id is only unique within a single cluster and as long as the %% serial store hasn't been deleted. %% -%% If you are not concerned with predictability, fast_guid/0 is faster. -guid() -> +%% If you are not concerned with predictability, gen/0 is faster. +gen_secure() -> %% Here instead of hashing once we hash the GUID and the counter each time, %% so that the GUID is not predictable. G = case get(fast_guid) of - undefined -> {fresh_guid(), 0}; + undefined -> {fresh(), 0}; {S, I} -> {S, I+1} end, put(fast_guid, G), erlang:md5(term_to_binary(G)). -%% generate a GUID. This function should be used when performance is a -%% priority and predictability is not an issue. -fast_guid() -> - %% We hash a fresh GUID and then XOR it with a process-local counter - %% to get a new GUID each time. - {S, I} = - case get(guid) of - undefined -> G = fresh_guid(), - <<S0:128/integer>> = erlang:md5(term_to_binary(G)), - {S0, 0}; - {S0, I0} -> {S0, I0+1} - end, - put(guid, {S, I}), - <<(S bxor I):128>>. - %% generate a readable string representation of a GUID. %% %% employs base64url encoding, which is safer in more contexts than %% plain base64. -string_guid(Prefix) -> +string(G, Prefix) -> Prefix ++ "-" ++ lists:foldl(fun ($\+, Acc) -> [$\- | Acc]; ($\/, Acc) -> [$\_ | Acc]; ($\=, Acc) -> Acc; (Chr, Acc) -> [Chr | Acc] - end, [], base64:encode_to_string(guid())). + end, [], base64:encode_to_string(G)). -binstring_guid(Prefix) -> - list_to_binary(string_guid(Prefix)). +binary(G, Prefix) -> + list_to_binary(string(G, Prefix)). %%---------------------------------------------------------------------------- |