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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,27 @@ func (c *Client) Rpc(fn string, args interface{}, opts *RpcOptions) *FilterBuild
// RpcWithError executes a Postgres function (a.k.a., Remote Procedure Call), given the
// function name and, optionally, a body, returning the result as a string.
func (c *Client) RpcWithError(name string, count string, rpcBody interface{}) (string, error) {
return c.RpcWithContext(context.Background(), name, count, rpcBody)
}

// RpcWithContext executes a Postgres function (RPC) with the given context.
func (c *Client) RpcWithContext(
ctx context.Context,
name string,
count string,
rpcBody interface{},
) (string, error) {
opts := &RpcOptions{Count: count}
filterBuilder := c.Rpc(name, rpcBody, opts)
response, err := filterBuilder.Execute(context.Background())

response, err := filterBuilder.Execute(ctx)
if err != nil {
return "", err
}
if response.Error != nil {
return "", response.Error
}
// Convert response.Data to string

dataBytes, _ := json.Marshal(response.Data)
return string(dataBytes), nil
}
Expand Down
13 changes: 13 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,16 @@ func TestClient_RpcWithError(t *testing.T) {
assert.NotEmpty(t, result)
}
}

func TestClient_RpcWithContext_Canceled(t *testing.T) {
c := createClient(t)

ctx, cancel := context.WithCancel(context.Background())
cancel() // immediately cancel

_, err := c.RpcWithContext(ctx, "test_function", "", map[string]interface{}{
"a": 1,
})

assert.Error(t, err)
}