2020-10-22 11:04:46 +00:00
|
|
|
package response
|
|
|
|
|
|
|
|
import (
|
2023-04-03 11:23:53 +00:00
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/util"
|
2020-10-22 11:04:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ClientMessageStreamer represents client-side message streamer
|
|
|
|
// that sets meta values to the response.
|
|
|
|
type ClientMessageStreamer struct {
|
2022-12-30 14:48:50 +00:00
|
|
|
srv *Service
|
2020-10-22 11:04:46 +00:00
|
|
|
|
|
|
|
send util.RequestMessageWriter
|
|
|
|
|
|
|
|
close util.ClientStreamCloser
|
|
|
|
}
|
|
|
|
|
2021-06-23 13:29:46 +00:00
|
|
|
// Send calls send method of internal streamer.
|
2023-04-03 11:23:53 +00:00
|
|
|
func (s *ClientMessageStreamer) Send(ctx context.Context, req any) error {
|
|
|
|
if err := s.send(ctx, req); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) could not send the request: %w", s, err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-10-22 11:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CloseAndRecv closes internal stream, receivers the response,
|
|
|
|
// sets meta values and returns the result.
|
2023-04-03 11:23:53 +00:00
|
|
|
func (s *ClientMessageStreamer) CloseAndRecv(ctx context.Context) (util.ResponseMessage, error) {
|
|
|
|
resp, err := s.close(ctx)
|
2020-10-22 11:04:46 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not close stream and receive response: %w", s, err)
|
2020-10-22 11:04:46 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 14:48:50 +00:00
|
|
|
s.srv.SetMeta(resp)
|
2020-10-22 11:04:46 +00:00
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRequestStreamer wraps stream methods and returns ClientMessageStreamer instance.
|
|
|
|
func (s *Service) CreateRequestStreamer(sender util.RequestMessageWriter, closer util.ClientStreamCloser) *ClientMessageStreamer {
|
|
|
|
return &ClientMessageStreamer{
|
2022-12-30 14:48:50 +00:00
|
|
|
srv: s,
|
2020-10-22 11:04:46 +00:00
|
|
|
send: sender,
|
|
|
|
close: closer,
|
|
|
|
}
|
|
|
|
}
|