TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Creating a Proxy Server with Go

51 点作者 jokeofweek超过 11 年前

3 条评论

tptacek超过 11 年前
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 未加载
mcot2超过 11 年前
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 未加载
farslan超过 11 年前
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.