I’m not sure exactly what you are trying to accomplish, but I suspect that this 
article would help you.

 

https://blog.golang.org/go-concurrency-patterns-timing-out-and

 

John

    John Souvestre - New Orleans LA

 

From: golang-nuts@googlegroups.com [mailto:golang-nuts@googlegroups.com] On 
Behalf Of desaiabhi...@gmail.com
Sent: 2017 August 06, Sun 09:58
To: golang-nuts
Subject: [go-nuts] Re: Goroutines chan to get data at specific time

 

yes this is what I want.. 

 

Need to access web service multiple times to get the entire data unless return 
status is not "completed" But same time I need timeout

 

Thanks for the help

 

Thanks

 

Abhi

On Sunday, August 6, 2017 at 7:48:05 PM UTC+5:30, snmed wrote:

I still no get the idea behind your requirement, in your example you calling 
the web service 6 times in sequential manner and then write it to the channel.

But again you can only write once to a channel with a capacity of one, as long 
as you not read from the same channel and you still read it in the select 
statement.

That means all other calls to 'GetPartWebServiceData()' in the goroutine are 
blocked until that read in the select statement happens. In your case it is 
always the first

value written to the channel.

 

I try to give you a solution with although i'm not completely understand your 
requirements:

 

Here a working sequential solution: https://play.golang.org/p/2qohwIjP96


I hope that helps

 

Cheers snmed

Am Sonntag, 6. August 2017 13:05:11 UTC+2 schrieb Abhijit Desai:

Requirement is to collect the data with in cuttoff time say 3 sec by invoking 
web service multiple times and return only latest invoked data

 

c1 <- "result 1" // In real scenario this taking almost 1 seconds as in real 
scenario it's taking Part chunk of data from Web Service

 

Something like... Expecting data from c1 <- GetPartWebServiceData("request 3") 
//at least I should get this data!?

 

 

package main

 

import "time"

import "fmt"

 

func main() {

 

    c1 := make(chan string, 1)

    

    go func() {

  

        c1 <- GetPartWebServiceData("request 1") //say try 6 times

        c1 <- GetPartWebServiceData("request 2")

        c1 <- GetPartWebServiceData("request 3") //at least I should get this 
data!?

        c1 <- GetPartWebServiceData("request 4")

        c1 <- GetPartWebServiceData("request 5")

        c1 <- GetPartWebServiceData("request 6")

    }()

 

        select {

            case res := <-c1: //here program get out without waiting 3 seconds 
because it got the data

                fmt.Println(res)

            case <-time.After(time.Second * 4):

                fmt.Println("timeout")

        }

}

 

func GetPartWebServiceData(request string) string{

    time.Sleep(time.Second * 1) // Time Consuming work returning part value

    partResponse := "Response for " + request  //some response

    return partResponse

}

 

 

 

On Sunday, August 6, 2017 at 4:03:08 PM UTC+5:30, snmed wrote:

Hi 

What are you trying to solve? Your channel has a capacity of 1, the first write 
to c1 is successful but the second write to c1 is blocked until it has been 
read in the select statement. And therefore you print the first value written 
to c1.

I recommend you to read this https://golang.org/doc/effective_go.html#channels 
documentation about channels.

Cheers snmed

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to