如何在一定时间后取消goroutines

时间:2017-03-26 22:26:37

标签: go

我正在制作一个负载测试工具,它在goroutine中进行多次HTTP调用,但它可以工作,但现在我试图让它只运行指定的持续时间。

如何在睡眠结束后取消goroutine?

我目前正在尝试做的是在指定的持续时间内制作time.Sleep()的goroutine,一旦完成,它将向频道广播一条消息。

在我的无限循环中,我在switch语句中侦听消息,当它在那里时我会返回。这就像我想要的那样工作。

问题是,来自go httpPost()行的goroutines将继续进行HTTP调用。我甚至尝试将通道传递到该函数并在那里监听相同的CALL_TIME_RAN_OUT消息,但无论出于何种原因,当我这样做时,goroutines只运行一次然后立即返回,而不是等待消息在Sleep

结束时播放

有谁知道我可以采取更好的方法?这似乎不起作用。

这是代码(删除不相关的部分):

func attack(cfg AttackConfig) {
    // some code ...

    var ar attackResponse
    ch := make(chan uint8, 8)

    go func() {
        time.Sleep(cfg.Duration * time.Second)
        ch <- CALL_TIME_RAN_OUT
    }()

    for {
        if atomic.LoadInt32(&currConnections) < atomic.LoadInt32(&maxConnections) - 1 {
            go httpPost(cfg, &ar, ch)
        }

        switch <-ch {
        // some other cases ...
        case CALL_TIME_RAN_OUT:
            fmt.Printf("%d seconds have elapsed. Shutting down!", cfg.Duration)
            return
        }
    }
}

func httpPost(cfg AttackConfig, a *attackResponse, ch chan uint8) {
    // some code here to create HTTP client ...

    for {
        // some code to make HTTP call ...

        switch <-ch {
        case CALL_TIME_RAN_OUT:
            return
        }
    }
}

1 个答案:

答案 0 :(得分:2)

使用包golang.org/x/net/context。 Go 1.7将golang.org/x/net/context包移动到标准库context。 如果您使用版本1.7 +,那么只需import context

用法很简单:

package main

import (
    "context"
    "fmt"
    "time"
)

func test(ctx context.Context) {
    t := time.Now()

    select {
    case <-time.After(1 * time.Second):
        fmt.Println("overslept")
    case <-ctx.Done():
    }
    fmt.Println("used:", time.Since(t))
}

func main() {
    ctx, _ := context.WithTimeout(context.Background(), 50*time.Millisecond)
    test(ctx)
}
相关问题