Go语言开发小技巧&易错点100例(五)

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

往期回顾

本期看点技巧类用【技】表示易错点用【易】表示

1pprof查看运行时状态信息【技】

2goruntine使用后的销毁【易】

PS《Go语言开发小技巧&易错点100例》算上这一篇已经完成了20篇啦五分之一继续加油

正文如下

1 pprof查看运行时状态信息

pprof是Go语言的性能分析工具主要可以分析以下几种情况

  • allocs过去所有内存分配的示例
  • block导致同步原语阻塞的堆栈跟踪
  • cmdline当前程序的命令行调用
  • goroutine当前所有goroutine的堆栈跟踪
  • heap活动对象的内存分配示例。您可以指定gc GET参数以便在执行堆样例之前运行gc。
  • mutex竞争互斥对象持有者的堆栈跟踪
  • profileCPU profile。可以在seconds GET参数中指定持续时间。获得概要文件后使用go工具pprof命令来研究概要文件。
  • threadcreate用于创建新的操作系统线程的堆栈跟踪
  • trace当前程序执行的跟踪。可以在seconds GET参数中指定持续时间。获得跟踪文件后使用go工具trace命令来调查跟踪。

运行代码

package main

import (
   "fmt"
   "net/http"
   _ "net/http/pprof"
   "strings"
)

func main() {
   http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
      writer.Write([]byte("Hello~"))
   })     
   http.ListenAndServe(":9090", nil) 
}

访问链接http://localhost:9090/debug/pprof/

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vWPbqALX-1673792495830)(Go语言开发小技巧&易错点100例五.assets/image-20230115173923597.png)]

2 goruntine使用后的销毁

在上节我们学到了如何去控制goruntine的数量因为短时间内大量的goroutine同时执行也会造成内存崩溃、CPU占用率过高等问题不仅如此当goruntine启动后未进行关闭时因为大量的挤压goroutine也会造成相同的问题因此我们要习惯在项目中使用goruntine后进行关闭。

2.1 使用后不销毁出现的问题示例

代码示例

package main

import (
   "fmt"
   "net/http"
   _ "net/http/pprof"
)

func main() {
   ints := make(chan int,1000)
   i := 0

   worker := func (ch chan int){
      for v := range ch {
         fmt.Println(v)
      }
   }

   http.HandleFunc("/go", func(writer http.ResponseWriter, request *http.Request) {
      i+=1
      ints<-i
      go worker(ints)
   })
   http.ListenAndServe(":9090", nil)
}

查看pprof

在这里插入图片描述

我们发现同一个地方我们只go出去一次但是几次请求后还是有很多goruntine这样如果大批量请求的话一定会出问题。

2.2 使用后销毁的方式

2.2.1 优化代码逻辑

第一种优化方式就是换一种方式去监听channel

package main

import (
   "fmt"
   "net/http"
   _ "net/http/pprof"
)


func main() {
   ints := make(chan int,1000)
   i := 0

   worker := func (ch chan int){
      select {
      case v := <-ch:
         fmt.Println(v)
      default:
         return
      }
   }

   http.HandleFunc("/go", func(writer http.ResponseWriter, request *http.Request) {
      i+=1
      ints<-i
      go worker(ints)
   })
   http.ListenAndServe(":9090", nil)
}
2.2.2 利用channel销毁多余的goruntine

第二种方式我们可以close掉channel来间接的关闭监听他的goruntine

package main

import (
   "fmt"
   "net/http"
   _ "net/http/pprof"
)

func worker(v ...int) {
   count := len(v)
   res :=make(chan int,count)

   go func (ch chan int){
      for i := 0; i < count; i++ {
         fmt.Println(v[i])
         ch <- v[i]
      }
   }(res)

   for i := 0; i < count; i++ {
      <-res
   }

   close(res)
}

func main() {
   val := 0

   http.HandleFunc("/go", func(writer http.ResponseWriter, request *http.Request) {
      val += 1
      go worker(val)
   })
   http.ListenAndServe(":9090", nil)
}
2.2.3 利用Context销毁多余的goruntine

第三种方式使用Context机制关闭多余的goruntine

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: go

“Go语言开发小技巧&易错点100例(五)” 的相关文章