本文整理汇总了Golang中euphoria/io/scope.Context.Done方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.Done方法的具体用法?Golang Context.Done怎么用?Golang Context.Done使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类euphoria/io/scope.Context
的用法示例。
在下文中一共展示了Context.Done方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Serve
func Serve(ctx scope.Context, addr string) {
http.Handle("/metrics", prometheus.Handler())
listener, err := net.Listen("tcp", addr)
if err != nil {
ctx.Terminate(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, nil); err != nil {
fmt.Printf("http[%s]: %s\n", addr, err)
ctx.Terminate(err)
}
closeListener()
ctx.WaitGroup().Done()
}
示例2: 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()
}
示例3: 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
}
}
示例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: 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:])
}
}
}
示例6: ScanLoop
func ScanLoop(ctx scope.Context, c cluster.Cluster, pb *psql.Backend, interval time.Duration) {
defer ctx.WaitGroup().Done()
errCount := 0
for {
t := time.After(interval)
select {
case <-ctx.Done():
return
case <-t:
if err := scan(ctx.Fork(), c, pb); err != nil {
errCount++
fmt.Printf("scan error [%d/%d]: %s", errCount, maxErrors, err)
if errCount > maxErrors {
fmt.Printf("maximum scan errors exceeded, terminating\n")
ctx.Terminate(fmt.Errorf("maximum scan errors exceeded"))
return
}
continue
}
errCount = 0
}
}
}