为什么我的golang程序创建了这么多线程?

时间:2014-12-22 10:14:28

标签: go

我的服务器运行了一段时间,创建了大约200个连接并进行了一些计算并关闭,我发现它占用了大约2,7G的内存并且在几天之后从未减少过。程序本身并没有占用那么多,我通过memstats检查了它。通过cat /proc/11686/status | grep -i threads我得到Threads: 177,所以我认为它占用了大量内存的原因是它创建了很多线程。为什么go创建了这么多线程?是因为我使用了太多go func()吗?我确信goroutines没有增加,他们正常退出。

PS

我的程序中有很多代码,所以我排除了细节,只需保留主要的

我的问题是go创建一个线程来做某事。拥有这么多线程是正常的吗?我认为它对代码并不重视。

main.go

package main

import (
    "sanguo/base/log"
    "fmt"
    "runtime"
    "math/rand"
    "time"
    "net"
    "os"
)

type GameServer struct {
    Host   string
}


func (server *GameServer) Start() {
    // load system data
    log.Debug("/*************************SREVER START********************************/")

    tcpAddr, err := net.ResolveTCPAddr("tcp4", server.Host)
    if err != nil {
        log.Error(err.Error())
        os.Exit(-1)
    }
    go func(){
        for{
            select {
            case <-time.After(30*time.Second):
                LookUp("read memstats")
            }
        }
    }()
    listener, err := net.ListenTCP("tcp", tcpAddr)
    if err != nil {
        log.Error(err.Error())
        os.Exit(-1)
    }
    log.Debug("/*************************SERVER SUCC********************************/")
    for {
        conn, err := listener.AcceptTCP()
        if err != nil {
            continue
        }
        log.Debug("Accept a new connection ", conn.RemoteAddr())
        go handleClient(conn)
    }
}

func handleClient(conn *net.TCPConn) {
    sess := NewSession(conn)
    sess.Start()
}

func main() {
    rand.Seed(time.Now().Unix())

    runtime.GOMAXPROCS(runtime.NumCPU())

    log.SetLevel(0)

    filew := log.NewFileWriter("log", true)
    err := filew.StartLogger()
    if err != nil {
        fmt.Println("Failed start log",err)
        return
    }

    var server GameServer
    server.Host = "127.0.0.1:9999"
    server.Start()
}

session.go

package main

import (
    "io"
    "encoding/binary"
    "encoding/json"
    "github.com/felixge/tcpkeepalive"
    "net"
    "sanguo/base/log"
    "strings"
    "sync"
    "time"
)


type Session struct {

    conn *net.TCPConn //the tcp connection from client

    recvChan      chan *bufferedManager.Token //data from client
    closeNotiChan chan bool   //

    ok   bool
    lock sync.Mutex

}


func NewSession(connection *net.TCPConn) (sess *Session) {
    var client Session

    client.conn = connection

    client.recvChan = make(chan []byte, 1024)
    client.closeNotiChan = make(chan bool)
    client.ok = true

    log.Debug("New Connection", &client)

    kaConn, err := tcpkeepalive.EnableKeepAlive(connection)
    if err != nil {
        log.Debug("EnableKeepAlive err ", err)
    } else {
        kaConn.SetKeepAliveIdle(120 * time.Second)
        kaConn.SetKeepAliveCount(4)
        kaConn.SetKeepAliveInterval(5 * time.Second)
    }
    return &client
}


func (sess *Session) Close() {
    sess.lock.Lock()
    if sess.ok {
        sess.ok = false
        close(sess.closeNotiChan)
        sess.conn.Close()
        log.Trace("Sess Close Succ", sess, sess.uid)
    }
    sess.lock.Unlock()
}

func (sess *Session) handleRecv() {
    defer func(){
        if err := recover(); err != nil {
            log.Critical("Panic", err)
        }
        log.Trace("Session Recv Exit", sess, sess.uid)
        sess.Close()
    }()
    ch := sess.recvChan
    header := make([]byte, 2)
    for {
        /**block until recieve len(header)**/
        n, err := io.ReadFull(sess.conn, header)
        if n == 0 && err == io.EOF {
            //Opposite socket is closed
            log.Warn("Socket Read EOF And Close", sess)
            break
        } else if err != nil {
            //Sth wrong with this socket
            log.Warn("Socket Wrong:", err)
            break
        }
        size := binary.LittleEndian.Uint16(header) + 4
        data := make([]byte, size)
        n, err = io.ReadFull(sess.conn, t.Data)
        if n == 0 && err == io.EOF {
            log.Warn("Socket Read EOF And Close", sess)
            break
        } else if err != nil {
            log.Warn("Socket Wrong:", err)
            break
        }
        ch <- data //send data to Client to process
    }
}

func (sess *Session) handleDispatch() {
    defer func(){
        log.Trace("Session Dispatch Exit",  sess, sess.uid)
        sess.Close()
    }()
    for {
        select {
        case msg, _ := <-sess.recvChan:
            log.Debug("msg", msg)
            sess.SendDirectly("helloworldhellowor", 1)

        case <-sess.closeNotiChan:
                return
        }
    }
}

func (sess *Session) Start() {
    defer func() {
        if err := recover(); err != nil {
            log.Critical("Panic", err)
        }
    }()
    go sess.handleRecv()

    sess.handleDispatch()

    close(sess.recvChan)
    log.Warn("Session Start Exit", sess, sess.uid)
}


func (sess *Session) SendDirectly(back interface{}, op int) bool {
    back_json, err := json.Marshal(back)
    if err != nil {
        log.Error("Can't encode json message ", err, back)
        return false
    }
    log.Debug(sess.uid, "OUT cmd:", op, string(back_json))
    _, err = sess.conn.Write(back_json)
    if err != nil {
        log.Error("send fail", err)
        return false
    }
    return true
}

1 个答案:

答案 0 :(得分:6)

使用Go,您可以创建许多goroutine,它不应该增加线程数。在您的代码中,运行Go代码的线程数受到runtime.NumCPU()。

的限制

当goroutine必须执行阻塞调用(例如系统调用)或通过cgo调用C库时,可能会创建一个线程。在这种情况下,运行时调度程序从其调度池中删除运行goroutine的线程。如果调度池的线程数少于GOMAXPROCS,则将创建一个新的线程。

您可以在此处找到有关其工作原理的更多信息: https://softwareengineering.stackexchange.com/questions/222642/are-go-langs-goroutine-pools-just-green-threads/222694#222694

要理解代码生成线程的原因,您必须调查导致阻塞系统调用或C调用的所有代码路径。请注意,网络相关的调用是非阻塞的,因为它们由标准库自动复用。但是,如果执行某些磁盘I / O或调用外部库,则会生成线程。

例如,代码中使用的日志库可能会执行一些阻塞I / O,导致创建线程(特别是如果生成的文件托管在慢速设备上)。