TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Creating a Proxy Server with Go

51 pointsby jokeofweekover 11 years ago

3 comments

tptacekover 11 years ago
I know it&#x27;s irrational, but it drives me a little nuts how the proxy idiom in go is &quot;two goroutines implementing socket-to-socket copy&quot;. The inner handler loop of a proxy is a place where, to me, select&#x2F;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&#x27;t free.<p>I know it&#x27;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.
评论 #6426961 未加载
评论 #6427828 未加载
评论 #6427489 未加载
mcot2over 11 years ago
I&#x27;m not sure I agree with how channels are used here. What&#x27;s the point of this spaces chan? Why couldn&#x27;t a simple atomic counter solve this (see sync&#x2F;atomic)? Why allocate a thousand bools?<p><pre><code> &#x2F;&#x2F; The booleans representing the free active connection spaces. spaces := make(chan bool, *maxConnections) &#x2F;&#x2F; Initialize the spaces for i := 0; i &lt; *maxConnections; i++ { spaces &lt;- true }</code></pre> }<p>Is this really how people use go???
评论 #6426897 未加载
评论 #6427338 未加载
评论 #6426664 未加载
评论 #6426850 未加载
评论 #6426983 未加载
farslanover 11 years ago
The inbuilt reverseproxy is also handy for small tasks:<p><pre><code> package main import ( &quot;net&#x2F;http&quot; &quot;net&#x2F;http&#x2F;httputil&quot; &quot;net&#x2F;url&quot; ) func main() { target, _ := url.Parse(&quot;http:&#x2F;&#x2F;127.0.0.1:8000&quot;) http.ListenAndServe(&quot;:80&quot;, httputil.NewSingleHostReverseProxy(target)) } </code></pre> This will http proxy :80 to :8000.