ylywyn / jpush-api-go-client

jpush-api-go-client
183 stars 106 forks source link

文件句柄泄露问题 #8

Closed maskwang closed 6 years ago

maskwang commented 6 years ago

当大规模调用推送,会导致tcp全部都是链接状态,而不能复用以及释放, 进而导致文件句柄占满,导致服务不可用

ylywyn commented 6 years ago

使用 SendPushBytes(), // str, err := c.SendPushBytes(bytes)

maskwang commented 6 years ago

代码如下:

package main

import (
    "fmt"
    "log"
    "os"
    "os/exec"
    "strings"

    "github.com/ylywyn/jpush-api-go-client"
)

var url = "http://www.baidu.com/"

func main() {
    for i := 0; i < 200; i++ {
        go func() {
            jpushclient.SendPostBytes(url, []byte("123"), "")
            //jpushclient.SendPostBytes2(url, []byte("123"), "")
            log.Println("done")
        }()
        //time.Sleep(1 * time.Second)
        log.Println("open files:", openFiles())
    }

    select {}
}

func openFiles() int {
    out, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("lsof -p %v", os.Getpid())).Output()
    if err != nil {
        log.Fatal(err)
    }
    lines := strings.Split(string(out), "\n")
    return len(lines) - 1
}

结果一样的: SendPostBytes方法测试结果: image

SendPostBytes2方法测试结果: image