Juanvulcano
Juanvulcano

Reputation: 1396

Golang, changing structs of custom clients MaxIdleConnsPerHost

I want to set a custom http client to get several http requests concurrently. However, I'm relatively new to Golang and I'm facing issues with changing the struct of the clients and the feasibility of what I want to do.

I want to change the Transport of s with the one I created.

A simplified version of my script is:

package main

import (
    "fmt"
    "os"
    "github.com/njasm/gosoundcloud"
)

func main() {
    s, err := gosoundcloud.NewSoundcloudApi("client_id", "client_secret", nil)
    if err != nil {
        panic(err)
    }

   if err = s.PasswordCredentialsToken("[email protected]", "password"); err!= nil {
    fmt.Println(err)
    os.Exit(1)
    }

    var userID uint64 = 1
    Transport, ok := http.DefaultTransport.(*http.Transport)
    if !ok {
        fmt.Println("http.DefaultClient is not an http.Client")
        os.Exit(1)
    }
    Transport.MaxIdleConnsPerHost = 250
    //Here I would like to change s'Transport so that it handles 250 MaxIdleConns
    member, err := s.GetUser(userID)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println(member)
}

Here are the structs for s

type SoundcloudApi struct {
conf       *oauth2.Config
httpClient *http.Client
response   *http.Response
}

References:

https://golang.org/pkg/net/http/#Client https://godoc.org/github.com/njasm/gosoundcloud#SoundcloudApi

Upvotes: 0

Views: 342

Answers (2)

njasm
njasm

Reputation: 431

I'm the creator of the gosoundcloud..

First of all thanks for using the library.

Second, as @OneOfOne pointed out, I too believe that any answer to your needs here will be a workaround that we can probably fix with a feature request.

So, I invite you to file an issue to discuss the issue further.

I'll be more then glad to help out.

Upvotes: 1

How about:

tr := &http.Transport{DisableKeepAlives: false, MaxIdleConnsPerHost:250} 
client := &http.Client{Transport: tr}

And assing client to SoundcloudApi.httpClient

Upvotes: 1

Related Questions