From 2254578760c10d8d7d2471eaa1a62858dce246de Mon Sep 17 00:00:00 2001 From: Asim Date: Wed, 11 May 2016 00:01:32 +0100 Subject: [PATCH] We don't need sync.Once, we can just select --- client/rpc_client.go | 4 ---- client/rpc_stream.go | 18 +++++++++--------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/client/rpc_client.go b/client/rpc_client.go index 0bce57fb..cdf2eeca 100644 --- a/client/rpc_client.go +++ b/client/rpc_client.go @@ -75,11 +75,9 @@ func (r *rpcClient) call(ctx context.Context, address string, req Request, resp return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err)) } - var once sync.Once stream := &rpcStream{ context: ctx, request: req, - once: once, closed: make(chan bool), codec: newRpcPlusCodec(msg, c, cf), } @@ -139,11 +137,9 @@ func (r *rpcClient) stream(ctx context.Context, address string, req Request, opt return nil, errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err)) } - var once sync.Once stream := &rpcStream{ context: ctx, request: req, - once: once, closed: make(chan bool), codec: newRpcPlusCodec(msg, c, cf), } diff --git a/client/rpc_stream.go b/client/rpc_stream.go index c5937b49..2d3b3ddc 100644 --- a/client/rpc_stream.go +++ b/client/rpc_stream.go @@ -12,7 +12,6 @@ import ( type rpcStream struct { sync.RWMutex seq uint64 - once sync.Once closed chan bool err error request Request @@ -22,13 +21,11 @@ type rpcStream struct { func (r *rpcStream) isClosed() bool { select { - case _, ok := <-r.closed: - if !ok { - return true - } + case <-r.closed: + return true default: + return false } - return false } func (r *rpcStream) Context() context.Context { @@ -112,8 +109,11 @@ func (r *rpcStream) Error() error { } func (r *rpcStream) Close() error { - r.once.Do(func() { + select { + case <-r.closed: + return nil + default: close(r.closed) - }) - return r.codec.Close() + return r.codec.Close() + } }