-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rpc: Use MultiplexV2 for connections
MultiplexV2 is a new connection multiplex header that supports multiplex both RPC and streaming requests over the same Yamux connection. MultiplexV2 was added in 0.8.0 as part of #3892 . So Nomad 0.11 can expect it to be supported. Though, some more rigorous testing is required before merging this. I want to call out some implementation details: First, the current connection pool reuses the Yamux stream for multiple RPC calls, and doesn't close them until an error is encountered. This commit doesn't change it, and sets the `RpcNomad` byte only at stream creation. Second, the StreamingRPC session gets closed by callers and cannot be reused. Every StreamingRPC opens a new Yamux session.
- Loading branch information
Mahmood Ali
committed
Feb 4, 2020
1 parent
61d4a44
commit 7144efe
Showing
3 changed files
with
38 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -653,52 +653,19 @@ func (r *rpcHandler) getServer(region, serverID string) (*serverParts, error) { | |
// initial handshake, returning the connection or an error. It is the callers | ||
// responsibility to close the connection if there is no returned error. | ||
func (r *rpcHandler) streamingRpc(server *serverParts, method string) (net.Conn, error) { | ||
// Try to dial the server | ||
conn, err := net.DialTimeout("tcp", server.Addr.String(), 10*time.Second) | ||
c, err := r.connPool.StreamingRPC(r.config.Region, server.Addr, server.MajorVersion) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
notnoop
Contributor
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Cast to TCPConn | ||
if tcp, ok := conn.(*net.TCPConn); ok { | ||
tcp.SetKeepAlive(true) | ||
tcp.SetNoDelay(true) | ||
} | ||
|
||
return r.streamingRpcImpl(conn, server.Region, method) | ||
return r.streamingRpcImpl(c, method) | ||
} | ||
|
||
// streamingRpcImpl takes a pre-established connection to a server and conducts | ||
// the handshake to establish a streaming RPC for the given method. If an error | ||
// is returned, the underlying connection has been closed. Otherwise it is | ||
// assumed that the connection has been hijacked by the RPC method. | ||
func (r *rpcHandler) streamingRpcImpl(conn net.Conn, region, method string) (net.Conn, error) { | ||
// Check if TLS is enabled | ||
r.tlsWrapLock.RLock() | ||
tlsWrap := r.tlsWrap | ||
r.tlsWrapLock.RUnlock() | ||
|
||
if tlsWrap != nil { | ||
// Switch the connection into TLS mode | ||
if _, err := conn.Write([]byte{byte(pool.RpcTLS)}); err != nil { | ||
conn.Close() | ||
return nil, err | ||
} | ||
|
||
// Wrap the connection in a TLS client | ||
tlsConn, err := tlsWrap(region, conn) | ||
if err != nil { | ||
conn.Close() | ||
return nil, err | ||
} | ||
conn = tlsConn | ||
} | ||
|
||
// Write the multiplex byte to set the mode | ||
if _, err := conn.Write([]byte{byte(pool.RpcStreaming)}); err != nil { | ||
conn.Close() | ||
return nil, err | ||
} | ||
func (r *rpcHandler) streamingRpcImpl(conn net.Conn, method string) (net.Conn, error) { | ||
|
||
// Send the header | ||
encoder := codec.NewEncoder(conn, structs.MsgpackHandle) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Why here
r.config.Region
, bot notserver.Region