forked from TrueCloudLab/frostfs-node
Evgenii Stratonikov
167a67f0b8
There is no need in a wrapper with many from-`interface{}` conversions. Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package response
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/util"
|
|
)
|
|
|
|
// ClientMessageStreamer represents client-side message streamer
|
|
// that sets meta values to the response.
|
|
type ClientMessageStreamer struct {
|
|
srv *Service
|
|
|
|
send util.RequestMessageWriter
|
|
|
|
close util.ClientStreamCloser
|
|
}
|
|
|
|
// Send calls send method of internal streamer.
|
|
func (s *ClientMessageStreamer) Send(ctx context.Context, req any) error {
|
|
if err := s.send(ctx, req); err != nil {
|
|
return fmt.Errorf("(%T) could not send the request: %w", s, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CloseAndRecv closes internal stream, receivers the response,
|
|
// sets meta values and returns the result.
|
|
func (s *ClientMessageStreamer) CloseAndRecv(ctx context.Context) (util.ResponseMessage, error) {
|
|
resp, err := s.close(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("(%T) could not close stream and receive response: %w", s, err)
|
|
}
|
|
|
|
s.srv.SetMeta(resp)
|
|
|
|
return resp, nil
|
|
}
|
|
|
|
// CreateRequestStreamer wraps stream methods and returns ClientMessageStreamer instance.
|
|
func (s *Service) CreateRequestStreamer(sender util.RequestMessageWriter, closer util.ClientStreamCloser) *ClientMessageStreamer {
|
|
return &ClientMessageStreamer{
|
|
srv: s,
|
|
send: sender,
|
|
close: closer,
|
|
}
|
|
}
|