Errors with counters #17

Closed
denen99 opened this Issue Mar 27, 2012 · 4 comments

Projects

None yet

2 participants

@denen99
denen99 commented Mar 27, 2012

hello,

I am trying to keep track of the number of connected users to a process inside of cowboy (for a websocket connection).

My code is as follows:

Inside of cowboy websocket_init:

gproc:reg({c,l,wsCounter}),
gproc:update_shared_counter({c,l,wsCounter},1)

Error Stacktrace:

** Stacktrace: [{ets,update_counter,[gproc,{{c,l,wsCounter},shared},{3,1}],[]},
{gproc_lib,update_counter,3,
[{file,"src/gproc_lib.erl"},{line,370}]},
{gproc,update_shared_counter,2,
[{file,"src/gproc.erl"},{line,1299}]},
{erltest1_http_handler,websocket_init,3,
[{file,"src/erltest1_http_handler.erl"},{line,46}]},

Any thoughts?

Thanks
-A

@uwiger
Owner
uwiger commented Mar 27, 2012

Hi Adam,

You need to create shared counters using gproc:reg_shared().

The counter you created with gproc:reg/1 is not a shared counter, but a
'normal' one (registered to the current process). Thus,
update_shared_counter/2 can't find it.

BR,
Ulf W

Den 27 mars 2012 15:21 skrev Adam Denenberg <
reply@reply.github.com

:

hello,

I am trying to keep track of the number of connected users to a process
inside of cowboy (for a websocket connection).

My code is as follows:

Inside of cowboy websocket_init:

gproc:reg({c,l,wsCounter}),
gproc:update_shared_counter({c,l,wsCounter},1)

Error Stacktrace:

** Stacktrace:
[{ets,update_counter,[gproc,{{c,l,wsCounter},shared},{3,1}],[]},
{gproc_lib,update_counter,3,
[{file,"src/gproc_lib.erl"},{line,370}]},
{gproc,update_shared_counter,2,
[{file,"src/gproc.erl"},{line,1299}]},
{erltest1_http_handler,websocket_init,3,
[{file,"src/erltest1_http_handler.erl"},{line,46}]},

Any thoughts?

Thanks
-A


Reply to this email directly or view it on GitHub:
#17

@denen99
denen99 commented Mar 27, 2012

thanks, getting closer.. i did

gproc:reg_shared({c,l,wsCounter}),
gproc:update_shared_counter({c,l,wsCounter},1),
io:format("cowboy: Current count is pn,",[gproc:get_value({c,l,wsCounter})]),

but its throwing:

** Stacktrace: [{ets,lookup_element,[gproc,{{c,l,wsCounter},<0.164.0>},3],[]},
{gproc,get_value1,2,[{file,"src/gproc.erl"},{line,1087}]},
{gproc,get_value,1,[{file,"src/gproc.erl"},{line,1068}]},
{erltest1_http_handler,websocket_init,3,
[{file,"src/erltest1_http_handler.erl"},{line,47}]},

Thanks
-A

@denen99
denen99 commented Mar 27, 2012

nevermind i got it. I had to move reg_shared into my startup function not the websocket init handler.

so i ended up with just

gproc:update_shared_counter({c,l,wsCounter},1), in websocket_init

and

gproc:update_shared_counter({c,l,wsCounter},-1) in websocket_terminate

@uwiger
Owner
uwiger commented Mar 27, 2012

If you want to call get_value() on a shared counter, you must use
gproc:get_value/2,
e.g.

gproc:get_value({c,l,wsCounter}, shared).

BR,
Ulf W

Den 27 mars 2012 16:04 skrev Adam Denenberg <
reply@reply.github.com

:

thanks, getting closer.. i did

gproc:reg_shared({c,l,wsCounter}),
gproc:update_shared_counter({c,l,wsCounter},1),
io:format("cowboy: Current count is
pn,",[gproc:get_value({c,l,wsCounter})]),

but its throwing:

** Stacktrace:
[{ets,lookup_element,[gproc,{{c,l,wsCounter},<0.164.0>},3],[]},
{gproc,get_value1,2,[{file,"src/gproc.erl"},{line,1087}]},
{gproc,get_value,1,[{file,"src/gproc.erl"},{line,1068}]},
{erltest1_http_handler,websocket_init,3,
[{file,"src/erltest1_http_handler.erl"},{line,47}]},

Thanks
-A


Reply to this email directly or view it on GitHub:
#17 (comment)

@l4u l4u referenced this issue Dec 31, 2012
Closed

set_value/3 #32

@uwiger uwiger closed this May 24, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment