本文整理匯總了Golang中euphoria/io/scope.Context.Terminate方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Terminate方法的具體用法?Golang Context.Terminate怎麽用?Golang Context.Terminate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類euphoria/io/scope.Context
的用法示例。
在下文中一共展示了Context.Terminate方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: 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:])
}
}
}
示例3: 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
}
}
}