]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Improve 连接限制移入组件
authorqydysky <qydysky@foxmail.com>
Wed, 24 May 2023 23:16:12 +0000 (07:16 +0800)
committerqydysky <qydysky@foxmail.com>
Wed, 24 May 2023 23:16:30 +0000 (07:16 +0800)
.github/workflows/go.yml
.github/workflows/test.yml
Reply/F.go
go.mod
go.sum

index 41bc95a765739d70475c32f3afc4b91dc1ac734b..78f3e5abe0b48acd703dd43c08d6a65e151a6010 100644 (file)
@@ -61,6 +61,7 @@ jobs:
       run: |
         sudo apt-get update
         sudo apt-get install libgtk-3-dev libcairo2-dev libglib2.0-dev
+        git rev-parse --short HEAD > VERSION
         cd demo
         go get .
         CGO_ENABLED=0 go build -v -buildmode=exe -o demo.run main.go
@@ -93,6 +94,7 @@ jobs:
 
     - name: Build
       run: |
+        git rev-parse --short HEAD > VERSION
         cd demo
         set CGO_ENABLED=0
         go get .
index c7a6258d147195b9df48e0985b81d3e0d63edafb..9226b8ba39681cf34972a0999cd7cc99e2ff9f50 100644 (file)
@@ -62,6 +62,7 @@ jobs:
 
     - name: Build
       run: |
+        git rev-parse --short HEAD > VERSION
         cd demo
         sudo apt-get update
         sudo apt-get install libgtk-3-dev libcairo2-dev libglib2.0-dev
@@ -94,6 +95,7 @@ jobs:
 
     - name: Build
       run: |
+        git rev-parse --short HEAD > VERSION
         cd demo
         go get .
         set CGO_ENABLED=0
index 1d18d945829dbf2c44392984b1d258dad2172cd7..e2e1607e829f81ed9b256e8e8e5f5824e8cbc232 100644 (file)
@@ -9,7 +9,6 @@ import (
        "io"
        "io/fs"
        "math"
-       "net"
        "net/http"
        "net/http/pprof"
        "sort"
@@ -37,6 +36,7 @@ import (
        limit "github.com/qydysky/part/limit"
        msgq "github.com/qydysky/part/msgq"
        psync "github.com/qydysky/part/sync"
+       pweb "github.com/qydysky/part/web"
        websocket "github.com/qydysky/part/websocket"
 
        encoder "golang.org/x/text/encoding"
@@ -1156,14 +1156,7 @@ func init() {
                }
 
                // 直播流回放连接限制
-               type limitItem struct {
-                       cidr      *net.IPNet
-                       available int
-               }
-               var (
-                       limitCon []limitItem
-                       limitL   psync.RWMutex
-               )
+               var climit pweb.CountLimits
                if limits, ok := c.C.K_v.LoadV(`直播流回放连接限制`).([]any); ok {
                        for i := 0; i < len(limits); i++ {
                                if vm, ok := limits[i].(map[string]any); ok {
@@ -1172,11 +1165,7 @@ func init() {
                                        } else if max, ok := vm["max"].(float64); !ok {
                                                continue
                                        } else {
-                                               if _, cidrx, err := net.ParseCIDR(cidr); err != nil {
-                                                       flog.L(`E: `, err)
-                                               } else {
-                                                       limitCon = append(limitCon, limitItem{cidr: cidrx, available: int(max)})
-                                               }
+                                               climit.SetMaxCount(cidr, int(max))
                                        }
                                }
                        }
@@ -1258,25 +1247,10 @@ func init() {
                // 直播流播放器
                c.C.SerF.Store(path+"player/", func(w http.ResponseWriter, r *http.Request) {
                        // 直播流回放连接限制
-                       if len(limitCon) != 0 {
-                               ip := net.ParseIP(strings.Split(r.RemoteAddr, ":")[0])
-                               var isOverflow bool
-                               ul := limitL.RLock()
-                               for i := 0; i < len(limitCon); i++ {
-                                       if !limitCon[i].cidr.Contains(ip) {
-                                               continue
-                                       }
-                                       if limitCon[i].available == 0 {
-                                               isOverflow = true
-                                               break
-                                       }
-                               }
-                               ul()
-                               if isOverflow {
-                                       w.WriteHeader(http.StatusTooManyRequests)
-                                       _, _ = w.Write([]byte("已达到设定最大连接数"))
-                                       return
-                               }
+                       if climit.ReachMax(r) {
+                               w.WriteHeader(http.StatusTooManyRequests)
+                               _, _ = w.Write([]byte("已达到设定最大连接数"))
+                               return
                        }
 
                        p := strings.TrimPrefix(r.URL.Path, path+"player/")
@@ -1302,37 +1276,9 @@ func init() {
                // 流地址
                c.C.SerF.Store(path+"stream", func(w http.ResponseWriter, r *http.Request) {
                        // 直播流回放连接限制
-                       if len(limitCon) != 0 {
-                               ip := net.ParseIP(strings.Split(r.RemoteAddr, ":")[0])
-                               var add []int
-                               ul := limitL.Lock()
-                               for i := 0; i < len(limitCon); i++ {
-                                       if !limitCon[i].cidr.Contains(ip) {
-                                               continue
-                                       }
-                                       if limitCon[i].available != 0 {
-                                               add = append(add, i)
-                                       }
-                               }
-                               if len(add) == 0 {
-                                       ul()
-                                       w.WriteHeader(http.StatusTooManyRequests)
-                                       return
-                               } else {
-                                       for i := 0; i < len(add); i++ {
-                                               limitCon[add[i]].available -= 1
-                                       }
-                                       ul()
-                                       // 连接退出
-                                       go func() {
-                                               <-r.Context().Done()
-                                               ul := limitL.Lock()
-                                               for i := 0; i < len(add); i++ {
-                                                       limitCon[add[i]].available += 1
-                                               }
-                                               ul()
-                                       }()
-                               }
+                       if climit.AddCount(r) {
+                               w.WriteHeader(http.StatusTooManyRequests)
+                               return
                        }
 
                        //header
diff --git a/go.mod b/go.mod
index 39ba9aaad6f53e52c7625f31c671c1086899fb94..e6bf7a3596ff1b85e7464c356b5fbb3b319da799 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -5,7 +5,7 @@ go 1.20
 require (
        github.com/gotk3/gotk3 v0.6.2
        github.com/mdp/qrterminal/v3 v3.0.0
-       github.com/qydysky/part v0.27.9
+       github.com/qydysky/part v0.27.12
        github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
        github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
        golang.org/x/text v0.9.0
diff --git a/go.sum b/go.sum
index 3895d2a46982f7cf2d910c56ad2fda8dff336c4c..f83e64afa4162ccbf7b6e16f23ef076ec118c7b7 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -31,8 +31,8 @@ github.com/mdp/qrterminal/v3 v3.0.0/go.mod h1:NJpfAs7OAm77Dy8EkWrtE4aq+cE6McoLXl
 github.com/miekg/dns v1.1.54 h1:5jon9mWcb0sFJGpnI99tOMhCPyJ+RPVz5b63MQG0VWI=
 github.com/miekg/dns v1.1.54/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
-github.com/qydysky/part v0.27.9 h1:1VQHeayiAie8xSjVp4ILzfHtzK1QjgDSvcWH7ddUXvc=
-github.com/qydysky/part v0.27.9/go.mod h1:IEMpGB0NBl6MklZmoenSpS5ChhaIL79JYFo6mF1UkAU=
+github.com/qydysky/part v0.27.12 h1:IGVfRuiYYhfaIwlTYZL4QqFNJw7uF0l6uQcaI4Ajnso=
+github.com/qydysky/part v0.27.12/go.mod h1:IEMpGB0NBl6MklZmoenSpS5ChhaIL79JYFo6mF1UkAU=
 github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=