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:
parent
07cd4de2f2
commit
bdb2ab7a81
@ -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,
|
||||
|
20
server.go
20
server.go
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user