refactor(storage): read from local disk for putObject
This commit is contained in:
1 parent
7263d08bc5
commit
39a07252b6
10 files changed
+471
-1213
No files matched your search
@@ -229,274 +229,3 @@ func newGetObjectStream() *getObjectStream {
|
||||
}
|
||||
return stream
|
||||
}
|
||||
|
||||
// putObjectStreamRequest is used to mock a message sent from the client to the server.
|
||||
type putObjectStreamRequest struct {
|
||||
msg *plgpb.PutObjectRequest
|
||||
err error
|
||||
}
|
||||
|
||||
// puObjectStreamResponse is used to mock a message sent from the server to the client.
|
||||
type putObjectStreamResponse struct {
|
||||
msg *plgpb.PutObjectResponse
|
||||
err error
|
||||
}
|
||||
|
||||
// putObjectStream is used to mock the interactions between
|
||||
// the client and server for the PutObject method.
|
||||
type putObjectStream struct {
|
||||
client plgpb.StoragePluginService_PutObjectClient
|
||||
server plgpb.StoragePluginService_PutObjectServer
|
||||
|
||||
// requests is used to mock the client sending messages to the server.
|
||||
requests chan *putObjectStreamRequest
|
||||
|
||||
// responses is used to mock the server sending messages to the client.
|
||||
responses chan *putObjectStreamResponse
|
||||
|
||||
m *sync.Mutex
|
||||
clientClosed bool
|
||||
serverClosed bool
|
||||
}
|
||||
|
||||
func (s *putObjectStream) CloseClient() {
|
||||
s.m.Lock()
|
||||
defer s.m.Unlock()
|
||||
if s.clientClosed {
|
||||
return
|
||||
}
|
||||
close(s.requests)
|
||||
s.clientClosed = true
|
||||
}
|
||||
|
||||
func (s *putObjectStream) IsClientClosed() bool {
|
||||
s.m.Lock()
|
||||
defer s.m.Unlock()
|
||||
return s.clientClosed
|
||||
}
|
||||
|
||||
func (s *putObjectStream) CloseServer() {
|
||||
s.m.Lock()
|
||||
defer s.m.Unlock()
|
||||
if s.serverClosed {
|
||||
return
|
||||
}
|
||||
close(s.responses)
|
||||
s.serverClosed = true
|
||||
}
|
||||
|
||||
func (s *putObjectStream) IsServerClosed() bool {
|
||||
s.m.Lock()
|
||||
defer s.m.Unlock()
|
||||
return s.serverClosed
|
||||
}
|
||||
|
||||
// putObjectClient is used to mock the client stream
|
||||
// interactions for the PutObject method.
|
||||
type putObjectClient struct {
|
||||
sendToServer chan *putObjectStreamRequest
|
||||
sentFromServer chan *putObjectStreamResponse
|
||||
|
||||
// closeStream is used to close the channels of the stream.
|
||||
// This is used to prevent the channels from being closed multiple times.
|
||||
// This is needed because the channel can be closed by the client or the server.
|
||||
closeStream func()
|
||||
|
||||
// isStreamClosed is used to check if the stream is closed.
|
||||
// This is needed because the channel can be closed by the client or the server.
|
||||
isStreamClosed func() bool
|
||||
}
|
||||
|
||||
// Send will send a message to the server.
|
||||
// Send will return an error if the request is nil.
|
||||
// Send will return an error if the stream is closed.
|
||||
func (c *putObjectClient) Send(req *plgpb.PutObjectRequest) error {
|
||||
if req == nil {
|
||||
return fmt.Errorf(`parameter arg "req PutObjectRequest" cannot be nil`)
|
||||
}
|
||||
if c.isStreamClosed() {
|
||||
return fmt.Errorf("stream is closed")
|
||||
}
|
||||
c.sendToServer <- &putObjectStreamRequest{msg: req}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CloseAndRecv will return an PutObjectResponse if the server returns one.
|
||||
// CloseAndRecv will return an error if the server returns an error.
|
||||
// CloseAndRecv will return an io.EOF error if the server closes the stream.
|
||||
// CloseAndRecv will close the stream used to send messages to the server.
|
||||
func (c *putObjectClient) CloseAndRecv() (*plgpb.PutObjectResponse, error) {
|
||||
c.closeStream()
|
||||
resp, ok := <-c.sentFromServer
|
||||
if !ok {
|
||||
return nil, io.EOF
|
||||
}
|
||||
return resp.msg, resp.err
|
||||
}
|
||||
|
||||
// Header should not be used.
|
||||
// Header is implemeted to satisfy the grpc.ClientStream interface.
|
||||
// Header will always return an empty metadata and an nil error.
|
||||
func (c *putObjectClient) Header() (metadata.MD, error) {
|
||||
return make(metadata.MD), nil
|
||||
}
|
||||
|
||||
// Trailer should not be used.
|
||||
// Trailer is implemeted to satisfy the grpc.ClientStream interface.
|
||||
// Trailer will always return an empty metadata.
|
||||
func (c *putObjectClient) Trailer() metadata.MD {
|
||||
return make(metadata.MD)
|
||||
}
|
||||
|
||||
// CloseSend will close the channel used to retrieve messages from
|
||||
// the server. This will cause the CloseAndRecv method to return io.EOF.
|
||||
// CloseSend will always return a nill error.
|
||||
func (c *putObjectClient) CloseSend() error {
|
||||
if c.isStreamClosed() {
|
||||
return fmt.Errorf("stream is closed")
|
||||
}
|
||||
c.closeStream()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Context will always return a Background context.
|
||||
func (c *putObjectClient) Context() context.Context {
|
||||
return context.Background()
|
||||
}
|
||||
|
||||
// SendMsg should not be used.
|
||||
// SendMsg is implemeted to satisfy the grpc.ClientStream interface.
|
||||
// SendMsg will always return an nil error.
|
||||
func (c *putObjectClient) SendMsg(m interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RecvMsg should not be used.
|
||||
// RecvMsg is implemeted to satisfy the grpc.ClientStream interface.
|
||||
// RecvMsg will always return an nil error.
|
||||
func (c *putObjectClient) RecvMsg(m interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// putObjectServer is used to mock the server stream
|
||||
// interactions for the PutObject method.
|
||||
type putObjectServer struct {
|
||||
sentFromClient chan *putObjectStreamRequest
|
||||
sentToClient chan *putObjectStreamResponse
|
||||
|
||||
// closeStream is used to close the channels of the stream.
|
||||
// This is used to prevent the channels from being closed multiple times.
|
||||
// This is needed because the channel can be closed by the client or the server.
|
||||
closeStream func()
|
||||
|
||||
// isStreamClosed is used to check if the stream is closed.
|
||||
// This is needed because the channel can be closed by the client or the server.
|
||||
isStreamClosed func() bool
|
||||
}
|
||||
|
||||
// SendAndClose will send a PutObjectResponse to the client.
|
||||
// SendAndClose will return an error if the response is nil.
|
||||
// SendAndClose will close the stream used to send messages from the client.
|
||||
// SendAndClose will return an error if the stream is closed.
|
||||
func (s *putObjectServer) SendAndClose(resp *plgpb.PutObjectResponse) error {
|
||||
if resp == nil {
|
||||
return fmt.Errorf(`parameter arg "resp PutObjectResponse" cannot be nil`)
|
||||
}
|
||||
if s.isStreamClosed() {
|
||||
return fmt.Errorf("stream is closed")
|
||||
}
|
||||
s.sentToClient <- &putObjectStreamResponse{msg: resp}
|
||||
s.closeStream()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Recv will read a PutObjectRequest from the stream.
|
||||
// Recv will return an io.EOF error if the stream is closed.
|
||||
func (s *putObjectServer) Recv() (*plgpb.PutObjectRequest, error) {
|
||||
req, ok := <-s.sentFromClient
|
||||
if !ok {
|
||||
return nil, io.EOF
|
||||
}
|
||||
return req.msg, req.err
|
||||
}
|
||||
|
||||
// SetHeader should not be used.
|
||||
// SetHeader is implemeted to satisfy the grpc.ServerStream interface.
|
||||
// SetHeader will always return an nil error.
|
||||
func (s *putObjectServer) SetHeader(metadata.MD) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendHeader should not be used.
|
||||
// SendHeader is implemeted to satisfy the grpc.ServerStream interface.
|
||||
// SendHeader will always return an nil error.
|
||||
func (s *putObjectServer) SendHeader(metadata.MD) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetTrailer should not be used.
|
||||
// SetTrailer is implemeted to satisfy the grpc.ServerStream interface.
|
||||
func (s *putObjectServer) SetTrailer(metadata.MD) {
|
||||
}
|
||||
|
||||
// Context will always return a Background context.
|
||||
func (s *putObjectServer) Context() context.Context {
|
||||
return context.Background()
|
||||
}
|
||||
|
||||
// SendMsg allows sending errors other than io.EOF to the client.
|
||||
// Sending an error message will close the stream.
|
||||
// SendMsg allows sending PutObjectResponse messages to the client.
|
||||
// SendMsg returns an invalid argument error if the message is not
|
||||
// an error or PutObjectResponse.
|
||||
// SendMsg will return an error if the stream is closed.
|
||||
func (s *putObjectServer) SendMsg(m interface{}) error {
|
||||
switch msg := m.(type) {
|
||||
case *plgpb.PutObjectResponse:
|
||||
if s.isStreamClosed() {
|
||||
return fmt.Errorf("stream is closed")
|
||||
}
|
||||
s.sentToClient <- &putObjectStreamResponse{msg: msg}
|
||||
case error:
|
||||
if s.isStreamClosed() {
|
||||
return fmt.Errorf("stream is closed")
|
||||
}
|
||||
s.sentToClient <- &putObjectStreamResponse{err: msg}
|
||||
defer s.closeStream()
|
||||
default:
|
||||
return fmt.Errorf("invalid argument %v", m)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RecvMsg should not be used.
|
||||
// RecvMsg is implemeted to satisfy the grpc.ServerStream interface.
|
||||
// RecvMsg will always return an nil error.
|
||||
func (s *putObjectServer) RecvMsg(m interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// newPutObjectStream will create a mock stream for the PutObject method.
|
||||
// The client and server stream is mocked by creating a PutObjectRequest,
|
||||
// PutObjectResponse and an error channel that is shared between the client
|
||||
// and server.
|
||||
func newPutObjectStream() *putObjectStream {
|
||||
stream := &putObjectStream{
|
||||
m: new(sync.Mutex),
|
||||
requests: make(chan *putObjectStreamRequest),
|
||||
responses: make(chan *putObjectStreamResponse),
|
||||
}
|
||||
stream.client = &putObjectClient{
|
||||
sendToServer: stream.requests,
|
||||
sentFromServer: stream.responses,
|
||||
closeStream: stream.CloseClient,
|
||||
isStreamClosed: stream.IsClientClosed,
|
||||
}
|
||||
stream.server = &putObjectServer{
|
||||
sentFromClient: stream.requests,
|
||||
sentToClient: stream.responses,
|
||||
closeStream: stream.CloseServer,
|
||||
isStreamClosed: stream.IsServerClosed,
|
||||
}
|
||||
return stream
|
||||
}
|
||||
Reference in new issue
Block a user