[erlang-questions] Can gen_tcp/inets store connection-specific user state?

Tony Rogvall <>
Tue Jul 20 13:57:55 CEST 2010


On 20 jul 2010, at 00.57, Tony Arcieri wrote:

> On Mon, Jul 19, 2010 at 4:48 PM, Tony Rogvall <> wrote:
> Why not use a global ets table? (use socket/port as a key)
> 
> Then you lose the O(1) properties of having the whole system round trip state for you.  In that regard I'd prefer to use a process per connection.
>  

Why do you lose the O(1) ? ets:lookup on a set table is O(1) ?  ordered_set is O(log(N)) .
Please enlighten me ;-)

> (There is a hidden way of doing it, but it's already taken by inet ;-)
> 
> Ooh really!  What's the hidden way of doing it?

It's not really hidden, it's in the source among the built in function. It will not solve your problem, not any 
better than a global ets table anyhow.

24> {ok,S} = gen_tcp:connect("www.google.org", 80, [{active,false}]).
{ok,#Port<0.2958>}
25> erlang:port_get_data(S).
inet_tcp

As you can see the data bound to the socket is the protocol module atom. This is use for dispatching
to the correct module when using gen_tcp/gen_udp.
You can call erlang:port_set_data(S, foo) but then the gen_tcp calls will not work anymore.

If you put anything other than a "constant" the data will be copied, since the garbage collector for a process
has a limited view. This is true for built in timers, ets tables etc.

> 
> I'm using this as part of implementing Reia and want Reia's socket objects included in the active mode messages as opposed to raw Erlang ports.  So I wouldn't be opposed to doing some crazy hacks so long as I can get them to peacefully coexist with inets.
>  

I have not peeked into Reia. What is the idea here? I think you will have to copy data enyway!

/Tony

> -- 
> Tony Arcieri
> Medioh! A Kudelski Brand



More information about the erlang-questions mailing list