Live data from Hacker News

Creating a Proxy Server with Go

codingcookies.com

1–10 of 20 posts

Re: Creating a Proxy Server with Go

#2
I'm not sure I agree with how channels are used here. What's the point of this spaces chan? Why couldn't a simple atomic counter solve this (see sync/atomic)? Why allocate a thousand bools?

    // The booleans representing the free active connection spaces.
    spaces := make(chan bool, *maxConnections)
    // Initialize the spaces
    for i := 0; i 
}

Is this really how people use go???

Re: Creating a Proxy Server with Go

#3
post #2

I'm not sure I agree with how channels are used here. What's the point of this spaces chan? Why couldn't a simple atomic counter solve this (see sync/atomic)? Why allocate a thousand bools? // The booleans representing the free active connection spaces. spaces := make(chan bool, *maxConnections) // Initialize the spaces for i := 0; i } Is this really how people use go???

Looking at this some more... Why wouldn't you just make the waiting chan buffered and eliminate any tracking of connections.

Re: Creating a Proxy Server with Go

#4
post #3
post #2

I'm not sure I agree with how channels are used here. What's the point of this spaces chan? Why couldn't a simple atomic counter solve this (see sync/atomic)? Why allocate a thousand bools? // The booleans representing the free active connection spaces. spaces := make(chan bool, *maxConnections) // Initialize the spaces for i := 0; i } Is this really how people use go???

Looking at this some more... Why wouldn't you just make the waiting chan buffered and eliminate any tracking of connections.

Hey, poster here.

You're right that sync/atomic could've taken care of this, I wasn't aware of that package and figured channels were the way to go in Go.

As for making the waiting chan buffered, the reason I wanted to keep track of pending connections and active connections is because I'd like to proxy from a high-power server to a low-power server such as a Raspberry Pi. I agree with you that it could have done without though.

Thanks for the tips! :-)

Re: Creating a Proxy Server with Go

#5
post #3

Earlier quoted context omitted.

Looking at this some more... Why wouldn't you just make the waiting chan buffered and eliminate any tracking of connections.

Hey, poster here. You're right that sync/atomic could've taken care of this, I wasn't aware of that package and figured channels were the way to go in Go. As for making the waiting chan buffered, the reason I wanted to keep track of pending connections and active connections is because I'd like to proxy from a high-power server to a low-power server such as a Raspberry Pi. I agree with you that it could have done wit…

So perhaps I was a bit harsh. Do check out this set of slides though: http://talks.golang.org/2012/concurrency.slide

One of the last slides: http://talks.golang.org/2012/concurrency.slide#54

Re: Creating a Proxy Server with Go

#6
post #5

Earlier quoted context omitted.

Hey, poster here. You're right that sync/atomic could've taken care of this, I wasn't aware of that package and figured channels were the way to go in Go. As for making the waiting chan buffered, the reason I wanted to keep track of pending connections and active connections is because I'd like to proxy from a high-power server to a low-power server such as a Raspberry Pi. I agree with you that it could have done wit…

So perhaps I was a bit harsh. Do check out this set of slides though: http://talks.golang.org/2012/concurrency.slide One of the last slides: http://talks.golang.org/2012/concurrency.slide#54

Thanks, that's an awesome resource!

Re: Creating a Proxy Server with Go

#7
post #2

I'm not sure I agree with how channels are used here. What's the point of this spaces chan? Why couldn't a simple atomic counter solve this (see sync/atomic)? Why allocate a thousand bools? // The booleans representing the free active connection spaces. spaces := make(chan bool, *maxConnections) // Initialize the spaces for i := 0; i } Is this really how people use go???

I think in this case using atmoic.Add* is much clearer, and probably better.

Re: Creating a Proxy Server with Go

#8
post #2

I'm not sure I agree with how channels are used here. What's the point of this spaces chan? Why couldn't a simple atomic counter solve this (see sync/atomic)? Why allocate a thousand bools? // The booleans representing the free active connection spaces. spaces := make(chan bool, *maxConnections) // Initialize the spaces for i := 0; i } Is this really how people use go???

This is generally the Go way of limiting concurrency and usually the recommended approach. Some use empty structs in place of bools, but otherwise there doesn't seem to be a problem with the approach.

Re: Creating a Proxy Server with Go

#9
I know it's irrational, but it drives me a little nuts how the proxy idiom in go is "two goroutines implementing socket-to-socket copy". The inner handler loop of a proxy is a place where, to me, select/poll might actually make the code easier to follow; also, the idiom doubles the number of goroutines required to handle a given connection load, and while goroutines are cheap, they aren't free.

I know it's possible to pull select() into Golang programs (I ended up having to, to write a fast port scanner), but Golang people look at you weirdly when you tell them you did that.

Re: Creating a Proxy Server with Go

#10
post #9

I know it's irrational, but it drives me a little nuts how the proxy idiom in go is "two goroutines implementing socket-to-socket copy". The inner handler loop of a proxy is a place where, to me, select/poll might actually make the code easier to follow; also, the idiom doubles the number of goroutines required to handle a given connection load, and while goroutines are cheap, they aren't free. I know it's possible t…

Go is strongly opinionated and very normative (which is a good thing for its main target audience). It's not surprising it attracts people who would look down to you for departing from the norm, even if your choices are technically justified.
Post reply on HN