Skip to content

Commit 546d92e

Browse files
authored
Merge pull request #75 from vinyas-bharadwaj/main
Introduced a shared HTTP client with optimized settings
2 parents bc0e7c3 + eacc47c commit 546d92e

5 files changed

Lines changed: 66 additions & 34 deletions

File tree

internal/claude/claude.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net/http"
99

10+
httpClient "github.com/dfanso/commit-msg/internal/http"
1011
"github.com/dfanso/commit-msg/pkg/types"
1112
)
1213

@@ -58,7 +59,7 @@ func GenerateCommitMessage(config *types.Config, changes string, apiKey string,
5859
req.Header.Set("x-api-key", apiKey)
5960
req.Header.Set("anthropic-version", "2023-06-01")
6061

61-
client := &http.Client{}
62+
client := httpClient.GetClient()
6263
resp, err := client.Do(req)
6364
if err != nil {
6465
return "", err

internal/grok/grok.go

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,15 @@ package grok
22

33
import (
44
"bytes"
5-
"crypto/tls"
65
"encoding/json"
76
"fmt"
87
"io"
98
"net/http"
10-
"sync"
11-
"time"
129

10+
httpClient "github.com/dfanso/commit-msg/internal/http"
1311
"github.com/dfanso/commit-msg/pkg/types"
1412
)
1513

16-
var (
17-
grokClientOnce sync.Once
18-
grokClient *http.Client
19-
)
20-
21-
func getHTTPClient() *http.Client {
22-
grokClientOnce.Do(func() {
23-
transport := &http.Transport{
24-
TLSHandshakeTimeout: 10 * time.Second,
25-
MaxIdleConns: 10,
26-
IdleConnTimeout: 30 * time.Second,
27-
DisableCompression: true,
28-
TLSClientConfig: &tls.Config{
29-
InsecureSkipVerify: false,
30-
},
31-
}
32-
grokClient = &http.Client{
33-
Timeout: 30 * time.Second,
34-
Transport: transport,
35-
}
36-
})
37-
return grokClient
38-
}
39-
4014
// GenerateCommitMessage calls X.AI's Grok API to create a commit message from
4115
// the provided Git diff and generation options.
4216
func GenerateCommitMessage(config *types.Config, changes string, apiKey string, opts *types.GenerationOptions) (string, error) {
@@ -71,7 +45,7 @@ func GenerateCommitMessage(config *types.Config, changes string, apiKey string,
7145
req.Header.Set("Content-Type", "application/json")
7246
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
7347

74-
client := getHTTPClient()
48+
client := httpClient.GetClient()
7549
resp, err := client.Do(req)
7650
if err != nil {
7751
return "", err

internal/groq/groq.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"io"
88
"net/http"
99
"os"
10-
"time"
1110

11+
httpClient "github.com/dfanso/commit-msg/internal/http"
1212
"github.com/dfanso/commit-msg/pkg/types"
1313
)
1414

@@ -38,8 +38,7 @@ const defaultModel = "llama-3.3-70b-versatile"
3838

3939
var (
4040
// allow overrides in tests
41-
baseURL = "https://api.groq.com/openai/v1/chat/completions"
42-
httpClient = &http.Client{Timeout: 30 * time.Second}
41+
baseURL = "https://api.groq.com/openai/v1/chat/completions"
4342
)
4443

4544
// GenerateCommitMessage calls Groq's OpenAI-compatible chat completions API.
@@ -83,7 +82,7 @@ func GenerateCommitMessage(_ *types.Config, changes string, apiKey string, opts
8382
req.Header.Set("Content-Type", "application/json")
8483
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
8584

86-
resp, err := httpClient.Do(req)
85+
resp, err := httpClient.GetClient().Do(req)
8786
if err != nil {
8887
return "", fmt.Errorf("failed to call Groq API: %w", err)
8988
}

internal/http/client.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package http
2+
3+
import (
4+
"crypto/tls"
5+
"net/http"
6+
"sync"
7+
"time"
8+
)
9+
10+
var (
11+
clientOnce sync.Once
12+
sharedClient *http.Client
13+
14+
ollamaClientOnce sync.Once
15+
ollamaClient *http.Client
16+
)
17+
18+
// createTransport creates a shared HTTP transport with optimized settings
19+
func createTransport() *http.Transport {
20+
return &http.Transport{
21+
TLSHandshakeTimeout: 10 * time.Second,
22+
MaxIdleConns: 10,
23+
IdleConnTimeout: 30 * time.Second,
24+
DisableCompression: true,
25+
TLSClientConfig: &tls.Config{
26+
InsecureSkipVerify: false,
27+
},
28+
}
29+
}
30+
31+
// GetClient returns a shared HTTP client with optimized settings for cloud APIs
32+
func GetClient() *http.Client {
33+
clientOnce.Do(func() {
34+
sharedClient = &http.Client{
35+
Timeout: 30 * time.Second,
36+
Transport: createTransport(),
37+
}
38+
})
39+
return sharedClient
40+
}
41+
42+
// GetOllamaClient returns an HTTP client with extended timeout for local Ollama inference
43+
func GetOllamaClient() *http.Client {
44+
ollamaClientOnce.Do(func() {
45+
ollamaClient = &http.Client{
46+
Timeout: 10 * time.Minute, // 10 minutes for local inference
47+
Transport: createTransport(),
48+
}
49+
})
50+
return ollamaClient
51+
}

internal/ollama/ollama.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"net/http"
99

10+
httpClient "github.com/dfanso/commit-msg/internal/http"
1011
"github.com/dfanso/commit-msg/pkg/types"
1112
)
1213

@@ -46,7 +47,13 @@ func GenerateCommitMessage(_ *types.Config, changes string, url string, model st
4647
return "", fmt.Errorf("failed to marshal request: %v", err)
4748
}
4849

49-
resp, err := http.Post(url, "application/json", bytes.NewBuffer(body))
50+
req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
51+
if err != nil {
52+
return "", fmt.Errorf("failed to create request: %v", err)
53+
}
54+
req.Header.Set("Content-Type", "application/json")
55+
56+
resp, err := httpClient.GetOllamaClient().Do(req)
5057
if err != nil {
5158
return "", fmt.Errorf("failed to send request to Ollama: %v", err)
5259
}

0 commit comments

Comments
 (0)