Skip to content

Commit

Permalink
Reuse client at protocol level so that connections are reused.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreachild committed Jan 10, 2025
1 parent 8f1e2a8 commit d6303ef
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
32 changes: 20 additions & 12 deletions gremlin-go/driver/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,25 +608,20 @@ func TestConnection(t *testing.T) {
assert.NotNil(t, client)
defer client.Close()

var wg sync.WaitGroup
// synchronous
for i := 0; i < 5; i++ {
submitCount(i, client, t)
}

// async
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
resultSet, err := client.Submit("g.V().count().as('c').math('c + " + strconv.Itoa(i) + "')")
assert.Nil(t, err)
assert.NotNil(t, resultSet)
result, ok, err := resultSet.One()
assert.Nil(t, err)
assert.True(t, ok)
assert.NotNil(t, result)
c, err := result.GetInt()
assert.Equal(t, 6+i, c)
_, _ = fmt.Fprintf(os.Stdout, "Received result : %s\n", result)
submitCount(i, client, t)
}(i)
}

wg.Wait()

//
Expand Down Expand Up @@ -1292,3 +1287,16 @@ func TestConnection(t *testing.T) {
}
})
}

func submitCount(i int, client *Client, t *testing.T) {
resultSet, err := client.Submit("g.V().count().as('c').math('c + " + strconv.Itoa(i) + "')")
assert.Nil(t, err)
assert.NotNil(t, resultSet)
result, ok, err := resultSet.One()
assert.Nil(t, err)
assert.True(t, ok)
assert.NotNil(t, result)
c, err := result.GetInt()
assert.Equal(t, 6+i, c)
_, _ = fmt.Fprintf(os.Stdout, "Received result : %s\n", result)
}
16 changes: 15 additions & 1 deletion gremlin-go/driver/httpProtocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,28 @@ type httpProtocol struct {
logHandler *logHandler
url string
connSettings *connectionSettings
httpClient *http.Client
}

func newHttpProtocol(handler *logHandler, url string, connSettings *connectionSettings) (*httpProtocol, error) {
transport := &http.Transport{
TLSClientConfig: connSettings.tlsConfig,
MaxConnsPerHost: 0, // TODO
IdleConnTimeout: 0, // TODO
DisableCompression: !connSettings.enableCompression,
}

httpClient := http.Client{
Transport: transport,
Timeout: connSettings.connectionTimeout,
}

httpProt := &httpProtocol{
serializer: newGraphBinarySerializer(handler),
logHandler: handler,
url: url,
connSettings: connSettings,
httpClient: &httpClient,
}
return httpProt, nil
}
Expand All @@ -56,7 +70,7 @@ func (protocol *httpProtocol) send(request *request) (ResultSet, error) {
return nil, err
}

transport := NewHttpTransporter(protocol.url, protocol.connSettings)
transport := NewHttpTransporter(protocol.url, protocol.connSettings, protocol.httpClient)

// async send request and wait for response
transport.wg.Add(1)
Expand Down
20 changes: 4 additions & 16 deletions gremlin-go/driver/httpTransporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,18 @@ type HttpTransporter struct {
isClosed bool
connSettings *connectionSettings
responseChannel chan []byte // response channel needs to be per request, not per client
client http.Client
httpClient *http.Client
wg *sync.WaitGroup
}

func NewHttpTransporter(url string, connSettings *connectionSettings) *HttpTransporter {
transport := &http.Transport{
TLSClientConfig: connSettings.tlsConfig,
MaxConnsPerHost: 0, // TODO
IdleConnTimeout: 0, // TODO
DisableCompression: !connSettings.enableCompression,
}

c := http.Client{
Transport: transport,
Timeout: connSettings.connectionTimeout,
}

func NewHttpTransporter(url string, connSettings *connectionSettings, httpClient *http.Client) *HttpTransporter {
wg := &sync.WaitGroup{}

return &HttpTransporter{
url: url,
connSettings: connSettings,
responseChannel: make(chan []byte, writeChannelSizeDefault),
client: c,
httpClient: httpClient,
wg: wg,
}
}
Expand Down Expand Up @@ -98,7 +86,7 @@ func (transporter *HttpTransporter) Write(data []byte) error {
ContentLength: int64(len(data)),
}

resp, err := transporter.client.Do(&req)
resp, err := transporter.httpClient.Do(&req)
if err != nil {
return err
}
Expand Down

0 comments on commit d6303ef

Please sign in to comment.