本文整理匯總了Golang中euphoria/io/scope.Context.Err方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Err方法的具體用法?Golang Context.Err怎麽用?Golang Context.Err使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類euphoria/io/scope.Context
的用法示例。
在下文中一共展示了Context.Err方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: run
func (cmd *serveEmbedCmd) run(ctx scope.Context, args []string) error {
listener, err := net.Listen("tcp", cmd.addr)
if err != nil {
return err
}
closed := false
m := sync.Mutex{}
closeListener := func() {
m.Lock()
if !closed {
listener.Close()
closed = true
}
m.Unlock()
}
// Spin off goroutine to watch ctx and close listener if shutdown requested.
go func() {
<-ctx.Done()
closeListener()
}()
if err := http.Serve(listener, cmd); err != nil {
fmt.Printf("http[%s]: %s\n", cmd.addr, err)
return err
}
closeListener()
ctx.WaitGroup().Done()
return ctx.Err()
}
示例2: WaitForJob
func (jq *JobQueueBinding) WaitForJob(ctx scope.Context) error {
ch := make(chan error)
// background goroutine to wait on condition
go func() {
// synchronize with caller
<-ch
jq.m.Unlock()
jq.Backend.jobQueueListener().wait(jq.Name())
ch <- nil
}()
// synchronize with background goroutine
jq.m.Lock()
ch <- nil
jq.m.Lock()
jq.m.Unlock()
select {
case <-ctx.Done():
jq.Backend.jobQueueListener().wakeAll(jq.Name())
<-ch
return ctx.Err()
case err := <-ch:
return err
}
}
示例3: Claim
func Claim(ctx scope.Context, jq JobQueue, handlerID string, pollTime time.Duration, stealChance float64) (*Job, error) {
for ctx.Err() == nil {
if rand.Float64() < stealChance {
job, err := jq.TrySteal(ctx, handlerID)
if err != nil && err != ErrJobNotFound {
return nil, err
}
if err == nil {
return job, nil
}
}
job, err := jq.TryClaim(ctx, handlerID)
if err != nil {
if err == ErrJobNotFound {
child := ctx.ForkWithTimeout(pollTime)
if err = jq.WaitForJob(child); err != nil && err != scope.TimedOut {
return nil, err
}
continue
}
return nil, err
}
return job, nil
}
return nil, ctx.Err()
}
示例4: Send
func (s *session) Send(ctx scope.Context, cmdType proto.PacketType, payload interface{}) error {
// Special case: certain events have privileged info that may need to be stripped from them
switch event := payload.(type) {
case *proto.PresenceEvent:
switch s.privilegeLevel() {
case proto.Staff:
case proto.Host:
event.RealClientAddress = ""
default:
event.RealClientAddress = ""
event.ClientAddress = ""
}
case *proto.Message:
if s.privilegeLevel() == proto.General {
event.Sender.ClientAddress = ""
}
case *proto.EditMessageEvent:
if s.privilegeLevel() == proto.General {
event.Sender.ClientAddress = ""
}
}
var err error
payload, err = proto.DecryptPayload(payload, &s.client.Authorization, s.privilegeLevel())
if err != nil {
return err
}
encoded, err := json.Marshal(payload)
if err != nil {
return err
}
cmd := &proto.Packet{
Type: cmdType,
Data: encoded,
}
// Add to outgoing channel. If channel is full, defer to goroutine so as not to block
// the caller (this may result in deliveries coming out of order).
select {
case <-ctx.Done():
// Session is closed, return error.
return ctx.Err()
case s.outgoing <- cmd:
// Packet delivered to queue.
default:
// Queue is full.
logging.Logger(s.ctx).Printf("outgoing channel full, ordering cannot be guaranteed")
go func() { s.outgoing <- cmd }()
}
return nil
}
示例5: Loop
func Loop(ctx scope.Context, heim *proto.Heim, workerName, queueName string) error {
fmt.Printf("Loop\n")
ctrl, err := NewController(ctx, heim, workerName, queueName)
if err != nil {
fmt.Printf("error: %s\n", err)
return err
}
ctx.WaitGroup().Add(1)
go ctrl.background(ctx)
ctx.WaitGroup().Wait()
return ctx.Err()
}
示例6: terminal
func (ctrl *Controller) terminal(ctx scope.Context, ch ssh.Channel) {
defer ch.Close()
lines := make(chan string)
term := terminal.NewTerminal(ch, "> ")
go func() {
for ctx.Err() == nil {
line, err := term.ReadLine()
if err != nil {
ctx.Terminate(err)
return
}
lines <- line
}
}()
for {
var line string
select {
case <-ctx.Done():
return
case line = <-lines:
}
cmd := parse(line)
fmt.Printf("[control] > %v\n", cmd)
switch cmd[0] {
case "":
continue
case "quit":
return
case "shutdown":
ctrl.ctx.Terminate(fmt.Errorf("shutdown initiated from console"))
default:
runCommand(ctx.Fork(), ctrl, cmd[0], term, cmd[1:])
}
}
}