forked from TrueCloudLab/frostfs-api-go
[#2] rpc/client: Remove additional wrapper
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
513e3e137d
commit
cc8da15242
12 changed files with 82 additions and 268 deletions
58
rpc/client/stream_wrapper.go
Normal file
58
rpc/client/stream_wrapper.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/TrueCloudLab/frostfs-api-go/v2/rpc/message"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type streamWrapper struct {
|
||||
grpc.ClientStream
|
||||
timeout time.Duration
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (w streamWrapper) ReadMessage(m message.Message) error {
|
||||
// Can be optimized: we can create blank message here.
|
||||
gm := m.ToGRPCMessage()
|
||||
|
||||
err := w.withTimeout(func() error {
|
||||
return w.ClientStream.RecvMsg(gm)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return m.FromGRPCMessage(gm)
|
||||
}
|
||||
|
||||
func (w streamWrapper) WriteMessage(m message.Message) error {
|
||||
return w.withTimeout(func() error {
|
||||
return w.ClientStream.SendMsg(m.ToGRPCMessage())
|
||||
})
|
||||
}
|
||||
|
||||
func (w *streamWrapper) Close() error {
|
||||
return w.withTimeout(w.ClientStream.CloseSend)
|
||||
}
|
||||
|
||||
func (w *streamWrapper) withTimeout(closure func() error) error {
|
||||
ch := make(chan error, 1)
|
||||
go func() {
|
||||
ch <- closure()
|
||||
close(ch)
|
||||
}()
|
||||
|
||||
tt := time.NewTimer(w.timeout)
|
||||
|
||||
select {
|
||||
case err := <-ch:
|
||||
tt.Stop()
|
||||
return err
|
||||
case <-tt.C:
|
||||
w.cancel()
|
||||
return context.DeadlineExceeded
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue