]> 127.0.0.1 Git - front/.git/commitdiff
1 v0.1.20231214152456
authorqydysky <qydysky@foxmail.com>
Thu, 14 Dec 2023 15:24:36 +0000 (23:24 +0800)
committerqydysky <qydysky@foxmail.com>
Thu, 14 Dec 2023 15:24:36 +0000 (23:24 +0800)
config.go
go.mod
go.sum
main.go
main/main.json

index 8e3c3900a3a043f5a46a1a3ee592d4fb58b626f8..64fb27bbc0a9efd3915154ccacb5a55904c90c43 100644 (file)
--- a/config.go
+++ b/config.go
@@ -8,6 +8,8 @@ import (
        "net/http"
        "sync"
        "time"
+
+       pslice "github.com/qydysky/part/slice"
 )
 
 type Config struct {
@@ -18,8 +20,10 @@ type Config struct {
                Pub    string      `json:"pub"`
                Key    string      `json:"key"`
        } `json:"tls"`
-       MatchRule string  `json:"matchRule"`
-       Routes    []Route `json:"routes"`
+       MatchRule  string               `json:"matchRule"`
+       CopyBlocks int                  `json:"copyBlocks"`
+       BlocksI    pslice.BlocksI[byte] `json:"-"`
+       Routes     []Route              `json:"routes"`
 }
 
 type Route struct {
diff --git a/go.mod b/go.mod
index 53c70c7df132eeb627c863762c34ace034aabf7e..fadd0fe00d78442f1eef756af5b666991cad19bc 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -4,7 +4,7 @@ go 1.21.4
 
 require (
        github.com/gorilla/websocket v1.5.1
-       github.com/qydysky/part v0.28.20231214142916
+       github.com/qydysky/part v0.28.20231214150658
 )
 
 require (
diff --git a/go.sum b/go.sum
index 852ba969229cc1445d048cfe5f92e1a7d42dbd4c..081050ecabd15f7e5c6940666f82d989dbd1d9e8 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -27,6 +27,10 @@ github.com/qydysky/part v0.28.20231209070216 h1:1hnOxnsd4sTBTA/nF2pLhyL+0DidpwHo
 github.com/qydysky/part v0.28.20231209070216/go.mod h1:NyKyjpBCSjcHtKlC+fL5lCidm57UCnwEgufiBDs5yxA=
 github.com/qydysky/part v0.28.20231214142916 h1:pr4AyzEdcByXYlAB6yMyLebSDFH1X5fVnRqsdZkYNqU=
 github.com/qydysky/part v0.28.20231214142916/go.mod h1:NyKyjpBCSjcHtKlC+fL5lCidm57UCnwEgufiBDs5yxA=
+github.com/qydysky/part v0.28.20231214145324 h1:laylvWj1l83w3R3OkJBPd83fJutq+nYPwooPfa+aqDo=
+github.com/qydysky/part v0.28.20231214145324/go.mod h1:NyKyjpBCSjcHtKlC+fL5lCidm57UCnwEgufiBDs5yxA=
+github.com/qydysky/part v0.28.20231214150658 h1:MH+smtA+SyIRJR0bZI1dEXVF8s09wRZYWS9Kf58xQgA=
+github.com/qydysky/part v0.28.20231214150658/go.mod h1:NyKyjpBCSjcHtKlC+fL5lCidm57UCnwEgufiBDs5yxA=
 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
 github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
diff --git a/main.go b/main.go
index 4bbadc6f6e1d635a1ef1d7d0849b1c2f08996c0e..e513cf1b4db042d5bd6541b5aab3b116df8688a3 100644 (file)
--- a/main.go
+++ b/main.go
@@ -128,6 +128,13 @@ func Run(ctx context.Context, configSP *Config, logger Logger) {
                httpSer.TLSConfig = configSP.TLS.Config.Clone()
        }
 
+       if configSP.BlocksI == nil {
+               if configSP.CopyBlocks == 0 {
+                       configSP.CopyBlocks = 1000
+               }
+               configSP.BlocksI = pslice.NewBlocks[byte](16*1024, configSP.CopyBlocks)
+       }
+
        syncWeb := pweb.NewSyncMap(&httpSer, &routeP, matchfunc)
        defer syncWeb.Shutdown()
 
@@ -268,9 +275,9 @@ func applyConfig(ctx context.Context, configS *Config, routeP *pweb.WebPath, log
 
                        var e error
                        if r.Header.Get("Upgrade") == "websocket" {
-                               e = wsDealer(ctx1, w, r, path, backI, logger)
+                               e = wsDealer(ctx1, w, r, path, backI, logger, configS.BlocksI)
                        } else {
-                               e = httpDealer(ctx1, w, r, path, backI, logger)
+                               e = httpDealer(ctx1, w, r, path, backI, logger, configS.BlocksI)
                        }
                        if e != nil {
                                logger.Warn(`W:`, fmt.Sprintf("%s=>%s %v", path, backI.Name, e))
@@ -304,7 +311,7 @@ var (
        ErrHeaderCheckFail = errors.New("ErrHeaderCheckFail")
 )
 
-func httpDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, routePath string, back *Back, logger Logger) error {
+func httpDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, routePath string, back *Back, logger Logger, blocksi pslice.BlocksI[byte]) error {
        url := back.To
        if back.PathAdd {
                url += r.URL.String()
@@ -348,14 +355,20 @@ func httpDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, rou
        }
 
        defer resp.Body.Close()
-       if _, e = io.Copy(w, resp.Body); e != nil {
+       if tmpbuf, put, e := blocksi.Get(); e != nil {
                logger.Error(`E:`, fmt.Sprintf("%s=>%s %v", routePath, back.Name, e))
                return errors.Join(ErrCopy, e)
+       } else {
+               defer put()
+               if _, e = io.CopyBuffer(w, resp.Body, tmpbuf); e != nil {
+                       logger.Error(`E:`, fmt.Sprintf("%s=>%s %v", routePath, back.Name, e))
+                       return errors.Join(ErrCopy, e)
+               }
        }
        return nil
 }
 
-func wsDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, routePath string, back *Back, logger Logger) error {
+func wsDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, routePath string, back *Back, logger Logger, blocksi pslice.BlocksI[byte]) error {
        url := back.To
        if back.PathAdd {
                url += r.URL.String()
@@ -387,12 +400,12 @@ func wsDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, route
                        defer req.Close()
 
                        select {
-                       case e := <-copyWsMsg(req, res):
+                       case e := <-copyWsMsg(req, res, blocksi):
                                if e != nil {
                                        logger.Error(`E:`, fmt.Sprintf("%s=>%s s->c %v", routePath, back.Name, e))
                                        return errors.Join(ErrCopy, e)
                                }
-                       case e := <-copyWsMsg(res, req):
+                       case e := <-copyWsMsg(res, req, blocksi):
                                if e != nil {
                                        logger.Error(`E:`, fmt.Sprintf("%s=>%s c->s %v", routePath, back.Name, e))
                                        return errors.Join(ErrCopy, e)
@@ -429,16 +442,14 @@ func copyHeader(s, t http.Header, app []Header) error {
        return nil
 }
 
-var copyBuf = pslice.NewBlocks[byte](16*1024, 100)
-
-func copyWsMsg(dst io.Writer, src io.Reader) <-chan error {
+func copyWsMsg(dst io.Writer, src io.Reader, blocksi pslice.BlocksI[byte]) <-chan error {
        c := make(chan error, 1)
        go func() {
-               if tmpbuf, put, e := copyBuf.Get(); e != nil {
+               if tmpbuf, put, e := blocksi.Get(); e != nil {
                        c <- e
                } else {
+                       defer put()
                        _, e := io.CopyBuffer(dst, src, tmpbuf)
-                       put()
                        c <- e
                }
        }()
index f43088c408ba6f8d53ca6a371c29f81b0d7a6e91..093a83dc3fd55e82f78307ffda6dd36b8b3c1d90 100644 (file)
@@ -6,6 +6,7 @@
       "key": "key.pem"
     },
     "matchRule": "prefix",
+    "copyBlocks": 100,
     "routes": [
       {
         "path": "/1",