From f23608f4964fb9f5b14935b8ff26fa8d7ae19ae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=80=9D=E6=95=8F?= Date: Thu, 1 Sep 2022 15:05:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0TAP=E6=B5=8B=E8=AF=95(TODO=20=E8=A7=A3?= =?UTF-8?q?=E6=9E=90arp)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rpc_client.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/rpc_client.go b/rpc_client.go index b6bdbb5..975f979 100644 --- a/rpc_client.go +++ b/rpc_client.go @@ -63,12 +63,13 @@ func (cli *RPCClient) CheckConnect() { } func (cli *RPCClient) Push(frame []byte) { - cli.lock.Lock() - defer cli.lock.Unlock() - cli.Frames = append(cli.Frames, frame) - if len(cli.Frames) >= 1000 { - cli.trigger <- true - } + cli.Lock(func() bool { + cli.Frames = append(cli.Frames, frame) + if len(cli.Frames) >= 1000 { + cli.trigger <- true + } + return true + }) } func (cli *RPCClient) run() { @@ -97,13 +98,12 @@ func (cli *RPCClient) run() { var ticker = time.NewTicker(time.Millisecond * 20) buf := bytes.NewBuffer(nil) - for { select { case <-cli.trigger: case <-ticker.C: } - buf.Reset() + cli.Lock(func() bool { Compress(buf, cli.Frames) cli.Frames = cli.Frames[:0] @@ -114,10 +114,10 @@ func (cli *RPCClient) run() { err = stream.Send(&gen.RequestFrames{ Frames: buf.Bytes(), }) - if err != nil { log.Panic(err) } + buf.Reset() } }