Skip to content

Commit

Permalink
Add test and docs for custom transport
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Jun 6, 2024
1 parent 5e380fa commit 2b2ff55
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,49 @@ type _ interface {

```

### Custom Transport Feature
The go-jsonrpc library supports creating clients with custom transport mechanisms (e.g. use for IPC). This allows for greater flexibility in how requests are sent and received, enabling the use of custom protocols, special handling of requests, or integration with other systems.

#### Example Usage of Custom Transport

Here is an example demonstrating how to create a custom client with a custom transport mechanism:

```go
// Setup server
serverHandler := &SimpleServerHandler{} // some type with methods

rpcServer := jsonrpc.NewServer()
rpcServer.Register("SimpleServerHandler", serverHandler)

// Custom doRequest function
doRequest := func(ctx context.Context, body []byte) (io.ReadCloser, error) {
reader := bytes.NewReader(body)
pr, pw := io.Pipe()
go func() {
defer pw.Close()
rpcServer.HandleRequest(ctx, reader, pw) // handle the rpc frame
}()
return pr, nil
}

var client struct {
Add func(int) error
}

// Create custom client
closer, err := jsonrpc.NewCustomClient("SimpleServerHandler", []interface{}{&client}, doRequest)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer closer()

// Use the client
if err := client.Add(10); err != nil {
log.Fatalf("Failed to call Add: %v", err)
}
fmt.Printf("Current value: %d\n", client.AddGet(5))
```

## Contribute

PRs are welcome!
Expand Down
4 changes: 4 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func NewCustomClient(namespace string, outs []interface{}, doRequest func(ctx co
return clientResponse{}, xerrors.Errorf("marshalling request: %w", err)
}

if ctx == nil {
ctx = context.Background()
}

rawResp, err := doRequest(ctx, b)
if err != nil {
return clientResponse{}, xerrors.Errorf("doRequest failed: %w", err)
Expand Down
41 changes: 41 additions & 0 deletions rpc_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jsonrpc

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -1651,3 +1652,43 @@ func TestBigResult(t *testing.T) {

fmt.Println("done")
}

func TestNewCustomClient(t *testing.T) {
// Setup server
serverHandler := &SimpleServerHandler{}
rpcServer := NewServer()
rpcServer.Register("SimpleServerHandler", serverHandler)

// Custom doRequest function
doRequest := func(ctx context.Context, body []byte) (io.ReadCloser, error) {
reader := bytes.NewReader(body)
pr, pw := io.Pipe()
go func() {
defer pw.Close()
rpcServer.HandleRequest(ctx, reader, pw)
}()
return pr, nil
}

var client struct {
Add func(int) error
AddGet func(int) int
}

// Create custom client
closer, err := NewCustomClient("SimpleServerHandler", []interface{}{&client}, doRequest)
require.NoError(t, err)
defer closer()

// Add(int) error
require.NoError(t, client.Add(10))
require.Equal(t, int32(10), serverHandler.n)

err = client.Add(-3546)
require.EqualError(t, err, "test")

// AddGet(int) int
n := client.AddGet(3)
require.Equal(t, 13, n)
require.Equal(t, int32(13), serverHandler.n)
}

0 comments on commit 2b2ff55

Please sign in to comment.