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:
Stephen J Day 2017-11-27 18:05:12 -08:00
parent 07cd4de2f2
commit bdb2ab7a81
No known key found for this signature in database
GPG Key ID: 67B3DED84EDC823F
2 changed files with 22 additions and 1 deletions

View File

@ -27,6 +27,7 @@ type Client struct {
func NewClient(conn net.Conn) *Client {
c := &Client{
codec: codec{},
requestID: 1,
channel: newChannel(conn, conn),
sendRequests: make(chan sendRequest),
recvRequests: make(chan recvRequest),
@ -44,7 +45,7 @@ func (c *Client) Call(ctx context.Context, service, method string, req, resp int
return err
}
requestID := atomic.AddUint32(&c.requestID, 1)
requestID := atomic.AddUint32(&c.requestID, 2)
request := Request{
Service: service,
Method: method,

View File

@ -5,6 +5,8 @@ import (
"net"
"github.com/containerd/containerd/log"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type Server struct {
@ -89,6 +91,24 @@ func (s *Server) handleConn(conn net.Conn) {
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 {
case requests <- request{
id: mh.StreamID,