multiChan design

242 views
Skip to first unread message

Jacob Arthur

unread,
Dec 19, 2014, 4:45:21 PM12/19/14
to golan...@googlegroups.com
I don't really know how to describe this so it was hard to search if anyone else has come up with something similar and/or better for what I'm trying to do.

http://play.golang.org/p/izPUTlxU3J

Basically I wanted to create a wrapper around a channel that can handle the opening and closing the comm from multiple go routines and fan it into a standard channel. I can't be the only person who has needed this so I really wanted to see how other people solved the problem, and if not at least share the way I thought about it.

Thanks,
Jacob





Jesper Louis Andersen

unread,
Dec 21, 2014, 7:44:40 AM12/21/14
to Jacob Arthur, golan...@googlegroups.com
If I understand your code example correctly, you want a reference count on the channel so you can clean up when no more needs to send data fan-in on the channel. I'd probably use a sync.WaitGroup for this, or at least explore why that wouldn't work out in practice.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

wkharold

unread,
Dec 23, 2014, 11:45:43 AM12/23/14
to golan...@googlegroups.com
Perhaps you can reuse the fan-in aspect of the pipeline approach http://blog.golang.org/pipelines

Jacob Arthur

unread,
Dec 23, 2014, 2:29:20 PM12/23/14
to golan...@googlegroups.com
I've been looking into the WaitGroup and I don't think it will work with what I am trying to do. It seems that the WaitGroup is useful if you already know how many goroutines you want to create. I don't think that it fulfills my desire to have a ref counted channel.

wkharold

unread,
Dec 23, 2014, 4:42:56 PM12/23/14
to golan...@googlegroups.com
This is what I was suggesting: http://play.golang.org/p/GVqMtk8mLK

Jacob Arthur

unread,
Dec 23, 2014, 6:39:44 PM12/23/14
to wkharold, golan...@googlegroups.com
Yes but this also requires you to have all the channels before you merge them. My use case is to use filepath.Walk and process some files then send the processed data over a channel to be aggregated. Since the number of go routines that I create will vary depending on how many files I find I can't use this type of pipeline.

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/zlq-OM3ypq0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.

wkharold

unread,
Dec 23, 2014, 6:52:42 PM12/23/14
to golan...@googlegroups.com, wkha...@gmail.com
It wouldn't be hard to change the merge function so accept a channel of channels so that you could run it in it's own goroutine and feed it channels to merge dynamically. In fact, I'm doing pretty much that with some code I use to load file metadata into a database. I did too much reference counting when I was writing C++ code, your mileage may vary but it makes my skin crawl.
Reply all
Reply to author
Forward
0 new messages