dgrr / http2

HTTP/2 implementation for fasthttp
Apache License 2.0
210 stars 37 forks source link

Client transport undefined #20

Closed trinhdaiphuc closed 3 years ago

trinhdaiphuc commented 3 years ago

When I try the http2 client example, I have trouble with the Transport method. My code:

...

func main() {
    c := &fasthttp.HostClient{
        Addr:  "api.binance.com:443",
        IsTLS: true,
    }
    if err := http2.ConfigureClient(c, http2.OptionEnableCompression); err != nil {
        panic(err)
    }

    count := int32(0)
    var wg sync.WaitGroup
    for i := 0; i < 20; i++ {
        for atomic.LoadInt32(&count) >= 4 {
            time.Sleep(time.Millisecond * 100)
        }

        wg.Add(1)
        atomic.AddInt32(&count, 1)
        go func() {
            defer wg.Done()
            defer atomic.AddInt32(&count, -1)

            req := fasthttp.AcquireRequest()
            res := fasthttp.AcquireResponse()

            req.Header.SetMethod("GET")
            // TODO: Use SetRequestURI
            req.URI().Update("https://api.binance.com/api/v3/exchangeInfo")

            err := c.Do(req, res)
            if err != nil {
                log.Fatalln(err)
            }

            body := res.Body()

            fmt.Printf("%d: %d\n", res.Header.StatusCode(), len(body))
            res.Header.VisitAll(func(k, v []byte) {
                fmt.Printf("%s: %s\n", k, v)
            })
            fmt.Println("------------------------")
        }()
    }

    wg.Wait()
}

The result:

$ go run main.go 

# github.com/dgrr/http2
../../../.gvm/pkgsets/go1.16/global/pkg/mod/github.com/dgrr/http2@v0.0.0-20210224142606-fc9371cdebbf/client.go:201:3: c.Transport undefined (type *fasthttp.HostClient has no field or method Transport)
dgrr commented 3 years ago

Yes, that's because you need to use my fork in fasthttp. https://github.com/dgrr/fasthttp As this library is in development yet, and my fasthttp branch is too some features may not work.

husy-dev commented 3 years ago

library is in development yet, and my f

I edited the go.mod by replacing github.com/valyala/fasthttp v1.21.0 => github.com/dgrr/fasthttp v1.20.0 or git clone it to local directory. they do work.