golang mysql 高并发_golang的高并发

本文介绍了如何在Golang中设置并发限制,包括最大栈内存、线程数,并展示了如何利用goroutine和worker池处理高并发任务。通过示例代码展示了Dispatcher、Worker和JobQueue的实现,用于高效分发和执行任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

几个方法

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

}

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

}()

}

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

select {

case

time.Sleep(1 * time.Second)

// we have received a work request.

num++

fmt.Println("调起worker:", num)

case

// 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 :=

num2++

fmt.Println("JobQueue 收到请求:", num2)

// jobChannel :=

// // dispatch the job to the worker job channel

// jobChannel

go func(job Job) {

// try to obtain a worker job channel that is available.

// this will block until a worker is idle

jobChannel :=

// dispatch the job to the worker job channel

jobChannel

}(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

// return nil

// }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值