Skip to content

Commit a3c2775

Browse files
committed
Pass ProtocolInfo to Init
To allow servers to handle protocol changes.
1 parent 914e6c8 commit a3c2775

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

client_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ func TestStartFailed(t *testing.T) {
6464
c.Assert(client.Close(), qt.IsNil)
6565
}
6666

67+
const clientVersion = 3
68+
6769
func newTestClientForServer(t testing.TB, server string, codec codecs.Codec, cfg model.ExampleConfig, env ...string) *execrpc.Client[model.ExampleConfig, model.ExampleRequest, model.ExampleMessage, model.ExampleReceipt] {
6870
client, err := execrpc.StartClient(
6971
execrpc.ClientOptions[model.ExampleConfig, model.ExampleRequest, model.ExampleMessage, model.ExampleReceipt]{
7072
ClientRawOptions: execrpc.ClientRawOptions{
71-
Version: 1,
73+
Version: clientVersion,
7274
Cmd: "go",
7375
Dir: "./examples/servers/" + server,
7476
Args: []string{"run", "."},
@@ -224,7 +226,7 @@ func TestTyped(t *testing.T) {
224226
client, err := execrpc.StartClient(
225227
execrpc.ClientOptions[model.ExampleConfig, model.ExampleRequest, model.ExampleMessage, model.ExampleReceipt]{
226228
ClientRawOptions: execrpc.ClientRawOptions{
227-
Version: 1,
229+
Version: clientVersion,
228230
Cmd: "go",
229231
Dir: "./examples/servers/typed",
230232
Args: []string{"run", "."},

examples/servers/readmeexample/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"hash"
56
"hash/fnv"
67
"log"
@@ -29,7 +30,10 @@ func main() {
2930
// Optional function to initialize the server
3031
// with the client configuration.
3132
// This will be called once on server start.
32-
Init: func(cfg model.ExampleConfig) error {
33+
Init: func(cfg model.ExampleConfig, procol execrpc.ProtocolInfo) error {
34+
if procol.Version != 3 {
35+
return fmt.Errorf("unsupported protocol version: %d", procol.Version)
36+
}
3337
clientConfig = cfg
3438
return clientConfig.Init()
3539
},

examples/servers/typed/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ func main() {
4343
execrpc.ServerOptions[model.ExampleConfig, model.ExampleRequest, model.ExampleMessage, model.ExampleReceipt]{
4444
GetHasher: getHasher,
4545
DelayDelivery: delayDelivery,
46-
Init: func(cfg model.ExampleConfig) error {
46+
Init: func(cfg model.ExampleConfig, protocol execrpc.ProtocolInfo) error {
47+
if protocol.Version != 3 {
48+
return fmt.Errorf("unsupported protocol version: %d", protocol.Version)
49+
}
4750
clientConfig = cfg
4851
return clientConfig.Init()
4952
},

server.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,18 @@ func NewServer[C, Q, M, R any](opts ServerOptions[C, Q, M, R]) (*Server[C, Q, M,
7676
return nil
7777
}
7878

79-
var cfg C
79+
var (
80+
cfg C
81+
protocolInfo = ProtocolInfo{Version: message.Header.Version}
82+
)
8083
err := opts.Codec.Decode(message.Body, &cfg)
8184
if err != nil {
8285
m := createErrorMessage(err, message.Header, MessageStatusErrDecodeFailed)
8386
d.SendMessage(m)
8487
return nil
8588
}
8689

87-
if err := opts.Init(cfg); err != nil {
90+
if err := opts.Init(cfg, protocolInfo); err != nil {
8891
m := createErrorMessage(err, message.Header, MessageStatusErrInitServerFailed)
8992
d.SendMessage(m)
9093
return nil
@@ -259,12 +262,20 @@ func createErrorMessage(err error, h Header, failureStatus uint16) Message {
259262
return m
260263
}
261264

265+
// ProtocolInfo is the protocol information passed to the server's Init function.
266+
type ProtocolInfo struct {
267+
// The version passed down from the client.
268+
// This usually represents a major version,
269+
// so any increment should be considered a breaking change.
270+
Version uint16 `json:"version"`
271+
}
272+
262273
// ServerOptions is the options for a server.
263274
type ServerOptions[C, Q, M, R any] struct {
264275
// Init is the function that will be called when the server is started.
265276
// It can be used to initialize the server with the given configuration.
266277
// If an error is returned, the server will stop.
267-
Init func(C) error
278+
Init func(C, ProtocolInfo) error
268279

269280
// Handle is the function that will be called when a request is received.
270281
Handle func(*Call[Q, M, R])

0 commit comments

Comments
 (0)