MCPcopy Index your code
hub / github.com/subtrace/subtrace / Call

Function Call

rpc/rpc.go:62–119  ·  view source on GitHub ↗
(ctx context.Context, w proto.Message, path string, r *R, opts ...Option)

Source from the content-addressed store, hash-verified

60}
61
62func Call[R any, PR ptr[R]](ctx context.Context, w proto.Message, path string, r *R, opts ...Option) (int, error) {
63 b, err := protojson.Marshal(PR(r))
64 if err != nil {
65 return 0, fmt.Errorf("marshal: %w", err)
66 }
67
68 start := time.Now()
69
70 req, err := http.NewRequestWithContext(ctx, "POST", Format(path), bytes.NewBuffer(b))
71 if err != nil {
72 return 0, fmt.Errorf("new request: %w", err)
73 }
74
75 opts = append(opts, WithTag("subtrace_client_version", version.GetCanonicalString()))
76 for _, opt := range append(defaultOptions, opts...) {
77 opt(req)
78 }
79
80 resp, err := http.DefaultClient.Do(req)
81 if err != nil {
82 return 0, fmt.Errorf("do request: %w", err)
83 }
84 defer resp.Body.Close()
85
86 defer func() {
87 slog.Debug("http request", slog.Group("req",
88 "id", resp.Header.Get("x-subtrace-id"),
89 "method", req.Method,
90 "host", req.URL.Host,
91 "path", req.URL.Path,
92 "code", resp.StatusCode,
93 "status", resp.Status,
94 "took", time.Since(start).Round(time.Microsecond),
95 ))
96 }()
97
98 if resp.StatusCode >= 500 {
99 return resp.StatusCode, fmt.Errorf("%s", resp.Status)
100 }
101
102 b, err = io.ReadAll(io.LimitReader(resp.Body, 64<<20)) // 64 MB limit
103 if err != nil {
104 return 0, fmt.Errorf("read response: %w", err)
105 }
106
107 if err := protojson.Unmarshal(b, w); err != nil {
108 if errors.Is(err, io.EOF) {
109 return resp.StatusCode, fmt.Errorf("%s", resp.Status)
110 }
111 return resp.StatusCode, fmt.Errorf("unmarshal: %s: %w", resp.Status, err)
112 }
113 if e, ok := w.(interface{ GetError() string }); ok {
114 if err := e.GetError(); err != "" {
115 return resp.StatusCode, fmt.Errorf("client error: %s: %s", resp.Status, err)
116 }
117 }
118 return resp.StatusCode, nil
119}

Callers 4

dialSingleMethod · 0.92
flushOnceMethod · 0.92
loopMethod · 0.92
getWebsocketURLFunction · 0.92

Calls 6

GetCanonicalStringFunction · 0.92
FormatFunction · 0.85
WithTagFunction · 0.85
CloseMethod · 0.45
GetMethod · 0.45
GetErrorMethod · 0.45

Tested by

no test coverage detected