mirror of
https://github.com/lukaszraczylo/claude-mnemonic.git
synced 2026-10-18 06:09:15 +00:00
perf(statusline): use shared minimal client, drop net/http
This commit is contained in:
@@ -3,9 +3,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@@ -13,6 +13,9 @@ import (
|
|||||||
"github.com/lukaszraczylo/claude-mnemonic/pkg/hooks"
|
"github.com/lukaszraczylo/claude-mnemonic/pkg/hooks"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// statsTimeout bounds the worker stats request.
|
||||||
|
const statsTimeout = 100 * time.Millisecond
|
||||||
|
|
||||||
// StatusInput is the JSON input from Claude Code's statusline feature.
|
// StatusInput is the JSON input from Claude Code's statusline feature.
|
||||||
type StatusInput struct {
|
type StatusInput struct {
|
||||||
HookEventName string `json:"hook_event_name"`
|
HookEventName string `json:"hook_event_name"`
|
||||||
@@ -105,26 +108,19 @@ func handleStatusline(input *StatusInput, port int) string {
|
|||||||
// getWorkerStats fetches stats from the worker service.
|
// getWorkerStats fetches stats from the worker service.
|
||||||
func getWorkerStats(port int, project string) *WorkerStats {
|
func getWorkerStats(port int, project string) *WorkerStats {
|
||||||
// Build URL with optional project parameter
|
// Build URL with optional project parameter
|
||||||
endpoint := fmt.Sprintf("http://127.0.0.1:%d/api/stats", port)
|
endpoint := "/api/stats"
|
||||||
if project != "" {
|
if project != "" {
|
||||||
endpoint += "?project=" + url.QueryEscape(project)
|
endpoint += "?project=" + url.QueryEscape(project)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create HTTP client with short timeout (statusline must be fast)
|
// Short timeout: statusline must be fast
|
||||||
client := &http.Client{Timeout: 100 * time.Millisecond}
|
body, err := hooks.GETBody(context.Background(), statsTimeout, port, endpoint)
|
||||||
|
|
||||||
resp, err := client.Get(endpoint)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
defer func() { _ = resp.Body.Close() }()
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var stats WorkerStats
|
var stats WorkerStats
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&stats); err != nil {
|
if err := json.Unmarshal(body, &stats); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -79,3 +79,16 @@ func parseHTTPResponse(raw []byte) (*httpResponse, error) {
|
|||||||
}
|
}
|
||||||
return &httpResponse{Status: code, StatusText: rest, Body: body}, nil
|
return &httpResponse{Status: code, StatusText: rest, Body: body}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GETBody fetches path from the worker within timeout and returns the raw body.
|
||||||
|
// Statuses of 400 and above are returned as errors.
|
||||||
|
func GETBody(ctx context.Context, timeout time.Duration, port int, path string) ([]byte, error) {
|
||||||
|
resp, err := httpDo(ctx, timeout, "GET", port, path, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if resp.Status >= 400 {
|
||||||
|
return nil, fmt.Errorf("request failed: %s", resp.StatusText)
|
||||||
|
}
|
||||||
|
return resp.Body, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user