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 [email protected].
For more options, visit https://groups.google.com/d/optout.