• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang context.Context类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: Ack

// Ack acknowledges a task in the queue.
func (c *Client) Ack(ctx context.Context, queue string, task server.TaskID) (
	err error) {
	_, call := c.GoAck(queue, task, nil)
	select {
	case res := <-call:
		return res.Error
	case <-ctx.Done():
		return ctx.Err()
	}
}
开发者ID:jyzhe,项目名称:beehive,代码行数:11,代码来源:client.go


示例2: waitElection

func (n *MultiNode) waitElection(ctx context.Context, group uint64) {
	ch := make(chan struct{})
	n.pmu.Lock()
	n.pendingElects[group] = append(n.pendingElects[group], ch)
	n.pmu.Unlock()
	select {
	case <-ch:
	case <-ctx.Done():
	case <-n.done:
	}
}
开发者ID:jyzhe,项目名称:beehive,代码行数:11,代码来源:node.go


示例3: DeQ

// DeQ dequeues a task from the given queue. It returns the task or an error.
func (c *Client) DeQ(ctx context.Context, queue string) (task server.Task,
	err error) {

	_, call := c.GoDeQ(queue, nil)
	select {
	case res := <-call:
		if res.Error != nil {
			return server.Task{}, res.Error
		}
		return res.Data.(server.Task), nil
	case <-ctx.Done():
		return server.Task{}, ctx.Err()
	}
}
开发者ID:jyzhe,项目名称:beehive,代码行数:15,代码来源:client.go


示例4: EnQ

// EnQ enqueues a task with the given body in the queue. It returns the
// task's id or an error.
func (c *Client) EnQ(ctx context.Context, queue string, body []byte) (
	id server.TaskID, err error) {

	_, call := c.GoEnQ(queue, body, nil)
	select {
	case res := <-call:
		if res.Error != nil {
			return 0, res.Error
		}
		return res.Data.(server.TaskID), res.Error
	case <-ctx.Done():
		return 0, ctx.Err()
	}
}
开发者ID:jyzhe,项目名称:beehive,代码行数:16,代码来源:client.go


示例5: StepBatch

// StepBatch steps all messages in the batch. Context is used for enqueuing the
// batch and timeout is used as the maximum wait time when stepping messages
// in the batch.
func (n *MultiNode) StepBatch(ctx context.Context, batch Batch,
	timeout time.Duration) error {

	bt := batchTimeout{
		batch:   batch,
		timeout: timeout,
	}
	select {
	case n.recvc <- bt:
		return nil
	case <-ctx.Done():
		return ctx.Err()
	}
}
开发者ID:jyzhe,项目名称:beehive,代码行数:17,代码来源:node.go


示例6: step

func (mn *multiNode) step(ctx context.Context, m multiMessage) error {
	ch := mn.recvc
	if m.msg.Type == pb.MsgProp {
		ch = mn.propc
	}

	select {
	case ch <- m:
		return nil
	case <-ctx.Done():
		return ctx.Err()
	case <-mn.done:
		return ErrStopped
	}
}
开发者ID:jyzhe,项目名称:beehive,代码行数:15,代码来源:multinode.go


示例7: step

// Step advances the state machine using msgs. The ctx.Err() will be returned,
// if any.
func (n *node) step(ctx context.Context, m pb.Message) error {
	ch := n.recvc
	if m.Type == pb.MsgProp {
		ch = n.propc
	}

	select {
	case ch <- m:
		return nil
	case <-ctx.Done():
		return ctx.Err()
	case <-n.done:
		return ErrStopped
	}
}
开发者ID:jyzhe,项目名称:beehive,代码行数:17,代码来源:node.go


示例8: Exists

func (n *MultiNode) Exists(ctx context.Context, gid uint64) (ok bool) {
	ch := make(chan groupResponse, 1)
	n.groupc <- groupRequest{
		reqType: groupRequestStatus,
		group:   &group{id: gid},
		ch:      ch,
	}
	select {
	case res := <-ch:
		return res.err == nil
	case <-ctx.Done():
		return false
	case <-n.done:
		return false
	}
}
开发者ID:jyzhe,项目名称:beehive,代码行数:16,代码来源:node.go


示例9: Sync

// Sync processes a synchrounous request and returns the response and error.
func (h *hive) Sync(ctx context.Context, req interface{}) (res interface{},
	err error) {

	id := uint64(rand.Int63())
	ch := make(chan syncRes, 1)

	// We should run this in parallel in case we are blocked on h.syncCh.
	go func() {
		sc := syncReqAndChan{
			req: syncReq{ID: id, Data: req},
			ch:  ch,
		}
		select {
		case h.syncCh <- sc:
		case <-ctx.Done():
		}
	}()

	select {
	case r := <-ch:
		if r.Err != nil {
			return nil, errors.New(r.Err.Error())
		}
		return r.Data, nil

	case <-ctx.Done():
		return nil, ctx.Err()
	}
}
开发者ID:jyzhe,项目名称:beehive,代码行数:30,代码来源:hive.go


示例10: processConfChange

func (n *MultiNode) processConfChange(ctx context.Context, group uint64,
	cc raftpb.ConfChange, gn GroupNode) error {

	if group == 0 || gn.Node == 0 || gn.Group != group {
		glog.Fatalf("invalid group node: %v", gn)
	}

	id := n.genID()
	req := Request{Data: gn}

	var err error
	cc.Context, err = n.encReq(id, req)
	if err != nil {
		return err
	}

	ch := n.line.wait(id, req)

	d, err := cc.Marshal()
	if err != nil {
		return err
	}
	select {
	case n.propc <- multiMessage{
		group: group,
		msg: raftpb.Message{
			Type:    raftpb.MsgProp,
			Entries: []raftpb.Entry{{Type: raftpb.EntryConfChange, Data: d}},
		},
	}:
	case <-ctx.Done():
		n.line.cancel(id)
		return ctx.Err()
	case <-n.done:
		return ErrStopped
	}

	select {
	case res := <-ch:
		return res.Err
	case <-ctx.Done():
		n.line.cancel(id)
		return ctx.Err()
	case <-n.done:
		return ErrStopped
	}
}
开发者ID:jyzhe,项目名称:beehive,代码行数:47,代码来源:node.go


示例11: Propose

// Propose proposes the request and returns the response. This method blocks and
// returns either when the ctx is cancelled or the raft node returns a response.
func (n *MultiNode) Propose(ctx context.Context, group uint64,
	req interface{}) (res interface{}, err error) {

	id := n.genID()
	r := Request{
		Data: req,
	}

	d, err := n.encReq(id, r)
	if err != nil {
		return nil, err
	}

	glog.V(2).Infof("%v waits on raft request %v", n, id)
	ch := n.line.wait(id, r)
	mm := multiMessage{group,
		raftpb.Message{
			Type:    raftpb.MsgProp,
			Entries: []raftpb.Entry{{Data: d}},
		}}
	select {
	case n.propc <- mm:
	case <-ctx.Done():
		n.line.cancel(id)
		return nil, ctx.Err()
	case <-n.done:
		return nil, ErrStopped
	}

	select {
	case res := <-ch:
		glog.V(2).Infof("%v wakes up for raft request %v", n, id)
		return res.Data, res.Err
	case <-ctx.Done():
		n.line.cancel(id)
		return nil, ctx.Err()
	case <-n.done:
		return nil, ErrStopped
	}
}
开发者ID:jyzhe,项目名称:beehive,代码行数:42,代码来源:node.go


示例12: CreateGroup

func (n *MultiNode) CreateGroup(ctx context.Context, cfg GroupConfig) error {
	glog.V(2).Infof("creating a new group %v (%v) on node %v (%v) with peers %v",
		cfg.ID, cfg.Name, n.id, n.name, cfg.Peers)

	rs, ds, _, lei, _, err := OpenStorage(cfg.ID, cfg.DataDir, cfg.StateMachine)
	if err != nil {
		glog.Fatalf("cannot open storage: %v", err)
	}

	snap, err := rs.Snapshot()
	if err != nil {
		glog.Errorf("error in storage snapshot: %v", err)
		return err
	}

	n.gen.StartFrom((lei + cfg.SnapCount) << 8) // To avoid conflicts.

	c := &etcdraft.Config{
		ID:              cfg.ID,
		ElectionTick:    cfg.ElectionTicks,
		HeartbeatTick:   cfg.HeartbeatTicks,
		Storage:         rs,
		MaxSizePerMsg:   cfg.MaxMsgSize,
		MaxInflightMsgs: cfg.MaxInFlights,
		// TODO(soheil): Figure this one out:
		//               Applied: lsi,
	}
	g := &group{
		node:         n,
		id:           cfg.ID,
		name:         cfg.Name,
		stateMachine: cfg.StateMachine,
		raftStorage:  rs,
		diskStorage:  ds,
		applyc:       make(chan etcdraft.Ready, cfg.SnapCount),
		savec:        make(chan readySaved, 1),
		fsyncTime:    cfg.FsyncTick,
		snapCount:    cfg.SnapCount,
		snapped:      snap.Metadata.Index,
		applied:      snap.Metadata.Index,
		confState:    snap.Metadata.ConfState,
		stopc:        make(chan struct{}),
		applierDone:  make(chan struct{}),
		saverDone:    make(chan struct{}),
	}
	ch := make(chan groupResponse, 1)
	n.groupc <- groupRequest{
		reqType: groupRequestCreate,
		group:   g,
		config:  c,
		peers:   cfg.Peers,
		ch:      ch,
	}
	select {
	case res := <-ch:
		return res.err
	case <-ctx.Done():
		return ctx.Err()
	case <-n.done:
		return ErrStopped
	}
}
开发者ID:jyzhe,项目名称:beehive,代码行数:62,代码来源:node.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang params.ModuleParams类代码示例发布时间:2022-05-23
下一篇:
Golang context.TODO函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap