From 74e5dbb6e5cde8e94e22159e64d885b26c351fdd Mon Sep 17 00:00:00 2001 From: qydysky Date: Thu, 14 Dec 2023 23:24:36 +0800 Subject: [PATCH] 1 --- config.go | 8 ++++++-- go.mod | 2 +- go.sum | 4 ++++ main.go | 35 +++++++++++++++++++++++------------ main/main.json | 1 + 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/config.go b/config.go index 8e3c390..64fb27b 100644 --- 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 53c70c7..fadd0fe 100644 --- 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 852ba96..081050e 100644 --- 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 4bbadc6..e513cf1 100644 --- 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 } }() diff --git a/main/main.json b/main/main.json index f43088c..093a83d 100644 --- a/main/main.json +++ b/main/main.json @@ -6,6 +6,7 @@ "key": "key.pem" }, "matchRule": "prefix", + "copyBlocks": 100, "routes": [ { "path": "/1", -- 2.39.2