Golang中HTTP服务的分析与设计详解(朗格男表价格及图片)不看后悔

随心笔谈1年前 (2023)发布 admin
108 0

func (srv *Server) Serve(l net.Listener) error {
if fn :=testHookServerServe; fn !=nil {
fn(srv, l) // call hook with unwrapped listener
}
origListener :=l
l=&onceCloseListener{Listener: l}
defer l.Close()
if err :=srv.setupHTTP2_Serve(); err !=nil {
return err
}
if !srv.trackListener(&l, true) {
return ErrServerClosed
}
defer srv.trackListener(&l, false)
baseCtx :=context.Background()
if srv.BaseContext !=nil {
baseCtx=srv.BaseContext(origListener)
if baseCtx==nil {
panic(“BaseContext returned a nil context”)
}
}
var tempDelay time.Duration // how long to sleep on accept failure
// 将整个Server对象设置进ctx中,在多个goroutinue中共享
ctx :=context.WithValue(baseCtx, ServerContextKey, srv)
for {
rw, err :=l.Accept() // 阻塞等待连接
if err !=nil {
select {
case <-srv.getDoneChan():
return ErrServerClosed
default:
}
if ne, ok :=err.(net.Error); ok && ne.Temporary() {
if tempDelay==0 {
tempDelay=5 * time.Millisecond
} else {
tempDelay *=2
}
if max :=1 * time.Second; tempDelay > max {
tempDelay=max
}
srv.logf(“http: Accept error: %v; retrying in %v”, err, tempDelay)
time.Sleep(tempDelay)
continue
}
return err
}
connCtx :=ctx
if cc :=srv.ConnContext; cc !=nil {
connCtx=cc(connCtx, rw)
if connCtx==nil {
panic(“ConnContext returned nil”)
}
}
tempDelay=0
c :=srv.newConn(rw)
c.setState(c.rwc, StateNew, runHooks) // before Serve can return
go c.serve(connCtx) //Serve a new connection 建立新的连接
}
}

© 版权声明

相关文章