Skip to content

Commit 91457b4

Browse files
Introduced a shared HTTP client with optimized settings
1 parent bc0e7c3 commit 91457b4

6 files changed

Lines changed: 74 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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
// GetClient returns a shared HTTP client with optimized settings for cloud APIs
19+
func GetClient() *http.Client {
20+
clientOnce.Do(func() {
21+
transport := &http.Transport{
22+
TLSHandshakeTimeout: 10 * time.Second,
23+
MaxIdleConns: 10,
24+
IdleConnTimeout: 30 * time.Second,
25+
DisableCompression: true,
26+
TLSClientConfig: &tls.Config{
27+
InsecureSkipVerify: false,
28+
},
29+
}
30+
31+
sharedClient = &http.Client{
32+
Timeout: 30 * time.Second,
33+
Transport: transport,
34+
}
35+
})
36+
return sharedClient
37+
}
38+
39+
// GetOllamaClient returns an HTTP client with extended timeout for local Ollama inference
40+
func GetOllamaClient() *http.Client {
41+
ollamaClientOnce.Do(func() {
42+
transport := &http.Transport{
43+
TLSHandshakeTimeout: 10 * time.Second,
44+
MaxIdleConns: 10,
45+
IdleConnTimeout: 30 * time.Second,
46+
DisableCompression: true,
47+
TLSClientConfig: &tls.Config{
48+
InsecureSkipVerify: false,
49+
},
50+
}
51+
52+
ollamaClient = &http.Client{
53+
Timeout: 10 * time.Minute, // 10 minutes for local inference
54+
Transport: transport,
55+
}
56+
})
57+
return ollamaClient
58+
}

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
}

test2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test change for shared client

0 commit comments

Comments
 (0)