Skip to content

Commit 044aadd

Browse files
authored
add ProtocolVersion to ReattachConfig (#171)
* add ProtocolVersion to ReattachConfig A client connecting to a plugin process needs to know the resolved ProtocolVersion, so this PR adds that information to the returned reattach config. Prior to this, client.NegotiatedVersion() always returned 0. * Add a test for correct protocol version in reattach config
1 parent 0c19a13 commit 044aadd

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

client.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,10 @@ type ClientConfig struct {
209209
// already-running plugin process. You can retrieve this information by
210210
// calling ReattachConfig on Client.
211211
type ReattachConfig struct {
212-
Protocol Protocol
213-
Addr net.Addr
214-
Pid int
212+
Protocol Protocol
213+
ProtocolVersion int
214+
Addr net.Addr
215+
Pid int
215216

216217
// Test is set to true if this is reattaching to to a plugin in "test mode"
217218
// (see ServeConfig.Test). In this mode, client.Kill will NOT kill the
@@ -839,6 +840,10 @@ func (c *Client) reattach() (net.Addr, error) {
839840
c.protocol = ProtocolNetRPC
840841
}
841842

843+
if c.config.Reattach.Test {
844+
c.negotiatedVersion = c.config.Reattach.ProtocolVersion
845+
}
846+
842847
// If we're in test mode, we do NOT set the process. This avoids the
843848
// process being killed (the only purpose we have for c.process), since
844849
// in test mode the process is responsible for exiting on its own.

server.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,11 @@ func Serve(opts *ServeConfig) {
415415
// quite ready if they connect immediately but the client should
416416
// retry a few times.
417417
ch <- &ReattachConfig{
418-
Protocol: protoType,
419-
Addr: listener.Addr(),
420-
Pid: os.Getpid(),
421-
Test: true,
418+
Protocol: protoType,
419+
ProtocolVersion: protoVersion,
420+
Addr: listener.Addr(),
421+
Pid: os.Getpid(),
422+
Test: true,
422423
}
423424
}
424425

server_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ func TestServer_testMode(t *testing.T) {
4242
t.Fatal("config should not be nil")
4343
}
4444

45+
// Check that the reattach config includes the negotiated protocol version
46+
if config.ProtocolVersion != int(testHandshake.ProtocolVersion) {
47+
t.Fatalf("wrong protocol version in reattach config. got %d, expected %d", config.ProtocolVersion, testHandshake.ProtocolVersion)
48+
}
49+
4550
// Connect!
4651
c := NewClient(&ClientConfig{
4752
Cmd: nil,

0 commit comments

Comments
 (0)