go http服务器性能,golang 并发的http服务器

首先,我使用了github.com/ant0ine/go-json-rest/rest 这个包

直接贴代码

func Start(){

NeptuneLog := logs.New()

api := rest.NewApi()

api.Use(rest.DefaultDevStack...)

router, err := rest.MakeRouter(

rest.Get("/:type/:action", handle),

rest.Post("/:type/:action", handle),

)

if err != nil {

NeptuneLog.Fatal(err)

}

api.SetApp(router)

NeptuneLog.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))

}

=================start 负责调用 handle ====================

func handle (w rest.ResponseWriter, r *rest.Request) {

NeptuneLog := logs.New()

t := r.PathParam("type")

if (t == "submit") {

action := r.PathParam("action")

submitNode := SubmitNode{R:r,W:w,Action:action}

SubmitChan

NeptuneLog.Printf("%v",w)

//node := test11{Action:action}

//w.WriteJson(node)

} else if t =="browser" {

action := r.PathParam("action")

browserNode := BrowserNode{R:r,W:w,Action:action}

BrowserChan

} else {

//w.WriteJson("not found")

}

}

============= handle 负责把request 和 response 放到结构体,然后放到channel ============

func doSubmit () {

NeptuneLog := logs.New()

for item := range SubmitChan{

NeptuneLog.Println("111")

node := test11{Action:item.Action}

NeptuneLog.Printf("%v",node)

NeptuneLog.Printf("%v",item.W)

item.W.WriteJson(node)

NeptuneLog.Printf("%v",item.W)

}

}

=========== dosubmit 真正干活,负责从channel拿东西,然后返回 ======

调用之后,server打印出了

http: multiple response.WriteHeader calls

的错误

==========更新下最新进展 ========

我把打印的返回拿出来的

发现有Conn.Write wrote more than the declared Content-Length 这个错误

=======更新下进展 =========

我测试了下,我在doSubmit里面sleep 10 ,然后看结果,结果请求很快返回了,说明在我复制response之后,这个访问就结束了,这样也能解释为什么会出现以上的错误

那么新的问题就是,如果实现一个高并发的服务器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值