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


Golang Context.Done方法代码示例

本文整理汇总了Golang中github.com/jketcham/vicus/Godeps/_workspace/src/golang.org/x/net/context.Context.Done方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.Done方法的具体用法?Golang Context.Done怎么用?Golang Context.Done使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/jketcham/vicus/Godeps/_workspace/src/golang.org/x/net/context.Context的用法示例。


在下文中一共展示了Context.Done方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Do

// Do sends an HTTP request with the provided http.Client and returns an HTTP response.
// If the client is nil, http.DefaultClient is used.
// If the context is canceled or times out, ctx.Err() will be returned.
func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
	if client == nil {
		client = http.DefaultClient
	}

	// Request cancelation changed in Go 1.5, see cancelreq.go and cancelreq_go14.go.
	cancel := canceler(client, req)

	type responseAndError struct {
		resp *http.Response
		err  error
	}
	result := make(chan responseAndError, 1)

	go func() {
		resp, err := client.Do(req)
		result <- responseAndError{resp, err}
	}()

	select {
	case <-ctx.Done():
		cancel()
		return nil, ctx.Err()
	case r := <-result:
		return r.resp, r.err
	}
}
开发者ID:jketcham,项目名称:vicus,代码行数:30,代码来源:ctxhttp.go

示例2: Wait

// Wait blocks until i) the new transport is up or ii) ctx is done or iii) cc is closed.
func (cc *Conn) Wait(ctx context.Context) (transport.ClientTransport, error) {
	for {
		cc.mu.Lock()
		switch {
		case cc.state == Shutdown:
			cc.mu.Unlock()
			return nil, ErrClientConnClosing
		case cc.state == Ready:
			cc.mu.Unlock()
			return cc.transport, nil
		default:
			ready := cc.ready
			if ready == nil {
				ready = make(chan struct{})
				cc.ready = ready
			}
			cc.mu.Unlock()
			select {
			case <-ctx.Done():
				return nil, transport.ContextErr(ctx.Err())
			// Wait until the new transport is ready or failed.
			case <-ready:
			}
		}
	}
}
开发者ID:jketcham,项目名称:vicus,代码行数:27,代码来源:clientconn.go

示例3: Next

func (w *watcher) Next(ctx context.Context) (nu []*naming.Update, err error) {
	once.Do(func() {
		select {
		case <-ctx.Done():
			err = ctx.Err()
		default:
			for _, v := range w.kv {
				nu = append(nu, &naming.Update{
					Op:   naming.Add,
					Addr: v,
				})
			}
		}
	})
	if len(nu) > 0 || err != nil {
		// once.Do ran. Return directly.
		return
	}
	for {
		resp, err := w.wr.Next(ctx)
		if err != nil {
			return nil, err
		}
		if resp.Node.Dir {
			continue
		}
		w.mu.Lock()
		switch resp.Action {
		case "set":
			if resp.PrevNode == nil {
				nu = append(nu, &naming.Update{
					Op:   naming.Add,
					Addr: resp.Node.Value,
				})
				w.kv[resp.Node.Key] = resp.Node.Value
			} else {
				nu = append(nu, &naming.Update{
					Op:   naming.Delete,
					Addr: w.kv[resp.Node.Key],
				})
				nu = append(nu, &naming.Update{
					Op:   naming.Add,
					Addr: resp.Node.Value,
				})
				w.kv[resp.Node.Key] = resp.Node.Value
			}
		case "delete":
			nu = append(nu, &naming.Update{
				Op:   naming.Delete,
				Addr: resp.Node.Value,
			})
			delete(w.kv, resp.Node.Key)
		}
		w.mu.Unlock()
		return nu, nil
	}
}
开发者ID:jketcham,项目名称:vicus,代码行数:57,代码来源:etcd.go

示例4: wait

// wait blocks until it can receive from ctx.Done, closing, or proceed.
// If it receives from ctx.Done, it returns 0, the StreamError for ctx.Err.
// If it receives from closing, it returns 0, ErrConnClosing.
// If it receives from proceed, it returns the received integer, nil.
func wait(ctx context.Context, closing <-chan struct{}, proceed <-chan int) (int, error) {
	select {
	case <-ctx.Done():
		return 0, ContextErr(ctx.Err())
	case <-closing:
		return 0, ErrConnClosing
	case i := <-proceed:
		return i, nil
	}
}
开发者ID:jketcham,项目名称:vicus,代码行数:14,代码来源:transport.go


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