]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20240811143447
authorqydysky <qydysky@foxmail.com>
Sun, 11 Aug 2024 14:29:23 +0000 (22:29 +0800)
committerqydysky <qydysky@foxmail.com>
Sun, 11 Aug 2024 14:29:23 +0000 (22:29 +0800)
io/io.go
websocket/Client.go

index fb50b83915419c6cd8f0f429e4c54567765baddd..5ef0e7eff8824ee486133bc9f12a2435813c46af 100644 (file)
--- a/io/io.go
+++ b/io/io.go
@@ -418,6 +418,10 @@ func Copy(r io.Reader, w io.Writer, c CopyConfig) (e error) {
 func ReadAll(r io.Reader, b []byte) ([]byte, error) {
        b = b[:0]
        for {
+               if len(b) == cap(b) {
+                       // Add more capacity (let append pick how much).
+                       b = append(b, 0)[:len(b)]
+               }
                n, err := r.Read(b[len(b):cap(b)])
                b = b[:len(b)+n]
                if err != nil {
@@ -426,10 +430,5 @@ func ReadAll(r io.Reader, b []byte) ([]byte, error) {
                        }
                        return b, err
                }
-
-               if len(b) == cap(b) {
-                       // Add more capacity (let append pick how much).
-                       b = append(b, 0)[:len(b)]
-               }
        }
 }
index eec5ece5cf7409bad6010b0a59456cc31956d583..3608ca4d282d29b5bf3e14107d56710f6a36fa5e 100644 (file)
@@ -20,11 +20,12 @@ type Client struct {
        // rec send close
        msg *msgq.MsgType[*WsMsg]
 
-       TO     int // depercated: use RTOMs WTOMs instead
-       RTOMs  int
-       WTOMs  int
-       Header map[string]string
-       Proxy  string
+       TO      int // depercated: use RTOMs WTOMs instead
+       RTOMs   int
+       WTOMs   int
+       BufSize int // msg buf 1: always use single buf >1: use bufs cycle
+       Header  map[string]string
+       Proxy   string
 
        Ping  Ping
        pingT int64
@@ -62,6 +63,9 @@ func New_client(config *Client) (*Client, error) {
        if tmp.Url == "" {
                return nil, errors.New(`url == ""`)
        }
+       if v := config.BufSize; v <= 1 {
+               tmp.BufSize = 1
+       }
        if v := config.TO; v != 0 {
                tmp.RTOMs = v
                tmp.WTOMs = v
@@ -148,7 +152,10 @@ func (o *Client) Handle() (*msgq.MsgType[*WsMsg], error) {
                }()
 
                buf := make([]byte, humanize.KByte)
-               var message []byte
+               var (
+                       msgs = make([][]byte, o.BufSize)
+                       cuP  = 0
+               )
                for {
                        if err := c.SetReadDeadline(time.Now().Add(time.Duration(o.RTOMs * int(time.Millisecond)))); err != nil {
                                o.error(err)
@@ -156,7 +163,15 @@ func (o *Client) Handle() (*msgq.MsgType[*WsMsg], error) {
                        }
                        msg_type, r, err := c.NextReader()
                        if err == nil {
-                               message, err = pio.ReadAll(r, buf)
+                               if msg, e := pio.ReadAll(r, buf); e != nil {
+                                       err = e
+                               } else {
+                                       msgs[cuP] = append(msgs[cuP][:0], msg...)
+                                       cuP++
+                                       if cuP >= o.BufSize {
+                                               cuP = 0
+                                       }
+                               }
                        }
                        if err != nil {
                                if e, ok := err.(*websocket.CloseError); ok {
@@ -179,7 +194,7 @@ func (o *Client) Handle() (*msgq.MsgType[*WsMsg], error) {
                        case websocket.PingMessage:
                                o.msg.PushLock_tag(`send`, &WsMsg{
                                        Type: websocket.PongMessage,
-                                       Msg:  message,
+                                       Msg:  msgs[cuP],
                                })
                        case websocket.PongMessage:
                                o.pingT = time.Now().UnixMilli()
@@ -193,7 +208,7 @@ func (o *Client) Handle() (*msgq.MsgType[*WsMsg], error) {
                        default:
                                o.msg.PushLock_tag(`rec`, &WsMsg{
                                        Type: websocket.TextMessage,
-                                       Msg:  message,
+                                       Msg:  msgs[cuP],
                                })
                        }
                }