本文整理汇总了Golang中net/http.ResponseWriter.CloseNotify方法的典型用法代码示例。如果您正苦于以下问题:Golang ResponseWriter.CloseNotify方法的具体用法?Golang ResponseWriter.CloseNotify怎么用?Golang ResponseWriter.CloseNotify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http.ResponseWriter
的用法示例。
在下文中一共展示了ResponseWriter.CloseNotify方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ServeHTTP
func (th *teeHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
t0 := time.Now()
if *mimeType != "" {
w.Header().Set("Content-Type", *mimeType)
} else if *mp3only {
w.Header().Set("Content-Type", "audio/mpeg")
}
cw := &countingWriter{Writer: w}
sc := &signalCloser{Writer: cw, Closed: make(chan struct{})}
log.Printf("%d +%+q", atomic.AddInt64(&th.clients, 1), req.RemoteAddr)
th.Add(sc)
errs := make(chan error)
go func() {
if w, ok := w.(http.CloseNotifier); ok {
<-w.CloseNotify()
} else {
<-sc.Closed
}
errs <- th.RemoveAndClose(sc)
}()
err := <-errs
errStr := ""
if err != nil {
errStr = err.Error()
}
t := time.Since(t0)
log.Printf("%d -%+q %s %d =%dB/s %+q", atomic.AddInt64(&th.clients, -1), req.RemoteAddr, t, cw.Count(), int64(float64(cw.Count())/t.Seconds()), errStr)
}
示例2: getBufferForResponseWriter
// Get a buffer from the pool -- but give up and return a non-nil
// error if resp implements http.CloseNotifier and tells us that the
// client has disconnected before we get a buffer.
func getBufferForResponseWriter(resp http.ResponseWriter, bufs *bufferPool, bufSize int) ([]byte, error) {
var closeNotifier <-chan bool
if resp, ok := resp.(http.CloseNotifier); ok {
closeNotifier = resp.CloseNotify()
}
var buf []byte
bufReady := make(chan []byte)
go func() {
bufReady <- bufs.Get(bufSize)
close(bufReady)
}()
select {
case buf = <-bufReady:
return buf, nil
case <-closeNotifier:
go func() {
// Even if closeNotifier happened first, we
// need to keep waiting for our buf so we can
// return it to the pool.
bufs.Put(<-bufReady)
}()
return nil, ErrClientDisconnect
}
}