本文介绍了使用Golang通道处理HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正尝试构建一个简单的Golang / Appengine应用程序,它使用一个频道来处理每个http请求。原因是我希望每个请求都能执行合理的大内存计算,并且每个请求都以线程安全的方式执行(即并发请求的计算不会混合),这一点很重要。 基本上我需要一个同步队列,它一次只能处理一个请求,并且通道看起来很自然。 =https://stackoverflow.com/questions/10424144/is-it-possible-to-use-gos-buffered-channel-as-a-thread-safe-queue>是否有可能使用Go的缓冲通道作为一个线程安全的队列? 然而,我无法得到我简单的Hello World例子的工作。它似乎在'go process(w,cr)'这一行上失败了;我从服务器得到了200个响应,但没有找到。如果我消除这条线上的'去',但工作正常,但我猜我没有正确调用频道。 任何人都指出我在哪里 谢谢! // curl -X POST http:// localhost:8080 / add-d{\A \:1,\B\:2} 包hello 导入(encoding / jsonnet / http) 类型MyStruct结构{ A,B,Total int64 $ b func(s * MyStruct)add(){ s.Total = sA + sB } func过程(w http.ResponseWriter,cr chan * http.Request){r:=< - cr var s MyStruct json.NewDecoder(r.Body).Decode(& s) s.add() json.NewEncoder(w).Encode(s)} func处理程序(w http.ResponseWriter,r * http.Request ){ cr:= make(chan * http.Request,1) cr go process(w,cr)//不起作用;没有响应:-( //进程(w,cr)//工作,但空白响应:-( ) func init(){ http .HandleFunc(/ add,handler)} 解决方案不确定这是否是正确的设计,但我怀疑问题在于,如果您开始第二个去程序,则第一个去程序会继续并完成写入连接等。 若要停止此操作,您可以使用waitgroup( http:// golang .org / pkg / sync /#WaitGroup )。 这阻止了为什么要将它放入一个线程的原因我认为你有设计问题)。 这是一些未经测试的代码,应该可以工作,或者至少有助于朝正确的方向发展。 b 包主 导入(编码/ json净/ http同步) 类型MyStruct结构{ A,B,共计int64 } func(s * MyStruct)add(){ s.Total = sA + sB func process(w http.ResponseWriter,cr chan * http.Request){r:=< - cr var s MyStruct json。 NewDecoder(r.Body).Decode(& s) s.add() json.NewEncoder(w).Encode(s)} func处理程序(w http.ResponseWriter,r * http.Request){ cr:= make(chan * http.Request,1) cr var请稍等sync.WaitGroup pleasewait.Add(1) 去func(){ defer pleasewait.Done() process(w,cr)//不起作用;没有回应:-( )() //进程(w,cr)//有效,但空白响应:-(请等待,等待()} func main(){ http.HandleFunc(/ add,处理程序)} I'm trying to build a simple Golang/Appengine app which uses a channel to handle each http request. Reason is I want each request to perform a reasonable large in- memory calculation, and it's important that each request is performed in a thread- safe manner (ie calculations from concurrent requests don't get mixed).Essentially I need a synchronous queue which will only process one request at a time, and channels look like a natural fit.Is it possible to use Go's buffered channel as a thread-safe queue?However I can't get my simple hello world example to work. It seems to fail on the line 'go process(w, cr)'; I get a 200 response from the server, but no contennt. Works fine if I eliminate 'go' from the this line, but then I'm guessing I'm not calling the channel correctly.Anyone point out where I'm going wrong ?Thanks!// curl -X POST "http://localhost:8080/add" -d "{\"A\":1, \"B\":2}"package helloimport ( "encoding/json" "net/http")type MyStruct struct { A, B, Total int64}func (s *MyStruct) add() { s.Total = s.A + s.B}func process(w http.ResponseWriter, cr chan *http.Request) { r := <- cr var s MyStruct json.NewDecoder(r.Body).Decode(&s) s.add() json.NewEncoder(w).Encode(s)}func handler(w http.ResponseWriter, r *http.Request) { cr := make(chan *http.Request, 1) cr <- r go process(w, cr) // doesn't work; no response :-( // process(w, cr) // works, but blank response :-(}func init() { http.HandleFunc("/add", handler)} 解决方案 Not sure this is the right design but I suspect that the issue is that where you're starting the second go routine the first go routine continues and finishes writing the connection etc.To stop this you can make the first routine wait using a waitgroup (http://golang.org/pkg/sync/#WaitGroup).This stop the whole reasoning behind why you're trying to put this into a thread (hence why I think you've got a design issue).Here is some untested code that should work or at least help in the right direction.package mainimport ( "encoding/json" "net/http" "sync")type MyStruct struct { A, B, Total int64}func (s *MyStruct) add() { s.Total = s.A + s.B}func process(w http.ResponseWriter, cr chan *http.Request) { r := <- cr var s MyStruct json.NewDecoder(r.Body).Decode(&s) s.add() json.NewEncoder(w).Encode(s)}func handler(w http.ResponseWriter, r *http.Request) { cr := make(chan *http.Request, 1) cr <- r var pleasewait sync.WaitGroup pleasewait.Add(1) go func() { defer pleasewait.Done() process(w, cr) // doesn't work; no response :-( }() // process(w, cr) // works, but blank response :-( pleasewait.Wait()}func main() { http.HandleFunc("/add", handler)} 这篇关于使用Golang通道处理HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 14:10