diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 676fd02ff..ee4ea5fc3 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -323,9 +323,6 @@ func (a *Allocation) InitAllocation() { a.mutex = &sync.Mutex{} a.commitMutex = &sync.Mutex{} a.fullconsensus, a.consensusThreshold = a.getConsensuses() - for _, blobber := range a.Blobbers { - zboxutil.SetHostClient(blobber.ID, blobber.Baseurl) - } a.readFree = true if a.ReadPriceRange.Max > 0 { for _, blobberDetail := range a.BlobberDetails { diff --git a/zboxcore/sdk/blockdownloadworker.go b/zboxcore/sdk/blockdownloadworker.go index 2024d343b..603003903 100644 --- a/zboxcore/sdk/blockdownloadworker.go +++ b/zboxcore/sdk/blockdownloadworker.go @@ -79,14 +79,14 @@ func InitBlockDownloader(blobbers []*blockchain.StorageNode, workerCount int) { for _, blobber := range blobbers { if _, ok := downloadBlockChan[blobber.ID]; !ok { downloadBlockChan[blobber.ID] = make(chan *BlockDownloadRequest, workerCount) - go startBlockDownloadWorker(downloadBlockChan[blobber.ID], workerCount, blobber.ID, blobber.Baseurl) + go startBlockDownloadWorker(downloadBlockChan[blobber.ID], workerCount) } } } -func startBlockDownloadWorker(blobberChan chan *BlockDownloadRequest, workers int, id, baseURL string) { +func startBlockDownloadWorker(blobberChan chan *BlockDownloadRequest, workers int) { sem := semaphore.NewWeighted(int64(workers)) - hostClient := zboxutil.GetHostClient(id, baseURL) + fastClient := zboxutil.GetFastHTTPClient() for { blockDownloadReq, open := <-blobberChan if !open { @@ -97,7 +97,7 @@ func startBlockDownloadWorker(blobberChan chan *BlockDownloadRequest, workers in continue } go func() { - blockDownloadReq.downloadBlobberBlock(hostClient) + blockDownloadReq.downloadBlobberBlock(fastClient) sem.Release(1) }() } @@ -116,7 +116,7 @@ func splitData(buf []byte, lim int) [][]byte { return chunks } -func (req *BlockDownloadRequest) downloadBlobberBlock(hostClient *fasthttp.HostClient) { +func (req *BlockDownloadRequest) downloadBlobberBlock(fastClient *fasthttp.Client) { if req.numBlocks <= 0 { req.result <- &downloadBlock{Success: false, idx: req.blobberIdx, err: errors.New("invalid_request", "Invalid number of blocks for download")} return @@ -157,7 +157,7 @@ func (req *BlockDownloadRequest) downloadBlobberBlock(hostClient *fasthttp.HostC err = func() error { now := time.Now() - statuscode, respBuf, err := hostClient.GetWithRequest(httpreq, req.respBuf) + statuscode, respBuf, err := fastClient.GetWithRequest(httpreq, req.respBuf) fasthttp.ReleaseRequest(httpreq) timeTaken := time.Since(now).Milliseconds() if err != nil { diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 46c33e0ef..65f038c88 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -45,7 +45,6 @@ type FastClient interface { var ( Client HttpClient - HostClientMap = make(map[string]*fasthttp.HostClient) FastHttpClient FastClient hostLock sync.RWMutex log logger.Logger @@ -132,41 +131,12 @@ func (pfe *proxyFromEnv) isLoopback(host string) (ok bool) { return net.ParseIP(host).IsLoopback() } -func SetHostClient(id, baseURL string) { - hostLock.Lock() - defer hostLock.Unlock() - if _, ok := HostClientMap[id]; !ok { - u, _ := url.Parse(baseURL) - host := fasthttp.AddMissingPort(u.Host, true) - HostClientMap[id] = &fasthttp.HostClient{ - NoDefaultUserAgentHeader: true, - Addr: host, - MaxConns: 1024, - MaxIdleConnDuration: 45 * time.Second, - DisableHeaderNamesNormalizing: true, - DisablePathNormalizing: true, - Dial: (&fasthttp.TCPDialer{ - Concurrency: 4096, - DNSCacheDuration: time.Hour, - }).Dial, - IsTLS: u.Scheme == "https", - ReadTimeout: 30 * time.Second, - WriteTimeout: 30 * time.Second, - } +func GetFastHTTPClient() *fasthttp.Client { + fc, ok := FastHttpClient.(*fasthttp.Client) + if ok { + return fc } -} - -func GetHostClient(id, baseURL string) *fasthttp.HostClient { - hostLock.RLock() - hc := HostClientMap[id] - if hc == nil { - hostLock.RUnlock() - SetHostClient(id, baseURL) - hostLock.RLock() - hc = HostClientMap[id] - } - hostLock.RUnlock() - return hc + return nil } func (pfe *proxyFromEnv) Proxy(req *http.Request) (proxy *url.URL, err error) {