Skip to content

Commit b30096b

Browse files
committed
all: remove gojay implemens
1 parent 7c56226 commit b30096b

24 files changed

+217
-1292
lines changed

.circleci/coverage-targets

Lines changed: 0 additions & 2 deletions
This file was deleted.

.circleci/lint-buildtags

Lines changed: 0 additions & 2 deletions
This file was deleted.

Makefile

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ test: ## Runs package test including race condition.
6060
$(call target)
6161
@CGO_ENABLED=${CGO_ENABLED} ${GO_TEST} ${GO_TEST_FLAGS} -run=${GO_TEST_FUNC} $(strip ${GO_FLAGS}) ${GO_TEST_PKGS}
6262

63-
.PHONY: test/gojay
64-
test/gojay: GO_BUILDTAGS+=gojay
65-
test/gojay: test
66-
6763
.PHONY: bench
6864
bench: ## Take a package benchmark.
6965
$(call target)
@@ -76,9 +72,6 @@ coverage: ## Takes packages test coverage.
7672
$(call target)
7773
@CGO_ENABLED=${CGO_ENABLED} ${GO_TEST} ${GO_TEST_FLAGS} -covermode=atomic -coverpkg=${PKG}/... -coverprofile=coverage.out $(strip ${GO_FLAGS}) ${GO_PKGS}
7874

79-
coverage/gojay: GO_BUILDTAGS+=gojay
80-
coverage/gojay: coverage
81-
8275

8376
##@ fmt, lint
8477

conn.go

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
package jsonrpc2
55

66
import (
7+
"bytes"
78
"context"
9+
"encoding/json"
810
"fmt"
911
"sync"
1012
"sync/atomic"
@@ -75,7 +77,74 @@ func NewConn(s Stream) Conn {
7577
return conn
7678
}
7779

78-
// Notify implemens Conn.
80+
// Call implements Conn.
81+
func (c *conn) Call(ctx context.Context, method string, params, result interface{}) (_ ID, err error) {
82+
// generate a new request identifier
83+
id := ID{number: atomic.AddInt64(&c.seq, 1)}
84+
call, err := NewCall(id, method, params)
85+
if err != nil {
86+
return id, fmt.Errorf("marshaling call parameters: %w", err)
87+
}
88+
89+
ctx, done := event.Start(ctx, method,
90+
tag.Method.Of(method),
91+
tag.RPCDirection.Of(tag.Outbound),
92+
tag.RPCID.Of(fmt.Sprintf("%q", id)),
93+
)
94+
defer func() {
95+
recordStatus(ctx, err)
96+
done()
97+
}()
98+
event.Metric(ctx, tag.Started.Of(1))
99+
100+
// We have to add ourselves to the pending map before we send, otherwise we
101+
// are racing the response. Also add a buffer to rchan, so that if we get a
102+
// wire response between the time this call is cancelled and id is deleted
103+
// from c.pending, the send to rchan will not block.
104+
rchan := make(chan *Response, 1)
105+
106+
c.pendingMu.Lock()
107+
c.pending[id] = rchan
108+
c.pendingMu.Unlock()
109+
110+
defer func() {
111+
c.pendingMu.Lock()
112+
delete(c.pending, id)
113+
c.pendingMu.Unlock()
114+
}()
115+
116+
// now we are ready to send
117+
n, err := c.write(ctx, call)
118+
event.Metric(ctx, tag.SentBytes.Of(n))
119+
if err != nil {
120+
// sending failed, we will never get a response, so don't leave it pending
121+
return id, err
122+
}
123+
124+
// now wait for the response
125+
select {
126+
case response := <-rchan:
127+
switch {
128+
case response.err != nil: // is it an error response?
129+
return id, response.err
130+
131+
case result == nil || len(response.result) == 0:
132+
return id, nil
133+
134+
default:
135+
dec := json.NewDecoder(bytes.NewReader(response.result))
136+
if err := dec.Decode(result); err != nil {
137+
return id, fmt.Errorf("unmarshaling result: %w", err)
138+
}
139+
return id, nil
140+
}
141+
142+
case <-ctx.Done():
143+
return id, ctx.Err()
144+
}
145+
}
146+
147+
// Notify implements Conn.
79148
func (c *conn) Notify(ctx context.Context, method string, params interface{}) (err error) {
80149
notify, err := NewNotification(method, params)
81150
if err != nil {
@@ -128,7 +197,7 @@ func (c *conn) write(ctx context.Context, msg Message) (int64, error) {
128197
return c.stream.Write(ctx, msg)
129198
}
130199

131-
// Go implemens Conn.
200+
// Go implements Conn.
132201
func (c *conn) Go(ctx context.Context, handler Handler) {
133202
go c.run(ctx, handler)
134203
}
@@ -176,17 +245,17 @@ func (c *conn) run(ctx context.Context, handler Handler) {
176245
}
177246
}
178247

179-
// Close implemens Conn.
248+
// Close implements Conn.
180249
func (c *conn) Close() error {
181250
return c.stream.Close()
182251
}
183252

184-
// Done implemens Conn.
253+
// Done implements Conn.
185254
func (c *conn) Done() <-chan struct{} {
186255
return c.done
187256
}
188257

189-
// Err implemens Conn.
258+
// Err implements Conn.
190259
func (c *conn) Err() error {
191260
if err := c.err.Load(); err != nil {
192261
return err.(error)

conn_gojay.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

conn_json.go

Lines changed: 0 additions & 85 deletions
This file was deleted.

errors.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package jsonrpc2
66
import (
77
"errors"
88
"fmt"
9+
10+
json "github.com/goccy/go-json"
911
)
1012

1113
// Error represents a JSON-RPC error.
@@ -18,7 +20,7 @@ type Error struct {
1820

1921
// Data a Primitive or Structured value that contains additional
2022
// information about the error. Can be omitted.
21-
Data *RawMessage `json:"data,omitempty"`
23+
Data *json.RawMessage `json:"data,omitempty"`
2224
}
2325

2426
// compile time check whether the Error implements error interface.

errors_gojay.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ module go.lsp.dev/jsonrpc2
33
go 1.15
44

55
require (
6-
github.com/francoispqt/gojay v1.2.13
76
github.com/goccy/go-json v0.3.4
8-
github.com/google/go-cmp v0.5.4
97
go.lsp.dev/pkg v0.0.0-20210125030640-b6310ac75a91
108
)

0 commit comments

Comments
 (0)