Skip to content
Merged
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
Binary file added .DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
tests:
strategy:
matrix:
go-version: [ 1.24.x, 1.25.x ]
go-version: [ 1.25.x ]
runner_arch: [ ubuntu-latest, ubuntu-24.04-arm ]

runs-on: ${{ matrix.runner_arch }}
Expand Down Expand Up @@ -41,7 +41,7 @@ jobs:
bench:
strategy:
matrix:
go-version: [ 1.24.x, 1.25.x ]
go-version: [ 1.25.x ]
runner_arch: [ ubuntu-latest, ubuntu-24.04-arm ]

runs-on: ${{ matrix.runner_arch }}
Expand Down
40 changes: 0 additions & 40 deletions data.go

This file was deleted.

29 changes: 0 additions & 29 deletions data_test.go

This file was deleted.

79 changes: 17 additions & 62 deletions execute.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,52 @@
package jsonrpc

import (
"bytes"
"context"
"io"
"net/http"
"sync"

"github.com/bytedance/sonic"
"github.com/rotisserie/eris"
)

var bufferPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, 512))
},
type praparedRPCRequest[Resp any] struct {
internal *http.Request
err error
}

func UseClient(cli *http.Client) func(in *http.Client) {
return func(in *http.Client) {
in = cli
}
}

func SetHeader(key, value string) func(in *http.Request) {
return func(in *http.Request) {
in.Header.Set(key, value)
}
}

func UseContext(ctx context.Context) func(in *http.Request) {
return func(in *http.Request) {
in = in.WithContext(ctx)
}
}
type ExecuteOpt func(*http.Client)

func (rpc *RPCRequest[Resp]) Execute(url string, opts ...any) (*Resp, error) {
buf := bufferPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufferPool.Put(buf)

enc := sonic.ConfigFastest.NewEncoder(buf)
if err := enc.Encode(rpc); err != nil {
return nil, eris.Wrap(err, "marshal request")
func (rpc *praparedRPCRequest[Resp]) Execute(client *http.Client, opts ...ExecuteOpt) (*Resp, error) {
if rpc.err != nil {
return nil, eris.Wrap(rpc.err, "execute prepared request")
}

req, err := http.NewRequest(http.MethodPost, url, buf)
if err != nil {
return nil, eris.Wrap(err, "prepare req")
cli := client
if client == nil {
cli = defaultHTTPClient
}
req.ContentLength = int64(buf.Len())
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")

cl := defaultHTTPClient
for _, opt := range opts {
switch v := opt.(type) {
case func(in *http.Request):
v(req)
case func(in *http.Client):
v(cl)
}
opt(cli)
}

resp, err := cl.Do(req)
resp, err := cli.Do(rpc.internal)
if err != nil {
return nil, eris.Wrap(err, "execute req")
}

defer func() {
_ = resp.Body.Close()
}()

if resp.StatusCode < 200 || resp.StatusCode > 299 {
_, _ = io.CopyN(io.Discard, resp.Body, 1024)
_, _ = io.Copy(io.Discard, resp.Body)
return nil, eris.Errorf("http status %d", resp.StatusCode)
}

var result RPCResponse[Resp]
err = sonic.ConfigFastest.Unmarshal(readAll(resp.Body), &result)
if err != nil {

decoder := sonic.ConfigDefault.NewDecoder(resp.Body)
if err := decoder.Decode(&result); err != nil {
return nil, eris.Wrap(err, "decode response")
}

Expand All @@ -88,16 +56,3 @@ func (rpc *RPCRequest[Resp]) Execute(url string, opts ...any) (*Resp, error) {

return &result.Result, nil
}

// readAll efficiently reads response body
func readAll(r io.Reader) []byte {
buf := bufferPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufferPool.Put(buf)

buf.ReadFrom(r)
// Return copy to avoid buffer pool corruption
result := make([]byte, buf.Len())
copy(result, buf.Bytes())
return result
}
Loading