本文整理匯總了Golang中github.com/maybebtc/interplanetary/Godeps/_workspace/src/code/google/com/p/go/net/context.Context.Err方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Err方法的具體用法?Golang Context.Err怎麽用?Golang Context.Err使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/maybebtc/interplanetary/Godeps/_workspace/src/code/google/com/p/go/net/context.Context
的用法示例。
在下文中一共展示了Context.Err方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Block
// GetBlock attempts to retrieve a particular block from peers within the
// deadline enforced by the context
//
// TODO ensure only one active request per key
func (bs *bitswap) Block(parent context.Context, k u.Key) (*blocks.Block, error) {
log.Debugf("Get Block %v", k)
now := time.Now()
defer func() {
log.Debugf("GetBlock took %f secs", time.Now().Sub(now).Seconds())
}()
ctx, cancelFunc := context.WithCancel(parent)
defer cancelFunc()
bs.wantlist.Add(k)
promise := bs.notifications.Subscribe(ctx, k)
const maxProviders = 20
peersToQuery := bs.routing.FindProvidersAsync(ctx, k, maxProviders)
go func() {
message := bsmsg.New()
for _, wanted := range bs.wantlist.Keys() {
message.AddWanted(wanted)
}
for peerToQuery := range peersToQuery {
log.Debugf("bitswap got peersToQuery: %s", peerToQuery)
go func(p peer.Peer) {
log.Debugf("bitswap dialing peer: %s", p)
err := bs.sender.DialPeer(ctx, p)
if err != nil {
log.Errorf("Error sender.DialPeer(%s)", p)
return
}
response, err := bs.sender.SendRequest(ctx, p, message)
if err != nil {
log.Errorf("Error sender.SendRequest(%s) = %s", p, err)
return
}
// FIXME ensure accounting is handled correctly when
// communication fails. May require slightly different API to
// get better guarantees. May need shared sequence numbers.
bs.strategy.MessageSent(p, message)
if response == nil {
return
}
bs.ReceiveMessage(ctx, p, response)
}(peerToQuery)
}
}()
select {
case block := <-promise:
bs.wantlist.Remove(k)
return &block, nil
case <-parent.Done():
return nil, parent.Err()
}
}
示例2: Handshake3
// Handshake3 exchanges local and remote service information
func Handshake3(ctx context.Context, c Conn) (*handshake.Handshake3Result, error) {
rpeer := c.RemotePeer()
lpeer := c.LocalPeer()
// setup + send the message to remote
var remoteH, localH *hspb.Handshake3
localH = handshake.Handshake3Msg(lpeer, c.RemoteMultiaddr())
localB, err := proto.Marshal(localH)
if err != nil {
return nil, err
}
c.Out() <- localB
log.Debugf("Handshake1: sent to %s", rpeer)
// wait + listen for response
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-c.Closing():
return nil, errors.New("Handshake3: error remote connection closed")
case remoteB, ok := <-c.In():
if !ok {
return nil, fmt.Errorf("Handshake3 error receiving from conn: %v", rpeer)
}
remoteH = new(hspb.Handshake3)
err = proto.Unmarshal(remoteB, remoteH)
if err != nil {
return nil, fmt.Errorf("Handshake3 could not decode remote msg: %q", err)
}
log.Debugf("Handshake3 received from %s", rpeer)
}
// actually update our state based on the new knowledge
res, err := handshake.Handshake3Update(lpeer, rpeer, remoteH)
if err != nil {
log.Errorf("Handshake3 failed to update %s", rpeer)
}
res.RemoteObservedAddress = c.RemoteMultiaddr()
return res, nil
}
示例3: ContextDo
func ContextDo(ctx context.Context, f func() error) error {
ch := make(chan error)
go func() {
select {
case <-ctx.Done():
case ch <- f():
}
}()
select {
case <-ctx.Done():
return ctx.Err()
case val := <-ch:
return val
}
return nil
}
示例4: Handshake1
// Handshake1 exchanges local and remote versions and compares them
// closes remote and returns an error in case of major difference
func Handshake1(ctx context.Context, c Conn) error {
rpeer := c.RemotePeer()
lpeer := c.LocalPeer()
var remoteH, localH *hspb.Handshake1
localH = handshake.Handshake1Msg()
myVerBytes, err := proto.Marshal(localH)
if err != nil {
return err
}
c.Out() <- myVerBytes
log.Debugf("Sent my version (%s) to %s", localH, rpeer)
select {
case <-ctx.Done():
return ctx.Err()
case <-c.Closing():
return errors.New("remote closed connection during version exchange")
case data, ok := <-c.In():
if !ok {
return fmt.Errorf("error retrieving from conn: %v", rpeer)
}
remoteH = new(hspb.Handshake1)
err = proto.Unmarshal(data, remoteH)
if err != nil {
return fmt.Errorf("could not decode remote version: %q", err)
}
log.Debugf("Received remote version (%s) from %s", remoteH, rpeer)
}
if err := handshake.Handshake1Compatible(localH, remoteH); err != nil {
log.Infof("%s (%s) incompatible version with %s (%s)", lpeer, localH, rpeer, remoteH)
return err
}
log.Debugf("%s version handshake compatible %s", lpeer, rpeer)
return nil
}
示例5: sendMessage
// sendMessage sends a message out (actual leg work. SendMessage is to export w/o rid)
func (s *service) sendMessage(ctx context.Context, m msg.NetMessage, rid RequestID) error {
// serialize ServiceMessage wrapper
data, err := wrapData(m.Data(), rid)
if err != nil {
return err
}
// log.Debug("Service send message [to = %s]", m.Peer())
// send message
m2 := msg.New(m.Peer(), data)
select {
case s.Outgoing <- m2:
case <-ctx.Done():
return ctx.Err()
}
return nil
}
示例6: SendRequest
func (f *fauxSender) SendRequest(ctx context.Context, m msg.NetMessage) (msg.NetMessage, error) {
f.Lock()
handlers := make([]mesHandleFunc, len(f.handlers))
copy(handlers, f.handlers)
f.Unlock()
for _, h := range handlers {
reply := h(m)
if reply != nil {
return reply, nil
}
}
// no reply? ok force a timeout
select {
case <-ctx.Done():
}
return nil, ctx.Err()
}
示例7: SendRequest
// SendRequest sends a request message out and awaits a response.
func (s *service) SendRequest(ctx context.Context, m msg.NetMessage) (msg.NetMessage, error) {
// check if we should bail given our contexts
select {
default:
case <-s.Closing():
return nil, fmt.Errorf("service closed: %s", s.Context().Err())
case <-ctx.Done():
return nil, ctx.Err()
}
// create a request
r, err := NewRequest(m.Peer().ID())
if err != nil {
return nil, err
}
// register Request
s.RequestsLock.Lock()
s.Requests[r.Key()] = r
s.RequestsLock.Unlock()
// defer deleting this request
defer func() {
s.RequestsLock.Lock()
delete(s.Requests, r.Key())
s.RequestsLock.Unlock()
}()
// check if we should bail after waiting for mutex
select {
default:
case <-s.Closing():
return nil, fmt.Errorf("service closed: %s", s.Context().Err())
case <-ctx.Done():
return nil, ctx.Err()
}
// Send message
s.sendMessage(ctx, m, r.ID)
// wait for response
m = nil
err = nil
select {
case m = <-r.Response:
case <-s.Closed():
err = fmt.Errorf("service closed: %s", s.Context().Err())
case <-ctx.Done():
err = ctx.Err()
}
if m == nil {
return nil, ErrNoResponse
}
return m, err
}