[Haskell-cafe] Bounded STM Queues with load shedding

12 views
Skip to first unread message

Alex Hammel

unread,
Feb 28, 2015, 11:31:57 AM2/28/15
to Haskell Café

Forwarding from Haskell-Beginners:

Hi list,

It would be handy for me to have a bounded STM Queue along the lines of TBQueue with the difference that when the queue is full, a call to writeTBQueue is a non-blocking no-op.

This is in the context of an asynchronous logging system. If the load (somehow) gets heavy enough that the log message queue is taking up a lot of memory, It's better to lose a few log messages than to have processes block until the logger catches up.

Are there any off-the-shelf solutions?

Cheers,
Alex

Yuras Shumovich

unread,
Feb 28, 2015, 11:51:43 AM2/28/15
to Alex Hammel, Haskell Café
You can try the stm-chans package:
http://hackage.haskell.org/package/stm-chans-3.0.0.2/docs/Control-Concurrent-STM-TBMQueue.html#v:tryWriteTBMQueue

Thanks,
Yuras

>
> Cheers,
> Alex
> _______________________________________________
> Haskell-Cafe mailing list
> Haskel...@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Alex Hammel

unread,
Feb 28, 2015, 11:56:45 AM2/28/15
to Haskell Café

Perfect, thanks!

Ryan Yates

unread,
Feb 28, 2015, 2:11:53 PM2/28/15
to Alex Hammel, Haskell Café
Note that this is what orElse is for, turning a blocking API unto a choice.  You could write tryWriteTBQueue as:

    tryWriteTBQueue queue v = (writeTBQueue queue v >> return True) `orElse` (return False)

Ryan
Reply all
Reply to author
Forward
0 new messages