Need some advice on multiple sockets and goroutines

101 views
Skip to first unread message

webuser 1300

unread,
Jan 6, 2022, 11:16:38 PM1/6/22
to golang-nuts
I'm designing an app which will be listening to messages on many sockets. 
- One goroutine per socket. 
- Each goroutine will do some processing on the messages to a summary datastructure. Only. That goroutine will be the only one writing to that datastructure
- There will be another goroutine that will read the summary data structures from above and take some action.

Performance is important so I want to copy as little data as possible and reduce the overhead of the GC. Is the preferred way of using a RW Mutex in each goroutine around writing and reading variables that will be accessed concurrently?

Brian Candler

unread,
Jan 7, 2022, 3:20:52 AM1/7/22
to golang-nuts
I don't think there's a hard-and-fast answer here.  You can pass pointers to objects to avoid copying, so it's really a question of how you synchronize the per-socket goroutines with the summarising goroutine.

I can think of different ways of doing this depending on the exact requirements.  One way would be to send them a message, they return a pointer to their object, and they pause until they are sent another message telling them it's OK to proceed.

Another way to avoid mutexes is to use a 1-deep buffered channel as a sort of 'holding area' for an object: push it into the channel when created, and when you need to use it,  pop it out, use it, then push it back in when done.  If you use a pointer to an object, or something which implicitly contains pointers (like a slice or a map) then this will be efficient.

I always point people to Bryan C. Mills excellent talk on "Rethinking Classical Concurrency Patterns":
It took me several views to get it, with lots of pauses to understand the code, but it was worth it.
Reply all
Reply to author
Forward
0 new messages