Skip to content

Instantly share code, notes, and snippets.

@bitemyapp
Forked from jlouis/foo.patch
Last active August 29, 2015 14:08
Show Gist options
  • Save bitemyapp/f13a1104b7962c527718 to your computer and use it in GitHub Desktop.
Save bitemyapp/f13a1104b7962c527718 to your computer and use it in GitHub Desktop.
diff --git a/apps/flake/src/flake_harness.erl b/apps/flake/src/flake_harness.erl
index 0657f03..6980cb3 100644
--- a/apps/flake/src/flake_harness.erl
+++ b/apps/flake/src/flake_harness.erl
@@ -18,33 +18,35 @@
-author ('Dietrich Featherston <[email protected]>').
-export ([
- generate/1,
- generate/2,
- timed_generate/1,
- timed_generate/2
+ generate/2
]).
-include_lib("eunit/include/eunit.hrl").
-generate(N) ->
- generate_ids(N, undefined, []).
-
-timed_generate(N) ->
- ?debugTime("generating ids", generate(N)).
-
-generate(N, Base) ->
- generate_ids(N, Base, []).
-
-timed_generate(N, Base) ->
- ?debugTime("generating ids", generate(N, Base)).
+generate(K, N) ->
+ Self = self(),
+ gather([spawn_worker(Self, N div K) || _ <- lists:seq(1, K)]).
+
+gather([]) -> ok;
+gather([_ | T]) ->
+ receive
+ done ->
+ gather(T)
+ after 5000 ->
+ exit(timeout)
+ end.
+
+spawn_worker(Parent, N) ->
+ spawn(fun() ->
+ generate_ids(N, undefined, []),
+ Parent ! done
+ end).
generate_ids(0, _Base, Acc) ->
Acc;
+generate_ids(N, undefined, Acc) ->
+ {ok, Flake} = flake_server:id(),
+ generate_ids(N-1, undefined, [Flake|Acc]);
generate_ids(N, Base, Acc) ->
- {ok, Flake} = case Base of
- undefined ->
- flake_server:id();
- _ ->
- flake_server:id(Base)
- end,
+ {ok, Flake} = flake_server:id(Base),
generate_ids(N-1, Base, [Flake|Acc]).
diff --git a/apps/flake/src/flake_server.erl b/apps/flake/src/flake_server.erl
index 558a158..bed97b5 100644
--- a/apps/flake/src/flake_server.erl
+++ b/apps/flake/src/flake_server.erl
@@ -19,6 +19,8 @@
-behaviour (gen_server).
+-compile({inline, [curr_time_millis/0]}).
+
%% API
-export ([
start_link/1,
@@ -67,14 +69,14 @@ respond(X) ->
%% ----------------------------------------------------------
init([{worker_id, WorkerId}]) ->
- {ok, #state{max_time=flake_util:curr_time_millis(), worker_id=WorkerId, sequence=0}}.
+ {ok, #state{max_time=curr_time_millis(), worker_id=WorkerId, sequence=0}}.
handle_call(get, _From, State = #state{max_time=MaxTime, worker_id=WorkerId, sequence=Sequence}) ->
- {Resp, S0} = get(flake_util:curr_time_millis(), MaxTime, WorkerId, Sequence, State),
+ {Resp, S0} = get(curr_time_millis(), MaxTime, WorkerId, Sequence, State),
{reply, Resp, S0};
handle_call({get,Base}, _From, State = #state{max_time=MaxTime,worker_id=WorkerId,sequence=Sequence}) ->
- {Resp, S0} = get(flake_util:curr_time_millis(), MaxTime, WorkerId, Sequence, State),
+ {Resp, S0} = get(curr_time_millis(), MaxTime, WorkerId, Sequence, State),
case Resp of
{ok, Id} ->
<<IntId:128/integer>> = Id,
@@ -98,10 +100,16 @@ code_change(_, State, _) -> {ok, State}.
%% clock hasn't moved, increment sequence
get(Time,Time,WorkerId,Seq0,State) ->
Sequence = Seq0 + 1,
- {{ok,flake_util:gen_id(Time,WorkerId,Sequence)},State#state{sequence=Sequence}};
+ {{ok,<<Time:64/integer, WorkerId:48/integer, Sequence:16/integer>>},
+ State#state{sequence=Sequence}};
%% clock has progressed, reset sequence
get(CurrTime,MaxTime,WorkerId,_,State) when CurrTime > MaxTime ->
- {{ok, flake_util:gen_id(CurrTime, WorkerId, 0)}, State#state{max_time=CurrTime, sequence=0}};
+ {{ok, <<CurrTime:64/integer, WorkerId:48/integer, 0:16/integer>>}, State#state{max_time=CurrTime, sequence=0}};
%% clock is running backwards
get(CurrTime, MaxTime, _WorkerId, _Sequence, State) when MaxTime > CurrTime ->
{{error, clock_running_backwards}, State}.
+
+curr_time_millis() ->
+ {MegaSec,Sec, MicroSec} = erlang:now(),
+ 1000000000*MegaSec + Sec*1000 + (MicroSec div 1000).
+
diff --git a/rel/files/sys.config b/rel/files/sys.config
index 2f2bada..5042080 100644
--- a/rel/files/sys.config
+++ b/rel/files/sys.config
@@ -1,6 +1,6 @@
[
{flake, [
- {interface, "en0"},
+ {interface, "eth0"},
{timestamp_path, "/tmp/flake-timestamp-dets"},
{allowable_downtime, 2592000000}
]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment