Skip to content

Commit 90a5005

Browse files
authored
fix: ungced memory retention in request dump buffer (#180)
* fix: ungced memory retention in request dump buffer --------- Signed-off-by: Neko Ayaka <[email protected]>
1 parent 717567b commit 90a5005

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

internal/models/chathistories/chat_histories_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestExtractTextFromMessage(t *testing.T) {
9393
}, nil
9494
}
9595

96-
expect := "看看这些链接:[Documentation](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/stringsandcharacters/#Extended-Grapheme-Clusters) 、[GPT-4 Developer Livestream - YouTube](https://www.youtube.com/watch?v=outcGtbnMuQ) [GitHub - nekomeowww/insights-bot: A bot works with OpenAI GPT models to provide insights for your info flows.](https://github.com/nekomeowww/insights-bot) 还有 [这个](https://matters.town/@1435Club/322889-这几天-web3在大理发生了什么),和这个 https://twitter.com/GoogleDevEurope/status/1640667303158198272"
96+
expect := "看看这些链接:[Documentation](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/stringsandcharacters/#Extended-Grapheme-Clusters) 、[GPT-4 Developer Livestream - YouTube](https://www.youtube.com/watch?v=outcGtbnMuQ) [GitHub - nekomeowww/insights-bot: A bot works with OpenAI GPT models to provide insights for your info flows.](https://github.com/nekomeowww/insights-bot) 还有 [这个](https://matters.town/@1435Club/322889-这几天-web3在大理发生了什么),和这个 [Google for Developers Europe (@GoogleDevEurope) on X](https://twitter.com/GoogleDevEurope/status/1640667303158198272)"
9797
assert.Equal(expect, model.ExtractTextFromMessage(message))
9898
})
9999
}

internal/models/smr/smr.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,19 @@ func (m *Model) extractContentFromURL(ctx context.Context, urlString string) (*r
167167
}, nil
168168
}
169169

170+
dumpBuffer := new(bytes.Buffer)
171+
defer dumpBuffer.Reset()
172+
170173
resp, err := m.req.
171174
R().
172-
EnableDump().
175+
EnableDumpTo(dumpBuffer).
173176
SetContext(ctx).
174177
Get(parsedURL.String())
175178
if err != nil {
176179
return nil, fmt.Errorf("failed to get url %s, %w: %v", parsedURL.String(), ErrNetworkError, err)
177180
}
178181
if !resp.IsSuccessState() {
179-
return nil, fmt.Errorf("failed to get url %s, %w, status code: %d, dump: %s", parsedURL.String(), ErrRequestFailed, resp.StatusCode, resp.Dump())
182+
return nil, fmt.Errorf("failed to get url %s, %w, status code: %d, dump: %s", parsedURL.String(), ErrRequestFailed, resp.StatusCode, dumpBuffer.String())
180183
}
181184
if !strings.Contains(resp.Header.Get("Content-Type"), "text/html") {
182185
return nil, fmt.Errorf("url fetched, but content-type not supported yet, %w, content-type: %s", ErrContentNotSupported, resp.Header.Get("Content-Type"))

pkg/linkprev/linkprev.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ func NewClient() *Client {
3333
}
3434
}
3535

36-
func (c *Client) Debug() *Client {
37-
c.reqClient.EnableDumpAll()
38-
return c
39-
}
40-
4136
func (c *Client) Preview(ctx context.Context, urlStr string) (Meta, error) {
4237
r := c.newRequest(ctx, urlStr)
4338

@@ -59,7 +54,6 @@ func (c *Client) Preview(ctx context.Context, urlStr string) (Meta, error) {
5954
func (c *Client) newRequest(ctx context.Context, urlStr string) *req.Request {
6055
request := c.reqClient.
6156
R().
62-
EnableDump().
6357
SetContext(ctx)
6458

6559
c.alterRequestForTwitter(request, urlStr)
@@ -92,12 +86,17 @@ func (c *Client) alterRequestForTwitter(request *req.Request, urlStr string) *re
9286
}
9387

9488
func (c *Client) request(r *req.Request, urlStr string) (io.Reader, error) {
95-
resp, err := r.Get(urlStr)
89+
dumpBuffer := new(bytes.Buffer)
90+
defer dumpBuffer.Reset()
91+
92+
resp, err := r.
93+
EnableDumpTo(dumpBuffer).
94+
Get(urlStr)
9695
if err != nil {
9796
return nil, fmt.Errorf("failed to get a preview of url %s, %w: %v", urlStr, ErrNetworkError, err)
9897
}
9998
if !resp.IsSuccessState() {
100-
return nil, fmt.Errorf("failed to get url %s, %w, status code: %d, dump: %s", urlStr, ErrRequestFailed, resp.StatusCode, resp.Dump())
99+
return nil, fmt.Errorf("failed to get url %s, %w, status code: %d, dump: %s", urlStr, ErrRequestFailed, resp.StatusCode, dumpBuffer.String())
101100
}
102101

103102
defer resp.Body.Close()

0 commit comments

Comments
 (0)