Skip to content

Commit

Permalink
[+] fix: concurrent HTTP requests (#107)
Browse files Browse the repository at this point in the history
* [+] fix: concurrent HTTP requests

* chore: Updated coverage badge.

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
Noooste and actions-user authored Jul 2, 2024
1 parent 8d108b4 commit bd43e91
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AzureTLS Client
[![GoDoc](https://godoc.org/github.com/Noooste/azuretls-client?status.svg)](https://godoc.org/github.com/Noooste/azuretls-client)
![Coverage](https://img.shields.io/badge/Coverage-77.8%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-77.6%25-brightgreen)
[![build](https://github.com/Noooste/azuretls-client/actions/workflows/push.yml/badge.svg)](https://github.com/Noooste/azuretls-client/actions/workflows/push.yml)
[![Go Report Card](https://goreportcard.com/badge/Noooste/azuretls-client)](https://goreportcard.com/report/Noooste/azuretls-client)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/Noooste/azuretls-client/blob/master/LICENSE)
Expand Down
4 changes: 3 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ func (s *Session) initConn(req *Request) (conn *Conn, err error) {
return nil, err
}
} else {
if conn.Conn, err = (&net.Dialer{Timeout: conn.TimeOut}).DialContext(s.ctx, "tcp", host); err != nil {
if conn.Conn, err = (&net.Dialer{
Timeout: conn.TimeOut,
}).DialContext(s.ctx, "tcp", host); err != nil {
return nil, err
}
}
Expand Down
15 changes: 8 additions & 7 deletions test/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ func concurrency(session *azuretls.Session, wg *sync.WaitGroup, ok *int64) bool
fmt.Println(err2)
return false
}

_, err2 = session.Get("http://example.com/")

if err2 != nil {
fmt.Println(err2)
return false
}
}

atomic.AddInt64(ok, 1)
Expand All @@ -107,7 +114,6 @@ func TestHighConcurrency(t *testing.T) {

wait.Add(int(count))

var err error
var ok = new(int64)

var i int64
Expand All @@ -118,12 +124,7 @@ func TestHighConcurrency(t *testing.T) {

wait.Wait()

if err != nil {
t.Error("TestHighConcurrency failed, expected: ", count, ", got: ", ok)
t.Fatal(err)
}

if atomic.LoadInt64(ok) < count-1 { //~1 request can fail
t.Fatal("TestHighConcurrency failed, expected: ", count, ", got: ", ok)
t.Fatal("TestHighConcurrency failed, expected: ", count, ", got: ", *ok)
}
}
19 changes: 6 additions & 13 deletions transport.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package azuretls

import (
"context"
http "github.com/Noooste/fhttp"
"github.com/Noooste/fhttp/http2"
"net"
"net/url"
)

func (s *Session) initTransport(browser string) (err error) {
Expand All @@ -28,17 +27,11 @@ func (s *Session) initHTTP1() {
s.Transport = &http.Transport{
TLSHandshakeTimeout: s.TimeOut,
ResponseHeaderTimeout: s.TimeOut,
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
s.Connections.mu.RLock()
defer s.Connections.mu.RUnlock()
rc := s.Connections.hosts[addr]
return rc.TLS, nil
},
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
s.Connections.mu.RLock()
defer s.Connections.mu.RUnlock()
rc := s.Connections.hosts[addr]
return rc.Conn, nil
Proxy: func(req *http.Request) (*url.URL, error) {
if s.ProxyDialer == nil {
return nil, nil
}
return s.ProxyDialer.ProxyURL, nil
},
}
}
Expand Down

0 comments on commit bd43e91

Please sign in to comment.