当前位置: 首页>>代码示例>>Golang>>正文


Golang ResponseWriter.CloseNotify方法代码示例

本文整理汇总了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)
}
开发者ID:tomclegg,项目名称:hwy3,代码行数:30,代码来源:server.go

示例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
	}
}
开发者ID:pombredanne,项目名称:arvados,代码行数:27,代码来源:handlers.go


注:本文中的net/http.ResponseWriter.CloseNotify方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。