mirror of
				https://github.com/minio/minio.git
				synced 2025-10-31 08:11:19 +01:00 
			
		
		
		
	This PR adds a WebSocket grid feature that allows servers to communicate via 
a single two-way connection.
There are two request types:
* Single requests, which are `[]byte => ([]byte, error)`. This is for efficient small
  roundtrips with small payloads.
* Streaming requests which are `[]byte, chan []byte => chan []byte (and error)`,
  which allows for different combinations of full two-way streams with an initial payload.
Only a single stream is created between two machines - and there is, as such, no
server/client relation since both sides can initiate and handle requests. Which server
initiates the request is decided deterministically on the server names.
Requests are made through a mux client and server, which handles message
passing, congestion, cancelation, timeouts, etc.
If a connection is lost, all requests are canceled, and the calling server will try
to reconnect. Registered handlers can operate directly on byte 
slices or use a higher-level generics abstraction.
There is no versioning of handlers/clients, and incompatible changes should
be handled by adding new handlers.
The request path can be changed to a new one for any protocol changes.
First, all servers create a "Manager." The manager must know its address 
as well as all remote addresses. This will manage all connections.
To get a connection to any remote, ask the manager to provide it given
the remote address using.
```
func (m *Manager) Connection(host string) *Connection
```
All serverside handlers must also be registered on the manager. This will
make sure that all incoming requests are served. The number of in-flight 
requests and responses must also be given for streaming requests.
The "Connection" returned manages the mux-clients. Requests issued
to the connection will be sent to the remote.
* `func (c *Connection) Request(ctx context.Context, h HandlerID, req []byte) ([]byte, error)`
   performs a single request and returns the result. Any deadline provided on the request is
   forwarded to the server, and canceling the context will make the function return at once.
* `func (c *Connection) NewStream(ctx context.Context, h HandlerID, payload []byte) (st *Stream, err error)`
   will initiate a remote call and send the initial payload.
```Go
// A Stream is a two-way stream.
// All responses *must* be read by the caller.
// If the call is canceled through the context,
//The appropriate error will be returned.
type Stream struct {
	// Responses from the remote server.
	// Channel will be closed after an error or when the remote closes.
	// All responses *must* be read by the caller until either an error is returned or the channel is closed.
	// Canceling the context will cause the context cancellation error to be returned.
	Responses <-chan Response
	// Requests sent to the server.
	// If the handler is defined with 0 incoming capacity this will be nil.
	// Channel *must* be closed to signal the end of the stream.
	// If the request context is canceled, the stream will no longer process requests.
	Requests chan<- []byte
}
type Response struct {
	Msg []byte
	Err error
}
```
There are generic versions of the server/client handlers that allow the use of type
safe implementations for data types that support msgpack marshal/unmarshal.
		
	
			
		
			
				
	
	
		
			286 lines
		
	
	
		
			7.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			286 lines
		
	
	
		
			7.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package cmd
 | |
| 
 | |
| // Code generated by github.com/tinylib/msgp DO NOT EDIT.
 | |
| 
 | |
| import (
 | |
| 	"github.com/tinylib/msgp/msgp"
 | |
| )
 | |
| 
 | |
| // DecodeMsg implements msgp.Decodable
 | |
| func (z *WalkDirOptions) DecodeMsg(dc *msgp.Reader) (err error) {
 | |
| 	var field []byte
 | |
| 	_ = field
 | |
| 	var zb0001 uint32
 | |
| 	zb0001, err = dc.ReadMapHeader()
 | |
| 	if err != nil {
 | |
| 		err = msgp.WrapError(err)
 | |
| 		return
 | |
| 	}
 | |
| 	for zb0001 > 0 {
 | |
| 		zb0001--
 | |
| 		field, err = dc.ReadMapKeyPtr()
 | |
| 		if err != nil {
 | |
| 			err = msgp.WrapError(err)
 | |
| 			return
 | |
| 		}
 | |
| 		switch msgp.UnsafeString(field) {
 | |
| 		case "Bucket":
 | |
| 			z.Bucket, err = dc.ReadString()
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "Bucket")
 | |
| 				return
 | |
| 			}
 | |
| 		case "BaseDir":
 | |
| 			z.BaseDir, err = dc.ReadString()
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "BaseDir")
 | |
| 				return
 | |
| 			}
 | |
| 		case "Recursive":
 | |
| 			z.Recursive, err = dc.ReadBool()
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "Recursive")
 | |
| 				return
 | |
| 			}
 | |
| 		case "ReportNotFound":
 | |
| 			z.ReportNotFound, err = dc.ReadBool()
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "ReportNotFound")
 | |
| 				return
 | |
| 			}
 | |
| 		case "FilterPrefix":
 | |
| 			z.FilterPrefix, err = dc.ReadString()
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "FilterPrefix")
 | |
| 				return
 | |
| 			}
 | |
| 		case "ForwardTo":
 | |
| 			z.ForwardTo, err = dc.ReadString()
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "ForwardTo")
 | |
| 				return
 | |
| 			}
 | |
| 		case "Limit":
 | |
| 			z.Limit, err = dc.ReadInt()
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "Limit")
 | |
| 				return
 | |
| 			}
 | |
| 		case "DiskID":
 | |
| 			z.DiskID, err = dc.ReadString()
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "DiskID")
 | |
| 				return
 | |
| 			}
 | |
| 		default:
 | |
| 			err = dc.Skip()
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err)
 | |
| 				return
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 	return
 | |
| }
 | |
| 
 | |
| // EncodeMsg implements msgp.Encodable
 | |
| func (z *WalkDirOptions) EncodeMsg(en *msgp.Writer) (err error) {
 | |
| 	// map header, size 8
 | |
| 	// write "Bucket"
 | |
| 	err = en.Append(0x88, 0xa6, 0x42, 0x75, 0x63, 0x6b, 0x65, 0x74)
 | |
| 	if err != nil {
 | |
| 		return
 | |
| 	}
 | |
| 	err = en.WriteString(z.Bucket)
 | |
| 	if err != nil {
 | |
| 		err = msgp.WrapError(err, "Bucket")
 | |
| 		return
 | |
| 	}
 | |
| 	// write "BaseDir"
 | |
| 	err = en.Append(0xa7, 0x42, 0x61, 0x73, 0x65, 0x44, 0x69, 0x72)
 | |
| 	if err != nil {
 | |
| 		return
 | |
| 	}
 | |
| 	err = en.WriteString(z.BaseDir)
 | |
| 	if err != nil {
 | |
| 		err = msgp.WrapError(err, "BaseDir")
 | |
| 		return
 | |
| 	}
 | |
| 	// write "Recursive"
 | |
| 	err = en.Append(0xa9, 0x52, 0x65, 0x63, 0x75, 0x72, 0x73, 0x69, 0x76, 0x65)
 | |
| 	if err != nil {
 | |
| 		return
 | |
| 	}
 | |
| 	err = en.WriteBool(z.Recursive)
 | |
| 	if err != nil {
 | |
| 		err = msgp.WrapError(err, "Recursive")
 | |
| 		return
 | |
| 	}
 | |
| 	// write "ReportNotFound"
 | |
| 	err = en.Append(0xae, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x4e, 0x6f, 0x74, 0x46, 0x6f, 0x75, 0x6e, 0x64)
 | |
| 	if err != nil {
 | |
| 		return
 | |
| 	}
 | |
| 	err = en.WriteBool(z.ReportNotFound)
 | |
| 	if err != nil {
 | |
| 		err = msgp.WrapError(err, "ReportNotFound")
 | |
| 		return
 | |
| 	}
 | |
| 	// write "FilterPrefix"
 | |
| 	err = en.Append(0xac, 0x46, 0x69, 0x6c, 0x74, 0x65, 0x72, 0x50, 0x72, 0x65, 0x66, 0x69, 0x78)
 | |
| 	if err != nil {
 | |
| 		return
 | |
| 	}
 | |
| 	err = en.WriteString(z.FilterPrefix)
 | |
| 	if err != nil {
 | |
| 		err = msgp.WrapError(err, "FilterPrefix")
 | |
| 		return
 | |
| 	}
 | |
| 	// write "ForwardTo"
 | |
| 	err = en.Append(0xa9, 0x46, 0x6f, 0x72, 0x77, 0x61, 0x72, 0x64, 0x54, 0x6f)
 | |
| 	if err != nil {
 | |
| 		return
 | |
| 	}
 | |
| 	err = en.WriteString(z.ForwardTo)
 | |
| 	if err != nil {
 | |
| 		err = msgp.WrapError(err, "ForwardTo")
 | |
| 		return
 | |
| 	}
 | |
| 	// write "Limit"
 | |
| 	err = en.Append(0xa5, 0x4c, 0x69, 0x6d, 0x69, 0x74)
 | |
| 	if err != nil {
 | |
| 		return
 | |
| 	}
 | |
| 	err = en.WriteInt(z.Limit)
 | |
| 	if err != nil {
 | |
| 		err = msgp.WrapError(err, "Limit")
 | |
| 		return
 | |
| 	}
 | |
| 	// write "DiskID"
 | |
| 	err = en.Append(0xa6, 0x44, 0x69, 0x73, 0x6b, 0x49, 0x44)
 | |
| 	if err != nil {
 | |
| 		return
 | |
| 	}
 | |
| 	err = en.WriteString(z.DiskID)
 | |
| 	if err != nil {
 | |
| 		err = msgp.WrapError(err, "DiskID")
 | |
| 		return
 | |
| 	}
 | |
| 	return
 | |
| }
 | |
| 
 | |
| // MarshalMsg implements msgp.Marshaler
 | |
| func (z *WalkDirOptions) MarshalMsg(b []byte) (o []byte, err error) {
 | |
| 	o = msgp.Require(b, z.Msgsize())
 | |
| 	// map header, size 8
 | |
| 	// string "Bucket"
 | |
| 	o = append(o, 0x88, 0xa6, 0x42, 0x75, 0x63, 0x6b, 0x65, 0x74)
 | |
| 	o = msgp.AppendString(o, z.Bucket)
 | |
| 	// string "BaseDir"
 | |
| 	o = append(o, 0xa7, 0x42, 0x61, 0x73, 0x65, 0x44, 0x69, 0x72)
 | |
| 	o = msgp.AppendString(o, z.BaseDir)
 | |
| 	// string "Recursive"
 | |
| 	o = append(o, 0xa9, 0x52, 0x65, 0x63, 0x75, 0x72, 0x73, 0x69, 0x76, 0x65)
 | |
| 	o = msgp.AppendBool(o, z.Recursive)
 | |
| 	// string "ReportNotFound"
 | |
| 	o = append(o, 0xae, 0x52, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x4e, 0x6f, 0x74, 0x46, 0x6f, 0x75, 0x6e, 0x64)
 | |
| 	o = msgp.AppendBool(o, z.ReportNotFound)
 | |
| 	// string "FilterPrefix"
 | |
| 	o = append(o, 0xac, 0x46, 0x69, 0x6c, 0x74, 0x65, 0x72, 0x50, 0x72, 0x65, 0x66, 0x69, 0x78)
 | |
| 	o = msgp.AppendString(o, z.FilterPrefix)
 | |
| 	// string "ForwardTo"
 | |
| 	o = append(o, 0xa9, 0x46, 0x6f, 0x72, 0x77, 0x61, 0x72, 0x64, 0x54, 0x6f)
 | |
| 	o = msgp.AppendString(o, z.ForwardTo)
 | |
| 	// string "Limit"
 | |
| 	o = append(o, 0xa5, 0x4c, 0x69, 0x6d, 0x69, 0x74)
 | |
| 	o = msgp.AppendInt(o, z.Limit)
 | |
| 	// string "DiskID"
 | |
| 	o = append(o, 0xa6, 0x44, 0x69, 0x73, 0x6b, 0x49, 0x44)
 | |
| 	o = msgp.AppendString(o, z.DiskID)
 | |
| 	return
 | |
| }
 | |
| 
 | |
| // UnmarshalMsg implements msgp.Unmarshaler
 | |
| func (z *WalkDirOptions) UnmarshalMsg(bts []byte) (o []byte, err error) {
 | |
| 	var field []byte
 | |
| 	_ = field
 | |
| 	var zb0001 uint32
 | |
| 	zb0001, bts, err = msgp.ReadMapHeaderBytes(bts)
 | |
| 	if err != nil {
 | |
| 		err = msgp.WrapError(err)
 | |
| 		return
 | |
| 	}
 | |
| 	for zb0001 > 0 {
 | |
| 		zb0001--
 | |
| 		field, bts, err = msgp.ReadMapKeyZC(bts)
 | |
| 		if err != nil {
 | |
| 			err = msgp.WrapError(err)
 | |
| 			return
 | |
| 		}
 | |
| 		switch msgp.UnsafeString(field) {
 | |
| 		case "Bucket":
 | |
| 			z.Bucket, bts, err = msgp.ReadStringBytes(bts)
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "Bucket")
 | |
| 				return
 | |
| 			}
 | |
| 		case "BaseDir":
 | |
| 			z.BaseDir, bts, err = msgp.ReadStringBytes(bts)
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "BaseDir")
 | |
| 				return
 | |
| 			}
 | |
| 		case "Recursive":
 | |
| 			z.Recursive, bts, err = msgp.ReadBoolBytes(bts)
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "Recursive")
 | |
| 				return
 | |
| 			}
 | |
| 		case "ReportNotFound":
 | |
| 			z.ReportNotFound, bts, err = msgp.ReadBoolBytes(bts)
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "ReportNotFound")
 | |
| 				return
 | |
| 			}
 | |
| 		case "FilterPrefix":
 | |
| 			z.FilterPrefix, bts, err = msgp.ReadStringBytes(bts)
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "FilterPrefix")
 | |
| 				return
 | |
| 			}
 | |
| 		case "ForwardTo":
 | |
| 			z.ForwardTo, bts, err = msgp.ReadStringBytes(bts)
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "ForwardTo")
 | |
| 				return
 | |
| 			}
 | |
| 		case "Limit":
 | |
| 			z.Limit, bts, err = msgp.ReadIntBytes(bts)
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "Limit")
 | |
| 				return
 | |
| 			}
 | |
| 		case "DiskID":
 | |
| 			z.DiskID, bts, err = msgp.ReadStringBytes(bts)
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err, "DiskID")
 | |
| 				return
 | |
| 			}
 | |
| 		default:
 | |
| 			bts, err = msgp.Skip(bts)
 | |
| 			if err != nil {
 | |
| 				err = msgp.WrapError(err)
 | |
| 				return
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 	o = bts
 | |
| 	return
 | |
| }
 | |
| 
 | |
| // Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
 | |
| func (z *WalkDirOptions) Msgsize() (s int) {
 | |
| 	s = 1 + 7 + msgp.StringPrefixSize + len(z.Bucket) + 8 + msgp.StringPrefixSize + len(z.BaseDir) + 10 + msgp.BoolSize + 15 + msgp.BoolSize + 13 + msgp.StringPrefixSize + len(z.FilterPrefix) + 10 + msgp.StringPrefixSize + len(z.ForwardTo) + 6 + msgp.IntSize + 7 + msgp.StringPrefixSize + len(z.DiskID)
 | |
| 	return
 | |
| }
 |