FAQ
Hello.
I am implementing the app which deals with REST API. I want to make every
api call in its own goroutine. Right now I am doing this way:

func (c *Client) GetServersList() <-chan response.ServersListResponse {
responseChan := make(chan response.ServersListResponse)

go c.getServersList(responseChan)

return responseChan
}

func (c *Client) getServersList(responseChan chan<-
response.ServersListResponse) {
client := http.Client{}
req := c.assembleRequest("GET", "/ve")
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error : %s", err.Error())
}

b, _ := ioutil.ReadAll(resp.Body)
var serversList response.ServersListResponse
xml.Unmarshal(b, &serversList)
responseChan <- serversList
}

So, every request returns a chan of its response type. But I faced a
problem that I cannot have a generic function that will have a request and
channel as param. Its not possible use "chan interface{}". So, I will have
to duplicate a function that sends request and puts value to the channel.
And I'll have to do this for every API method.

Is there any way to implement the API in another way? But I need to have an
ability to send requests simultaneously.

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

  • Egon at Feb 22, 2014 at 2:35 pm
    First, what's your long story.... what does the program do. What are you using for handling the requests. If net/http it already automatically creates a goroutine for each request, but you still may need some throttling for how many concurrent requests can be done.

    Generally, try to draw your communication flow. E.g. this is where you get the input, they send the thing to a pool of workers, the worker will send.... etc. If you have clearly separated flow setup, channel communication, worker pool, what the worker does, then the solution should become easier.

    You should watch the videos "Code that grows with grace", "Go concurrency patterns", if you already haven't.

    + egon

    --
    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.
  • Sergey Gavruk at Feb 22, 2014 at 2:54 pm
    Sorry, I've deleted my post. Here is its content again:

    I am implementing the app which deals with REST API. I want to make every
    api call in its own goroutine. Right now I am doing this way:

    func (c *Client) GetServersList() <-chan response.ServersListResponse {
    responseChan := make(chan response.ServersListResponse)

    go c.getServersList(responseChan)

    return responseChan
    }

    func (c *Client) getServersList(responseChan chan<-
    response.ServersListResponse) {
    client := http.Client{}
    req := c.assembleRequest("GET", "/ve")
    resp, err := client.Do(req)
    if err != nil {
    fmt.Printf("Error : %s", err.Error())
    }

    b, _ := ioutil.ReadAll(resp.Body)
    var serversList response.ServersListResponse
    xml.Unmarshal(b, &serversList)
    responseChan <- serversList
    }

    So, every request returns a chan of its response type. But I faced a
    problem that I cannot have a generic function that will have a request and
    channel as param. Its not possible use "chan interface{}". So, I will have
    to duplicate a function that sends request and puts value to the channel.
    And I'll have to do this for every API method.

    Is there any way to implement the API in another way? But I need to have an
    ability to send requests simultaneously.

    All I want to do is:
    serversListChan1 := client.GetServersList()
    serversListChan2 := client.GetServersList()
    serversListChan3 := client.GetServersList()

    serversList := <-serversListChan
    serversList := <-serversListChan
    serversList := <-serversListChan
    fmt.Println(serversList.Servers[0].Name)
    fmt.Println(serversList.Servers[0].Name)
    fmt.Println(serversList.Servers[0].Name)

    --
    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.
  • Sergey Gavruk at Feb 22, 2014 at 3:00 pm
    In general, is there any way to implement "DoRequest" function to make this
    work:
    http://play.golang.org/p/JTbttTIh2t

    --
    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.
  • Shaked Klein Orbach at Feb 23, 2014 at 4:01 pm
    Maybe this will help you? http://play.golang.org/p/yQm6vRfQRq

    Basically what you are trying to do is using channels with behaviours
    instead of actual data. The other option is to create channels as *make(chan
    interface{})* but in my opinion thats the worst specifically for what you
    are trying to do.


    On Saturday, February 22, 2014 4:00:45 PM UTC+1, Sergey Gavruk wrote:

    In general, is there any way to implement "DoRequest" function to make
    this work:
    http://play.golang.org/p/JTbttTIh2t
    --
    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
postedFeb 22, '14 at 1:45p
activeFeb 23, '14 at 4:01p
posts5
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase