From 1e0cad50b1a9afe3be83ee738f9d31153924f883 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sat, 24 Feb 2018 23:23:10 +0100 Subject: [PATCH] buffered input in kcp client --- transport/internet/kcp/dialer.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/transport/internet/kcp/dialer.go b/transport/internet/kcp/dialer.go index 44f9fd47..1c33410a 100644 --- a/transport/internet/kcp/dialer.go +++ b/transport/internet/kcp/dialer.go @@ -19,16 +19,26 @@ var ( ) func fetchInput(ctx context.Context, input io.Reader, reader PacketReader, conn *Connection) { - payload := buf.New() - defer payload.Release() - - for { - err := payload.Reset(buf.ReadFrom(input)) - if err != nil { - payload.Release() - return + cache := make(chan *buf.Buffer, 1024) + go func() { + for { + payload := buf.New() + if err := payload.Reset(buf.ReadFrom(input)); err != nil { + payload.Release() + close(cache) + return + } + select { + case cache <- payload: + default: + payload.Release() + } } + }() + + for payload := range cache { segments := reader.Read(payload.Bytes()) + payload.Release() if len(segments) > 0 { conn.Input(segments) }