ttrpc: use odd numbers for client initiated streams
Following the convention of http2, we now use odd stream ids for client initiated streams. This makes it easier to tell who initiates the stream. We enforce the convention on the server-side. This allows us to upgrade the protocol in the future to have server initiated streams. Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
@@ -27,6 +27,7 @@ type Client struct {
|
|||||||
func NewClient(conn net.Conn) *Client {
|
func NewClient(conn net.Conn) *Client {
|
||||||
c := &Client{
|
c := &Client{
|
||||||
codec: codec{},
|
codec: codec{},
|
||||||
|
requestID: 1,
|
||||||
channel: newChannel(conn, conn),
|
channel: newChannel(conn, conn),
|
||||||
sendRequests: make(chan sendRequest),
|
sendRequests: make(chan sendRequest),
|
||||||
recvRequests: make(chan recvRequest),
|
recvRequests: make(chan recvRequest),
|
||||||
@@ -44,7 +45,7 @@ func (c *Client) Call(ctx context.Context, service, method string, req, resp int
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
requestID := atomic.AddUint32(&c.requestID, 1)
|
requestID := atomic.AddUint32(&c.requestID, 2)
|
||||||
request := Request{
|
request := Request{
|
||||||
Service: service,
|
Service: service,
|
||||||
Method: method,
|
Method: method,
|
||||||
|
|||||||
20
server.go
20
server.go
@@ -5,6 +5,8 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/containerd/containerd/log"
|
"github.com/containerd/containerd/log"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
@@ -89,6 +91,24 @@ func (s *Server) handleConn(conn net.Conn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if mh.StreamID%2 != 1 {
|
||||||
|
// enforce odd client initiated identifiers.
|
||||||
|
select {
|
||||||
|
case responses <- response{
|
||||||
|
// even though we've had an invalid stream id, we send it
|
||||||
|
// back on the same stream id so the client knows which
|
||||||
|
// stream id was bad.
|
||||||
|
id: mh.StreamID,
|
||||||
|
resp: &Response{
|
||||||
|
Status: status.New(codes.InvalidArgument, "StreamID must be odd for client initiated streams").Proto(),
|
||||||
|
},
|
||||||
|
}:
|
||||||
|
case <-done:
|
||||||
|
}
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case requests <- request{
|
case requests <- request{
|
||||||
id: mh.StreamID,
|
id: mh.StreamID,
|
||||||
|
|||||||
Reference in New Issue
Block a user