Design question -- a valid or foolhardy approach to ets update?

Steve Davis steven.charles.davis@REDACTED
Tue Jul 14 12:02:36 CEST 2009

I am maintaining HTTP session data as records in an ETS table. This
table is managed by a gen_server. However, each time the client makes
a request the session expiry time needs to be updated, which means
updating one field in the session record.

In order not to bottleneck the gen_server process by having all
clients contact the gen_server at every request, I'm considering a
direct ETS update of the session record from the client process rather
than doing a safe gen_server:call to update the record. i.e.:

%% TODO: probably needs a big fat review...
update_session(S = #session{key=Key}) ->
    case ets:lookup(?ETS, Key) of
	[_Existing] ->
		NewS = S#session{expires = unow() + S#session.ttl},
		ets:insert(?ETS, NewS);
	[] ->
		{error, not_found}

My reasoning is that since **only the client process** (meaning the
spawned HTTP connection) will ever access its own record in the
central ?ETS session table this approach should be safe to do... am I
correct in my thinking or am I badly wrong somewhere? Is there some
consideration I'm missing that could mean that this could result in
lock contention or deadlock?

Would I be better advised to at least surround the read/insert in a
try begin/end catch?



More information about the erlang-questions mailing list