Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 107 lines (89 sloc) 3.313 kb
0761bb2 @nthauvin Pluggable ysession storage with mnesia sample in src/contrib
nthauvin authored
1 %%%----------------------------------------------------------------------
2 %%% File : yaws_mnesia_session.erl
3 %%% Author : Nicolas Thauvin <nicolas@corporama.com>
4 %%% Purpose : mnesia storage callbacks for yaws_session_server
5 %%%----------------------------------------------------------------------
6
7 %% Warning !!!
8 %% This module may init mnesia in a way that is not suitable for your
9 %% application !
10
11 %% Quick steps:
12 %% 1) Compile this module
13 %% 2) Drop the .beam in your code path
455578a @vinoski major trailing whitespace cleanup
vinoski authored
14 %% 3) Run Yaws with the
0761bb2 @nthauvin Pluggable ysession storage with mnesia sample in src/contrib
nthauvin authored
15
16 -module (yaws_mnesia_session).
17
18 -export ([init_backend/1, stop_backend/0]).
19 -export ([list/0, insert/1, lookup/1, delete/1]).
20 -export ([traverse/1, cleanup/0]).
21
22 -define(TABLE, ysession).
23
24 init_backend(Fields) ->
c6f26aa @nthauvin Pluggable ysession storage with mnesia sample in src/contrib, part 2
nthauvin authored
25 case net_kernel:get_net_ticktime() of
26 ignored ->
27 Message = "mnesia backend needs distribution (a node name)",
28 Error = {?MODULE, Message},
29 error_logger:error_msg("~p~n", [Error]),
30 exit (Error);
31 _ ->
32 ok
33 end,
0761bb2 @nthauvin Pluggable ysession storage with mnesia sample in src/contrib
nthauvin authored
34 case catch mnesia:table_info(schema, where_to_write) of
35 Nodes when is_list(Nodes) ->
36 case catch mnesia:table_info(?TABLE, where_to_write) of
37 {'EXIT', {aborted, {no_exists, ?TABLE, where_to_write}}} ->
38 Nodes = mnesia:system_info(running_db_nodes),
39 Options = [{disc_copies, Nodes}, {type, set},
40 {attributes, Fields}],
41 {atomic, ok} = mnesia: create_table (?TABLE, Options),
42 ok;
43 List when is_list (List) ->
44 ok = mnesia:wait_for_tables([?TABLE], 60000)
45 end;
46 {'EXIT',{aborted,{no_exists,schema,where_to_write}}} ->
47 application:stop(mnesia),
48 mnesia:create_schema([node()]),
49 ok = application:start(mnesia),
50 init_backend(Fields)
51 end.
52
53 stop_backend() ->
54 ok.
55
56 insert(Session) ->
57 Fun = fun () -> mnesia:write(Session) end,
58 {atomic, ok} = mnesia:transaction(Fun),
59 true.
60
455578a @vinoski major trailing whitespace cleanup
vinoski authored
61 lookup(Key) ->
0761bb2 @nthauvin Pluggable ysession storage with mnesia sample in src/contrib
nthauvin authored
62 Fun = fun () -> mnesia:read(?TABLE, Key) end,
63 {atomic, Result} = mnesia:transaction(Fun),
64 Result.
65
66 delete(Key) ->
67 Fun = fun () -> mnesia:delete({?TABLE, Key}) end,
68 {atomic, Result} = mnesia:transaction(Fun),
69 Result.
70
71 list() ->
72 Fold = fun (Session, Acc) -> [Session | Acc] end,
73 Fun = fun () -> mnesia:foldl(Fold, [], ?TABLE) end,
74 {atomic, Result} = mnesia:transaction(Fun),
75 Result.
76
77 cleanup() ->
78 mnesia:clear_table(?TABLE).
79
80 traverse(Gnow) ->
81 Fun = fun() -> tr_traverse(Gnow) end,
82 {atomic, Result} = mnesia:transaction(Fun),
83 Result.
84
85 tr_traverse(Gnow) ->
86 tr_traverse(Gnow, mnesia:first(?TABLE)).
87
88 tr_traverse(_N, '$end_of_table') ->
89 ok;
90 tr_traverse(N, Key) ->
91 case mnesia:read(?TABLE, Key) of
92 [Y] ->
93 case yaws_session_server:has_timedout(Y, N) of
94 false ->
95 tr_traverse(N, mnesia:next(?TABLE, Key));
96 true ->
97 yaws_session_server:report_timedout_sess(Y),
98 Next = mnesia:next(?TABLE, Key),
99 mnesia:delete({?TABLE, Key}),
455578a @vinoski major trailing whitespace cleanup
vinoski authored
100 tr_traverse(N, Next)
0761bb2 @nthauvin Pluggable ysession storage with mnesia sample in src/contrib
nthauvin authored
101 end;
102 [] ->
103 tr_traverse(N, mnesia:next(?MODULE, Key))
104 end.
105
106
Something went wrong with that request. Please try again.