Skip to content

Commit

Permalink
fix: ungced memory retention in request dump buffer (#180)
Browse files Browse the repository at this point in the history
* fix: ungced memory retention in request dump buffer

---------

Signed-off-by: Neko Ayaka <[email protected]>
  • Loading branch information
nekomeowww authored Dec 8, 2023
1 parent 717567b commit 90a5005
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion internal/models/chathistories/chat_histories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestExtractTextFromMessage(t *testing.T) {
}, nil
}

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"
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)"
assert.Equal(expect, model.ExtractTextFromMessage(message))
})
}
Expand Down
7 changes: 5 additions & 2 deletions internal/models/smr/smr.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,19 @@ func (m *Model) extractContentFromURL(ctx context.Context, urlString string) (*r
}, nil
}

dumpBuffer := new(bytes.Buffer)
defer dumpBuffer.Reset()

resp, err := m.req.
R().
EnableDump().
EnableDumpTo(dumpBuffer).
SetContext(ctx).
Get(parsedURL.String())
if err != nil {
return nil, fmt.Errorf("failed to get url %s, %w: %v", parsedURL.String(), ErrNetworkError, err)
}
if !resp.IsSuccessState() {
return nil, fmt.Errorf("failed to get url %s, %w, status code: %d, dump: %s", parsedURL.String(), ErrRequestFailed, resp.StatusCode, resp.Dump())
return nil, fmt.Errorf("failed to get url %s, %w, status code: %d, dump: %s", parsedURL.String(), ErrRequestFailed, resp.StatusCode, dumpBuffer.String())
}
if !strings.Contains(resp.Header.Get("Content-Type"), "text/html") {
return nil, fmt.Errorf("url fetched, but content-type not supported yet, %w, content-type: %s", ErrContentNotSupported, resp.Header.Get("Content-Type"))
Expand Down
15 changes: 7 additions & 8 deletions pkg/linkprev/linkprev.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ func NewClient() *Client {
}
}

func (c *Client) Debug() *Client {
c.reqClient.EnableDumpAll()
return c
}

func (c *Client) Preview(ctx context.Context, urlStr string) (Meta, error) {
r := c.newRequest(ctx, urlStr)

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

c.alterRequestForTwitter(request, urlStr)
Expand Down Expand Up @@ -92,12 +86,17 @@ func (c *Client) alterRequestForTwitter(request *req.Request, urlStr string) *re
}

func (c *Client) request(r *req.Request, urlStr string) (io.Reader, error) {
resp, err := r.Get(urlStr)
dumpBuffer := new(bytes.Buffer)
defer dumpBuffer.Reset()

resp, err := r.
EnableDumpTo(dumpBuffer).
Get(urlStr)
if err != nil {
return nil, fmt.Errorf("failed to get a preview of url %s, %w: %v", urlStr, ErrNetworkError, err)
}
if !resp.IsSuccessState() {
return nil, fmt.Errorf("failed to get url %s, %w, status code: %d, dump: %s", urlStr, ErrRequestFailed, resp.StatusCode, resp.Dump())
return nil, fmt.Errorf("failed to get url %s, %w, status code: %d, dump: %s", urlStr, ErrRequestFailed, resp.StatusCode, dumpBuffer.String())
}

defer resp.Body.Close()
Expand Down

0 comments on commit 90a5005

Please sign in to comment.