首頁  >  文章  >  後端開發  >  html 渲染函數記憶體洩漏

html 渲染函數記憶體洩漏

王林
王林轉載
2024-02-06 10:39:11626瀏覽

html 渲染函数内存泄漏

問題內容

我面臨的問題是,即使僅嘗試200 個請求也會導致程式佔用容器的6gb 記憶體並最終被oom 殺死。 我的想法是提取 html 中存在的所有文字節點,然後處理它們以提取它們的名稱、該標籤的 html 和文字。因此,為了產生特定標籤的 html,我使用 golang.org/x/net/html 中的 render 函數。其中我提供 strings.builder 作為 io.writer 來寫產生的 html。但由於某種原因,建構器佔用了太多記憶體。

package main

import (
    "encoding/csv"
    "io"
    "log"
    "net/http"
    "strings"
    "golang.org/x/net/html"
)

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/data", GetData)
    if err := http.ListenAndServe(":8001", mux); err != nil {
        log.Println(err)
    }
}

type TagInfo struct {
    Tag  string
    Name string
    Text string
}

// http.handler
func GetData(w http.ResponseWriter, r *http.Request) {
    u := r.URL.Query().Get("url")
    doc, err := GetDoc(u)
    if err != nil {
        log.Println(err)
        w.WriteHeader(500)
        return
    }
    var buf strings.Builder
    data := Extract(doc, &buf)
    csvw := csv.NewWriter(io.Discard)
    for _, d := range data {
        csvw.Write([]string{d.Name, d.Tag, d.Text})
    }
}

// fires request and get text/html
func GetDoc(u string) (*html.Node, error) {
    res, err := http.Get(u)
    if err != nil {
        return nil, err
    }
    defer res.Body.Close()
    return html.Parse(res.Body)
}

func Extract(doc *html.Node, buf *strings.Builder) []TagInfo {
    var (
        tags = make([]TagInfo, 0, 100)
        f    func(*html.Node)
    )

    f = func(n *html.Node) {
        if n.Type == html.TextNode {
            text := strings.TrimSpace(n.Data)
            if text != "" {
                parent := n.Parent
                tag := Render(parent, buf)
                tagInfo := TagInfo{
                    Tag:  tag,
                    Name: parent.Data,
                    Text: n.Data,
                }
                tags = append(tags, tagInfo)
            }
        }
        for child := n.FirstChild; child != nil; child = child.NextSibling {
            f(child)
        }
    }
    f(doc)
    return tags
}

// Render the html around the tag
// if node is text then pass the
// parent node paramter in function
func Render(n *html.Node, buf *strings.Builder) string {
    defer buf.Reset()
    if err := html.Render(buf, n); err != nil {
        log.Println(err)
        return ""
    }
    return buf.String()
}

如果您想要特定的網址列表,這裡就是。我一次發出了大約 60 個請求。

我嘗試使用 bytes.buffer bytes.buffersync.pool 但兩者都有相同的問題。使用 pprof 我注意到 strings.builder 的 writestring 方法導致大量記憶體使用。


正確答案


所以這裡的基本問題是接受任何content-type ,這在抓取方面是不可接受的,大多數網站都需要傳送text/html

問題是即使url發送任何不代表html資料的內容golang.org/x/net/html仍然接受它而不拋出錯誤。

讓我們舉一個例子,其中返回application/pdf ,然後正文將包含html.Parse 解析的pdf 的二進位數據,並且不會返回任何錯誤,這是用於抓取/爬行接受二進位資料的奇怪行為思維庫。

解決方案是:檢查回應頭,如果只有資料是html,然後繼續,否則會出現歧義或更高的記憶體使用量(可能更低),但我們無法預測會發生什麼發生。

以上是html 渲染函數記憶體洩漏的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除