2

I am trying to read a constant stream of data, if the call to receive stream takes longer than 30 seconds I need to timeout and exit the program. I am not sure how to exit the go routine once a timeout has been received.

func ReceiveStreamMessages(strm Stream, msg chan<- []byte) error {
  d := make(chan []byte, 1)

  e := make(chan error)

  tm := time.After(30 * time.Second)

  go func() {
    for {
        //blocking call
        data, err := strm.Recv()
        if err != nil {
            e <- err
            return
        }
        select {
        case d <- data.Result:
        case <-tm:
            //exit out go routine
            return
         }
      }
  }()

  for {
    select {
    case message := <-d:
        msg <- message
    case err := <-e:
        return err
    case <-tm:
        return nil
    }
  }
}

My code above is wrong as: in order for the select to run in the go routines for loop, the blocking function will have to return and data will be populated and therefore won't hit the timeout select case (or will do randomly as both will be ready). Is exiting the parent function enough to exit the go routine?

3
  • 2
    "Is exiting the parent function enough to exit the go routine" Nope. Go routines keep running till main thread is alive; Seems like you just need to use a context with timeout Commented Sep 22, 2020 at 11:12
  • Hmmm, im not sure how I see context will fix my problem? If i replace the timeout with context signal within the go routines select, won't I just be in the same place as I am now? Both channels ready to receive? Maybe I am missing something. Are you able to provide an example please? Commented Sep 22, 2020 at 11:27
  • 1
    Read this stackoverflow.com/questions/46789554/… Commented Sep 22, 2020 at 11:48

1 Answer 1

3

Use context package WithTimeout. Something like this:

package main

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

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
    // prepare
    ...
    // wait group just for test
    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
        for {
            select {
            case d <- data.Result:
               // do something
            case <-ctx.Done():
                fmt.Println("Done")
                wg.Done()
                return
            }
        }
    }()
    wg.Wait()
    cancel()
    fmt.Println("Hello, playground")
}

You can see a working example here https://play.golang.org/p/agi1fimtEkJ

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.