yanfangsheng123 发表于 2018-9-20 06:23:09

golang的http分析

// Multiple goroutines may invoke methods on a Listener simultaneously.  
type Listener interface {
  // Accept waits for and returns the next connection to the listener.
  
    Accept() (Conn, error)
  // Close closes the listener.
  // Any blocked Accept operations will be unblocked and return errors.
  
    Close() error
  // Addr returns the listener's network address.
  
    Addr() Addr
  
}
  
// 这里实现得比较好,覆盖了一个Accept方法,在其中加入了keepAlived的选项。其他两个方法仍旧使用原listener的
  
type tcpKeepAliveListener struct {
  *net.TCPListener             // 外层可直接调它的方法不需要指定成员
  
}
  
func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
  tc, err := ln.AcceptTCP()
  if err != nil {
  return
  }
  tc.SetKeepAlive(true)
  tc.SetKeepAlivePeriod(3 * time.Minute)
  return tc, nil
  
}


页: [1]
查看完整版本: golang的http分析