Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ func TestStartFailed(t *testing.T) {
c.Assert(client.Close(), qt.IsNil)
}

const clientVersion = 3

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] {
client, err := execrpc.StartClient(
execrpc.ClientOptions[model.ExampleConfig, model.ExampleRequest, model.ExampleMessage, model.ExampleReceipt]{
ClientRawOptions: execrpc.ClientRawOptions{
Version: 1,
Version: clientVersion,
Cmd: "go",
Dir: "./examples/servers/" + server,
Args: []string{"run", "."},
Expand Down Expand Up @@ -224,7 +226,7 @@ func TestTyped(t *testing.T) {
client, err := execrpc.StartClient(
execrpc.ClientOptions[model.ExampleConfig, model.ExampleRequest, model.ExampleMessage, model.ExampleReceipt]{
ClientRawOptions: execrpc.ClientRawOptions{
Version: 1,
Version: clientVersion,
Cmd: "go",
Dir: "./examples/servers/typed",
Args: []string{"run", "."},
Expand Down
6 changes: 5 additions & 1 deletion examples/servers/readmeexample/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"hash"
"hash/fnv"
"log"
Expand Down Expand Up @@ -29,7 +30,10 @@ func main() {
// Optional function to initialize the server
// with the client configuration.
// This will be called once on server start.
Init: func(cfg model.ExampleConfig) error {
Init: func(cfg model.ExampleConfig, procol execrpc.ProtocolInfo) error {
if procol.Version != 3 {
return fmt.Errorf("unsupported protocol version: %d", procol.Version)
}
clientConfig = cfg
return clientConfig.Init()
},
Expand Down
5 changes: 4 additions & 1 deletion examples/servers/typed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ func main() {
execrpc.ServerOptions[model.ExampleConfig, model.ExampleRequest, model.ExampleMessage, model.ExampleReceipt]{
GetHasher: getHasher,
DelayDelivery: delayDelivery,
Init: func(cfg model.ExampleConfig) error {
Init: func(cfg model.ExampleConfig, protocol execrpc.ProtocolInfo) error {
if protocol.Version != 3 {
return fmt.Errorf("unsupported protocol version: %d", protocol.Version)
}
clientConfig = cfg
return clientConfig.Init()
},
Expand Down
17 changes: 14 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,18 @@ func NewServer[C, Q, M, R any](opts ServerOptions[C, Q, M, R]) (*Server[C, Q, M,
return nil
}

var cfg C
var (
cfg C
protocolInfo = ProtocolInfo{Version: message.Header.Version}
)
err := opts.Codec.Decode(message.Body, &cfg)
if err != nil {
m := createErrorMessage(err, message.Header, MessageStatusErrDecodeFailed)
d.SendMessage(m)
return nil
}

if err := opts.Init(cfg); err != nil {
if err := opts.Init(cfg, protocolInfo); err != nil {
m := createErrorMessage(err, message.Header, MessageStatusErrInitServerFailed)
d.SendMessage(m)
return nil
Expand Down Expand Up @@ -259,12 +262,20 @@ func createErrorMessage(err error, h Header, failureStatus uint16) Message {
return m
}

// ProtocolInfo is the protocol information passed to the server's Init function.
type ProtocolInfo struct {
// The version passed down from the client.
// This usually represents a major version,
// so any increment should be considered a breaking change.
Version uint16 `json:"version"`
}

// ServerOptions is the options for a server.
type ServerOptions[C, Q, M, R any] struct {
// Init is the function that will be called when the server is started.
// It can be used to initialize the server with the given configuration.
// If an error is returned, the server will stop.
Init func(C) error
Init func(C, ProtocolInfo) error

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