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 建立新的连接
}
}
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 建立新的连接
}
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。