golang的高并发

wz998 · · 1239 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

几个方法

SetMaxStack设置该以被单个go程调用栈可使用的内存最大值。如果任何go程在增加其调用栈时超出了该限制,程序就会崩溃。SetMaxStack返回之前的设置。默认设置在32位系统是250MB,在64位系统是1GB。
SetMaxThreads设置go程序可以使用的最大操作系统线程数。如果程序试图使用超过该限制的线程数,就会导致程序崩溃。SetMaxThreads返回之前的设置,初始设置为10000个线程。

fmt.Println("runtime.NumCPU:", runtime.NumCPU())
fmt.Println("runtime.NumCgoCall:", runtime.NumCgoCall())
fmt.Println("runtime.NumGoroutine:", runtime.NumGoroutine())
fmt.Println("runtime.GOMAXPROCS:", runtime.GOMAXPROCS(0)) //GOMAXPROCS设置可同时执行的最大CPU数

一个例子

package main

import (
    "fmt"
    "os"
    "runtime"
    "time"
)

// SetMaxStack设置该以被单个go程调用栈可使用的内存最大值。如果任何go程在增加其调用栈时超出了该限制,程序就会崩溃。SetMaxStack返回之前的设置。默认设置在32位系统是250MB,在64位系统是1GB。
// SetMaxThreads设置go程序可以使用的最大操作系统线程数。如果程序试图使用超过该限制的线程数,就会导致程序崩溃。SetMaxThreads返回之前的设置,初始设置为10000个线程。
func main() {
    fmt.Println("runtime.NumCPU:", runtime.NumCPU())
    fmt.Println("runtime.NumCgoCall:", runtime.NumCgoCall())
    fmt.Println("runtime.NumGoroutine:", runtime.NumGoroutine())
    fmt.Println("runtime.GOMAXPROCS:", runtime.GOMAXPROCS(0)) //GOMAXPROCS设置可同时执行的最大CPU数

    NewDispatcher(1).Run()
    fmt.Println("收到 接收到红包数据 http请求")
    mtaskRequest := MtaskRequest{67}
    work := Job{MtaskRequest: mtaskRequest}
    for i := 0; i < 10; i++ {
        JobQueue <- work
    }
    time.Sleep(time.Second * 2)
    fmt.Println("runtime.NumCPU:", runtime.NumCPU())
    fmt.Println("runtime.NumCgoCall:", runtime.NumCgoCall())
    fmt.Println("runtime.NumGoroutine:", runtime.NumGoroutine())
    fmt.Println("runtime.GOMAXPROCS:", runtime.GOMAXPROCS(0))
    time.Sleep(time.Second * 100)
}

//任务的请求
type MtaskRequest struct {
    Ceshi int
    // [redacted]
}

//job队列+work池
var (
    MaxWorker = os.Getenv("MAX_WORKERS")
    MaxQueue  = os.Getenv("MAX_QUEUE")
)

// Job represents the job to be run
type Job struct {
    MtaskRequest MtaskRequest
}

// A buffered channel that we can send work requests on.

// var JobQueue chan Job ---这样申明会卡主,没有初始化
var JobQueue = make(chan Job)

// Worker represents the worker that executes the job
type Worker struct {
    WorkerPool chan chan Job
    JobChannel chan Job
    quit       chan bool
}

func NewWorker(workerPool chan chan Job) Worker {
    return Worker{
        WorkerPool: workerPool,
        JobChannel: make(chan Job),
        quit:       make(chan bool)}
}

// Stop signals the worker to stop listening for work requests.
func (w Worker) Stop() {
    go func() {
        w.quit <- true
    }()
}

type Dispatcher struct {
    // A pool of workers channels that are registered with the dispatcher
    WorkerPool chan chan Job
    maxWorkers int
}

func NewDispatcher(maxWorkers int) *Dispatcher {
    pool := make(chan chan Job, maxWorkers)
    return &Dispatcher{WorkerPool: pool, maxWorkers: maxWorkers}
}

var num = 0

// Start method starts the run loop for the worker, listening for a quit channel in
// case we need to stop it
func (w Worker) Start() {
    go func() {
        for {
            // register the current worker into the worker queue.
            w.WorkerPool <- w.JobChannel
            select {
            case <-w.JobChannel:
                time.Sleep(1 * time.Second)
                // we have received a work request.
                num++
                fmt.Println("调起worker:", num)
            case <-w.quit:
                // we have received a signal to stop
                return
                //不能写default
            }
        }
    }()
}

func (d *Dispatcher) Run() {
    //启动一定数量的worker
    fmt.Println("启动一定数量的worker")
    for i := 0; i < d.maxWorkers; i++ {
        worker := NewWorker(d.WorkerPool)
        worker.Start()
    }

    go d.dispatch()
}

var num2 = 0

//分派任务
func (d *Dispatcher) dispatch() {
    for {
        select {
        case job := <-JobQueue: //接收一个job请求
            num2++
            fmt.Println("JobQueue 收到请求:", num2)
            // jobChannel := <-d.WorkerPool
            // // dispatch the job to the worker job channel
            // jobChannel <- job
            go func(job Job) {
                // try to obtain a worker job channel that is available.
                // this will block until a worker is idle
                jobChannel := <-d.WorkerPool
                // dispatch the job to the worker job channel
                jobChannel <- job
            }(job)
        }
    }
}

// //接收到红包数据
// func (this *TaskRedbao) UserGetRedbao(red_id, uid, shop_id, rand_arr, Amoney string) error {
//  fmt.Println("收到 接收到红包数据 http请求")
//  mtaskRequest := MtaskRequest{67}
//  work := Job{MtaskRequest: mtaskRequest}

//  JobQueue <- work
//  return nil
// }

转载自 https://blog.csdn.net/jzbis/article/details/83066127


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:wz998

查看原文:golang的高并发

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1239 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传