We don't need sync.Once, we can just select
This commit is contained in:
parent
a0a4ab4c15
commit
2254578760
@ -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))
|
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
var once sync.Once
|
|
||||||
stream := &rpcStream{
|
stream := &rpcStream{
|
||||||
context: ctx,
|
context: ctx,
|
||||||
request: req,
|
request: req,
|
||||||
once: once,
|
|
||||||
closed: make(chan bool),
|
closed: make(chan bool),
|
||||||
codec: newRpcPlusCodec(msg, c, cf),
|
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))
|
return nil, errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
var once sync.Once
|
|
||||||
stream := &rpcStream{
|
stream := &rpcStream{
|
||||||
context: ctx,
|
context: ctx,
|
||||||
request: req,
|
request: req,
|
||||||
once: once,
|
|
||||||
closed: make(chan bool),
|
closed: make(chan bool),
|
||||||
codec: newRpcPlusCodec(msg, c, cf),
|
codec: newRpcPlusCodec(msg, c, cf),
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
type rpcStream struct {
|
type rpcStream struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
seq uint64
|
seq uint64
|
||||||
once sync.Once
|
|
||||||
closed chan bool
|
closed chan bool
|
||||||
err error
|
err error
|
||||||
request Request
|
request Request
|
||||||
@ -22,13 +21,11 @@ type rpcStream struct {
|
|||||||
|
|
||||||
func (r *rpcStream) isClosed() bool {
|
func (r *rpcStream) isClosed() bool {
|
||||||
select {
|
select {
|
||||||
case _, ok := <-r.closed:
|
case <-r.closed:
|
||||||
if !ok {
|
|
||||||
return true
|
return true
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
}
|
|
||||||
return false
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *rpcStream) Context() context.Context {
|
func (r *rpcStream) Context() context.Context {
|
||||||
@ -112,8 +109,11 @@ func (r *rpcStream) Error() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *rpcStream) Close() error {
|
func (r *rpcStream) Close() error {
|
||||||
r.once.Do(func() {
|
select {
|
||||||
|
case <-r.closed:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
close(r.closed)
|
close(r.closed)
|
||||||
})
|
|
||||||
return r.codec.Close()
|
return r.codec.Close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user