FAQ
for example, in PHP, if a request process more than 30's, the PHP will kill
the request, and then release the resource.

but in golang? such this code:
-------------------------------------------------------------------------------------
func main() {
http.HandleFunc("/", handler)
err := http.ListenAndServe(":80", nil)
fmt.Println(err)
}

func handler (resp http.ResponseWriter, req *http.Request) {

// a request is a new goroutine created by http/server.go

// do something ...

/* but if this goroutine process occur some problem,
such as Infinite loop, database error ....

and then it's forever exit, the goroutine LEAK.
*/
}
--------------------------------------------------------------------------------------
How to solve this problem? Thanks.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Kyle Lemons at Mar 3, 2013 at 3:05 am
    Make a deadline channel:

    func Handler(w http.ResponseWriter, r *http.Request) {
    deadline := time.After(MaxRequestTime)
    // do stuff
    moreStuff(deadline)
    }

    then, before doing anything with the request in the other goroutine, check
    if the deadline has passed:

    func moreStuff(deadline chan time.Time) {
    for {
    select {
    case <-deadline:
    // deadline passed
    return
    default:
    }
    // stuff
    }
    }

    Note that you still cannot exit the handler until all of your sub
    goroutines acknowledge that they're done, otherwise you have a race
    condition: the deadline could have not passed, then pass after they check
    but before the write to the request.

    On Sat, Mar 2, 2013 at 6:59 PM, Eucal J wrote:

    for example, in PHP, if a request process more than 30's, the PHP will
    kill the request, and then release the resource.

    but in golang? such this code:

    -------------------------------------------------------------------------------------
    func main() {
    http.HandleFunc("/", handler)
    err := http.ListenAndServe(":80", nil)
    fmt.Println(err)
    }

    func handler (resp http.ResponseWriter, req *http.Request) {

    // a request is a new goroutine created by http/server.go

    // do something ...

    /* but if this goroutine process occur some problem,
    such as Infinite loop, database error ....

    and then it's forever exit, the goroutine LEAK.
    */
    }

    --------------------------------------------------------------------------------------
    How to solve this problem? Thanks.

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Eucal J at Mar 3, 2013 at 3:35 am
    but the moreStuff must have a for{} loop to check the exit signal, for a
    example:
    func moreStuff(deadline <-chan time.Time) {
    for {
    select {
    case <-deadline:
    // deadline passed
    return
    default:
    }

    // for some reason, there have a Infinite loop or other
    unknow reason the process can't go on
    for {
    time.Sleep(1 * time.Second)
    fmt.Println(".....")
    }
    }
    }

    in this case, the moreStuff still can't exit.
    Is there a way, in main goroutine(Handler), to check if timeout then kill
    the sub goroutine?

    在 2013年3月3日星期日UTC+8上午11时04分46秒,Kyle Lemons写道:
    Make a deadline channel:

    func Handler(w http.ResponseWriter, r *http.Request) {
    deadline := time.After(MaxRequestTime)
    // do stuff
    moreStuff(deadline)
    }

    then, before doing anything with the request in the other goroutine, check
    if the deadline has passed:

    func moreStuff(deadline chan time.Time) {
    for {
    select {
    case <-deadline:
    // deadline passed
    return
    default:
    }
    // stuff
    }
    }

    Note that you still cannot exit the handler until all of your sub
    goroutines acknowledge that they're done, otherwise you have a race
    condition: the deadline could have not passed, then pass after they check
    but before the write to the request.


    On Sat, Mar 2, 2013 at 6:59 PM, Eucal J <jeu...@gmail.com <javascript:>>wrote:
    for example, in PHP, if a request process more than 30's, the PHP will
    kill the request, and then release the resource.

    but in golang? such this code:

    -------------------------------------------------------------------------------------
    func main() {
    http.HandleFunc("/", handler)
    err := http.ListenAndServe(":80", nil)
    fmt.Println(err)
    }

    func handler (resp http.ResponseWriter, req *http.Request) {

    // a request is a new goroutine created by http/server.go

    // do something ...

    /* but if this goroutine process occur some problem,
    such as Infinite loop, database error ....

    and then it's forever exit, the goroutine LEAK.
    */
    }

    --------------------------------------------------------------------------------------
    How to solve this problem? Thanks.

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Kyle Lemons at Mar 3, 2013 at 4:08 am

    On Sat, Mar 2, 2013 at 7:35 PM, Eucal J wrote:

    but the moreStuff must have a for{} loop to check the exit signal, for a
    example:
    func moreStuff(deadline <-chan time.Time) {
    for {
    select {
    case <-deadline:
    // deadline passed
    return
    default:
    }

    // for some reason, there have a Infinite loop or other
    unknow reason the process can't go on
    for {
    time.Sleep(1 * time.Second)
    fmt.Println(".....")
    }
    }
    }

    in this case, the moreStuff still can't exit.
    Is there a way, in main goroutine(Handler), to check if timeout then kill
    the sub goroutine?
    No. You cannot kill other goroutines. Note that even Java has deprecated
    its ways of killing other threads :). The only thing you can do is
    terminate the binary.

    在 2013年3月3日星期日UTC+8上午11时04分46秒,Kyle Lemons写道:
    Make a deadline channel:

    func Handler(w http.ResponseWriter, r *http.Request) {
    deadline := time.After(MaxRequestTime)
    // do stuff
    moreStuff(deadline)
    }

    then, before doing anything with the request in the other goroutine,
    check if the deadline has passed:

    func moreStuff(deadline chan time.Time) {
    for {
    select {
    case <-deadline:
    // deadline passed
    return
    default:
    }
    // stuff
    }
    }

    Note that you still cannot exit the handler until all of your sub
    goroutines acknowledge that they're done, otherwise you have a race
    condition: the deadline could have not passed, then pass after they check
    but before the write to the request.

    On Sat, Mar 2, 2013 at 6:59 PM, Eucal J wrote:

    for example, in PHP, if a request process more than 30's, the PHP will
    kill the request, and then release the resource.

    but in golang? such this code:
    ------------------------------**------------------------------**
    -------------------------
    func main() {
    http.HandleFunc("/", handler)
    err := http.ListenAndServe(":80", nil)
    fmt.Println(err)
    }

    func handler (resp http.ResponseWriter, req *http.Request) {

    // a request is a new goroutine created by http/server.go

    // do something ...

    /* but if this goroutine process occur some problem,
    such as Infinite loop, database error ....

    and then it's forever exit, the goroutine LEAK.
    */
    }
    ------------------------------**------------------------------**
    --------------------------
    How to solve this problem? Thanks.

    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts...@**googlegroups.com.

    For more options, visit https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
    .

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Eucal J at Mar 3, 2013 at 4:35 pm
    在 2013年3月3日星期日UTC+8下午12时08分31秒,Kyle Lemons写道:



    On Sat, Mar 2, 2013 at 7:35 PM, Eucal J <jeu...@gmail.com <javascript:>>wrote:
    but the moreStuff must have a for{} loop to check the exit signal, for a
    example:
    func moreStuff(deadline <-chan time.Time) {
    for {
    select {
    case <-deadline:
    // deadline passed
    return
    default:
    }

    // for some reason, there have a Infinite loop or other
    unknow reason the process can't go on
    for {
    time.Sleep(1 * time.Second)
    fmt.Println(".....")
    }
    }
    }

    in this case, the moreStuff still can't exit.
    Is there a way, in main goroutine(Handler), to check if timeout then kill
    the sub goroutine?
    No. You cannot kill other goroutines. Note that even Java has deprecated
    its ways of killing other threads :). The only thing you can do is
    terminate the binary.

    在 2013年3月3日星期日UTC+8上午11时04分46秒,Kyle Lemons写道:
    Make a deadline channel:

    func Handler(w http.ResponseWriter, r *http.Request) {
    deadline := time.After(MaxRequestTime)
    // do stuff
    moreStuff(deadline)
    }

    then, before doing anything with the request in the other goroutine,
    check if the deadline has passed:

    func moreStuff(deadline chan time.Time) {
    for {
    select {
    case <-deadline:
    // deadline passed
    return
    default:
    }
    // stuff
    }
    }

    Note that you still cannot exit the handler until all of your sub
    goroutines acknowledge that they're done, otherwise you have a race
    condition: the deadline could have not passed, then pass after they check
    but before the write to the request.

    On Sat, Mar 2, 2013 at 6:59 PM, Eucal J wrote:

    for example, in PHP, if a request process more than 30's, the PHP
    will kill the request, and then release the resource.

    but in golang? such this code:
    ------------------------------**------------------------------**
    -------------------------
    func main() {
    http.HandleFunc("/", handler)
    err := http.ListenAndServe(":80", nil)
    fmt.Println(err)
    }

    func handler (resp http.ResponseWriter, req *http.Request) {

    // a request is a new goroutine created by http/server.go

    // do something ...

    /* but if this goroutine process occur some problem,
    such as Infinite loop, database error ....

    and then it's forever exit, the goroutine LEAK.
    */
    }
    ------------------------------**------------------------------**
    --------------------------
    How to solve this problem? Thanks.

    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts...@**googlegroups.com.

    For more options, visit https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
    .

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    Ok, Thanks.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 3, '13 at 2:59a
activeMar 3, '13 at 4:35p
posts5
users2
websitegolang.org

2 users in discussion

Eucal J: 3 posts Kyle Lemons: 2 posts

People

Translate

site design / logo © 2022 Grokbase