• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Go]TCP服务中增加消息队列与工作池

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

之前的处理中每一个连接都会创建一个主groutine , 每个连接中的主groutine中创建出读groutine 和写groutine

每个连接处理业务再单独开出一个groutine ,这样如果有10万并发的连接 , 将会出现30万groutine ,其中读写占20万阻塞住的 , 不占用资源。处理业务的有10万groutine ,会不停的切换 , 比较占有CPU资源 , 现在把处理业务的groutine限制住 ,创建出一个工作池,里面存的是每个worker ,每个worker groutine去读取自己对应的channel ,这个channel是个有缓存的channel作为消息队列使用

package snet

import (
    "bufio"
    "fmt"
    "log"
    "math/rand"
    "net"
    "time"
)

type Conn struct {
    IP             string
    Port           uint32
    TCPConn        *net.TCPConn
    MsgChan        chan []byte
    ExitChan       chan bool
    Closed         bool
    WorkerPool     []chan []byte
    WorkerPoolSize uint32
    PreWorkerQueue uint32
}

func NewConn(IP string, Port uint32, WorkerPoolSize uint32) *Conn {
    s := &Conn{
        IP:             IP,
        Port:           Port,
        MsgChan:        make(chan []byte),
        ExitChan:       make(chan bool),
        WorkerPool:     make([]chan []byte, WorkerPoolSize),
        WorkerPoolSize: WorkerPoolSize,
        PreWorkerQueue: 1024,
    }
    return s
}

func (c *Conn) Start() {
    log.Printf("%s:%d start...\n", c.IP, c.Port)
    go func() {
        c.StartWorkerPool()
        addr, err := net.ResolveTCPAddr("tcp4", fmt.Sprintf("%s:%d", c.IP, c.Port))
        if err != nil {
            log.Println("resolve tcp addr err ", err)
            return
        }
        listener, err := net.ListenTCP("tcp4", addr)
        if err != nil {
            log.Println("listen tcp err ", err)
            return
        }
        var connid uint32
        connid = 0
        for {
            conn, err := listener.AcceptTCP()
            if err != nil {
                log.Println("accept tcp err ", err)
                continue
            }
            c.TCPConn = conn
            go c.StartRead()
            go c.StartWrite()
            connid++
        }
    }()
    select {}
}
func (c *Conn) StartRead() {
    log.Println("read groutine is waiting")
    defer c.Stop()
    defer log.Println("read groutine exit")
    reader := bufio.NewReader(c.TCPConn)
    for {
        lineBytes, err := reader.ReadBytes('\n')
        if err != nil {
            log.Println("startread read bytes error ", err)
            break
        }
        len := len(lineBytes)
        line := lineBytes[:len-1]
        log.Println("start read from client ", string(line))
        if c.WorkerPoolSize>0{
            c.SendMsgToWorker(line)
        }else{
            go c.HandleMsg(line)
        }
    }
}
func (c *Conn) StartWrite() {
    log.Println("write groutine is waiting")
    defer log.Println("write groutine exit")
    for {
        select {
        case data := <-c.MsgChan:
            if _, err := c.TCPConn.Write(data); err != nil {
                log.Println("startwrite conn write error ", err)
                return
            }
            log.Println("start write from server ", string(data))
        case <-c.ExitChan:
            return
        }
    }
}
func (c *Conn) HandleMsg(data []byte) {
    res := fmt.Sprintf("res:%s", string(data))
    c.MsgChan <- []byte(res)
}
func (c *Conn) SendMsgToWorker(data []byte) {
    rand.Seed(time.Now().UnixNano())
    workerId := rand.Intn(int(c.WorkerPoolSize))
    c.WorkerPool[workerId] <- data
}
func (c *Conn) StartWorkerPool() {
    for i := 0; i < int(c.WorkerPoolSize); i++ {
        c.WorkerPool[i] = make(chan []byte, c.PreWorkerQueue)
        go c.StartOneWorker(i, c.WorkerPool[i])
    }
}
func (c *Conn) StartOneWorker(workerId int, queue chan []byte) {
    log.Println("start one worker groutine is waiting:", workerId)
    for {
        select {
        case data := <-queue:
            c.HandleMsg(data)
            log.Println("one worker groutine is finshed:", workerId)
        }
    }
}
func (c *Conn) Stop() {
    if c.Closed {
        return
    }
    c.Closed = true
    c.ExitChan <- true

    c.TCPConn.Close()
    close(c.ExitChan)
    close(c.MsgChan)
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ReelSteady Go for mac(GoPro相机防抖处理工具)发布时间:2022-07-10
下一篇:
Go初接触之归并排序发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap