| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package jsonrpc2 |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"sync" |
| 13 |
"sync/atomic" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/modelcontextprotocol/go-sdk/internal/json" |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
type Connection struct { |
| 31 |
seq int64 |
| 32 |
|
| 33 |
stateMu sync.Mutex |
| 34 |
state inFlightState |
| 35 |
done chan struct{} |
| 36 |
|
| 37 |
writer Writer |
| 38 |
handler Handler |
| 39 |
|
| 40 |
onInternalError func(error) |
| 41 |
onDone func() |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
type inFlightState struct { |
| 47 |
connClosing bool |
| 48 |
reading bool |
| 49 |
readErr error |
| 50 |
writeErr error |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
closer io.Closer |
| 60 |
closeErr error |
| 61 |
|
| 62 |
outgoingCalls map[ID]*AsyncCall |
| 63 |
outgoingNotifications int |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
incoming int |
| 68 |
|
| 69 |
incomingByID map[ID]*incomingRequest |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
handlerQueue []*incomingRequest |
| 75 |
handlerRunning bool |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
func (c *Connection) updateInFlight(f func(*inFlightState)) { |
| 82 |
c.stateMu.Lock() |
| 83 |
defer c.stateMu.Unlock() |
| 84 |
|
| 85 |
s := &c.state |
| 86 |
|
| 87 |
f(s) |
| 88 |
|
| 89 |
select { |
| 90 |
case <-c.done: |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
if !s.idle() { |
| 96 |
panic("jsonrpc2: updateInFlight transitioned to non-idle when already done") |
| 97 |
} |
| 98 |
return |
| 99 |
default: |
| 100 |
} |
| 101 |
|
| 102 |
if s.idle() && s.shuttingDown(ErrUnknown) != nil { |
| 103 |
if s.closer != nil { |
| 104 |
s.closeErr = s.closer.Close() |
| 105 |
s.closer = nil |
| 106 |
} |
| 107 |
if s.reading { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
} else { |
| 112 |
|
| 113 |
|
| 114 |
if c.onDone != nil { |
| 115 |
c.onDone() |
| 116 |
} |
| 117 |
close(c.done) |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
func (s *inFlightState) idle() bool { |
| 128 |
return len(s.outgoingCalls) == 0 && s.outgoingNotifications == 0 && s.incoming == 0 && !s.handlerRunning |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
func (s *inFlightState) shuttingDown(errClosing error) error { |
| 135 |
if s.connClosing { |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
return errClosing |
| 140 |
} |
| 141 |
if s.readErr != nil { |
| 142 |
|
| 143 |
|
| 144 |
return fmt.Errorf("%w: %v", errClosing, s.readErr) |
| 145 |
} |
| 146 |
if s.writeErr != nil { |
| 147 |
|
| 148 |
|
| 149 |
return fmt.Errorf("%w: %v", errClosing, s.writeErr) |
| 150 |
} |
| 151 |
return nil |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
type incomingRequest struct { |
| 156 |
*Request |
| 157 |
ctx context.Context |
| 158 |
cancel context.CancelFunc |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
type Reader interface { |
| 169 |
|
| 170 |
Read(context.Context) (Message, error) |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
type Writer interface { |
| 181 |
|
| 182 |
Write(context.Context, Message) error |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
type ConnectionConfig struct { |
| 187 |
Reader Reader |
| 188 |
Writer Writer |
| 189 |
Closer io.Closer |
| 190 |
Preempter Preempter |
| 191 |
Bind func(*Connection) Handler |
| 192 |
OnDone func() |
| 193 |
OnInternalError func(error) |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
func NewConnection(ctx context.Context, cfg ConnectionConfig) *Connection { |
| 199 |
ctx = notDone{ctx} |
| 200 |
|
| 201 |
c := &Connection{ |
| 202 |
state: inFlightState{closer: cfg.Closer}, |
| 203 |
done: make(chan struct{}), |
| 204 |
writer: cfg.Writer, |
| 205 |
onDone: cfg.OnDone, |
| 206 |
onInternalError: cfg.OnInternalError, |
| 207 |
} |
| 208 |
c.handler = cfg.Bind(c) |
| 209 |
c.start(ctx, cfg.Reader, cfg.Preempter) |
| 210 |
return c |
| 211 |
} |
| 212 |
|
| 213 |
func (c *Connection) start(ctx context.Context, reader Reader, preempter Preempter) { |
| 214 |
c.updateInFlight(func(s *inFlightState) { |
| 215 |
select { |
| 216 |
case <-c.done: |
| 217 |
|
| 218 |
return |
| 219 |
default: |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
s.reading = true |
| 224 |
go c.readIncoming(ctx, reader, preempter) |
| 225 |
}) |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
func (c *Connection) Notify(ctx context.Context, method string, params any) (err error) { |
| 232 |
attempted := false |
| 233 |
|
| 234 |
defer func() { |
| 235 |
if attempted { |
| 236 |
c.updateInFlight(func(s *inFlightState) { |
| 237 |
s.outgoingNotifications-- |
| 238 |
}) |
| 239 |
} |
| 240 |
}() |
| 241 |
|
| 242 |
c.updateInFlight(func(s *inFlightState) { |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if len(s.outgoingCalls) == 0 && len(s.incomingByID) == 0 { |
| 248 |
err = s.shuttingDown(ErrClientClosing) |
| 249 |
if err != nil { |
| 250 |
return |
| 251 |
} |
| 252 |
} |
| 253 |
s.outgoingNotifications++ |
| 254 |
attempted = true |
| 255 |
}) |
| 256 |
if err != nil { |
| 257 |
return err |
| 258 |
} |
| 259 |
|
| 260 |
notify, err := NewNotification(method, params) |
| 261 |
if err != nil { |
| 262 |
return fmt.Errorf("marshaling notify parameters: %v", err) |
| 263 |
} |
| 264 |
|
| 265 |
return c.write(ctx, notify) |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
func (c *Connection) Call(ctx context.Context, method string, params any) *AsyncCall { |
| 274 |
|
| 275 |
id := Int64ID(atomic.AddInt64(&c.seq, 1)) |
| 276 |
|
| 277 |
ac := &AsyncCall{ |
| 278 |
id: id, |
| 279 |
ready: make(chan struct{}), |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
call, err := NewCall(ac.id, method, params) |
| 286 |
if err != nil { |
| 287 |
ac.retire(&Response{ID: id, Error: fmt.Errorf("marshaling call parameters: %w", err)}) |
| 288 |
return ac |
| 289 |
} |
| 290 |
|
| 291 |
c.updateInFlight(func(s *inFlightState) { |
| 292 |
err = s.shuttingDown(ErrClientClosing) |
| 293 |
if err != nil { |
| 294 |
return |
| 295 |
} |
| 296 |
if s.outgoingCalls == nil { |
| 297 |
s.outgoingCalls = make(map[ID]*AsyncCall) |
| 298 |
} |
| 299 |
s.outgoingCalls[ac.id] = ac |
| 300 |
}) |
| 301 |
if err != nil { |
| 302 |
ac.retire(&Response{ID: id, Error: err}) |
| 303 |
return ac |
| 304 |
} |
| 305 |
|
| 306 |
if err := c.write(ctx, call); err != nil { |
| 307 |
|
| 308 |
|
| 309 |
c.Retire(ac, err) |
| 310 |
} |
| 311 |
return ac |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
func (c *Connection) Retire(ac *AsyncCall, err error) { |
| 319 |
c.updateInFlight(func(s *inFlightState) { |
| 320 |
if s.outgoingCalls[ac.id] == ac { |
| 321 |
delete(s.outgoingCalls, ac.id) |
| 322 |
ac.retire(&Response{ID: ac.id, Error: err}) |
| 323 |
} else { |
| 324 |
|
| 325 |
} |
| 326 |
}) |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
func Async(ctx context.Context) { |
| 335 |
if r, ok := ctx.Value(asyncKey).(*releaser); ok { |
| 336 |
r.release(false) |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
type asyncKeyType struct{} |
| 341 |
|
| 342 |
var asyncKey = asyncKeyType{} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
type releaser struct { |
| 348 |
mu sync.Mutex |
| 349 |
ch chan struct{} |
| 350 |
released bool |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
func (r *releaser) release(soft bool) { |
| 356 |
r.mu.Lock() |
| 357 |
defer r.mu.Unlock() |
| 358 |
|
| 359 |
if r.released { |
| 360 |
if !soft { |
| 361 |
panic("jsonrpc2.Async called multiple times") |
| 362 |
} |
| 363 |
} else { |
| 364 |
close(r.ch) |
| 365 |
r.released = true |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
type AsyncCall struct { |
| 370 |
id ID |
| 371 |
ready chan struct{} |
| 372 |
response *Response |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
func (ac *AsyncCall) ID() ID { return ac.id } |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
func (ac *AsyncCall) retire(response *Response) { |
| 384 |
select { |
| 385 |
case <-ac.ready: |
| 386 |
panic(fmt.Sprintf("jsonrpc2: retire called twice for ID %v", ac.id)) |
| 387 |
default: |
| 388 |
} |
| 389 |
|
| 390 |
ac.response = response |
| 391 |
close(ac.ready) |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
func (ac *AsyncCall) Await(ctx context.Context, result any) error { |
| 400 |
select { |
| 401 |
case <-ctx.Done(): |
| 402 |
return ctx.Err() |
| 403 |
case <-ac.ready: |
| 404 |
} |
| 405 |
if ac.response.Error != nil { |
| 406 |
return ac.response.Error |
| 407 |
} |
| 408 |
if result == nil { |
| 409 |
return nil |
| 410 |
} |
| 411 |
return json.Unmarshal(ac.response.Result, result) |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
func (c *Connection) Cancel(id ID) { |
| 421 |
var req *incomingRequest |
| 422 |
c.updateInFlight(func(s *inFlightState) { |
| 423 |
req = s.incomingByID[id] |
| 424 |
}) |
| 425 |
if req != nil { |
| 426 |
req.cancel() |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
func (c *Connection) Wait() error { |
| 432 |
return c.wait(true) |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
func (c *Connection) wait(fromWait bool) error { |
| 444 |
var err error |
| 445 |
<-c.done |
| 446 |
c.updateInFlight(func(s *inFlightState) { |
| 447 |
if fromWait { |
| 448 |
if !errors.Is(s.readErr, io.EOF) { |
| 449 |
err = s.readErr |
| 450 |
} |
| 451 |
if err == nil && !errors.Is(s.writeErr, io.EOF) { |
| 452 |
err = s.writeErr |
| 453 |
} |
| 454 |
} |
| 455 |
if err == nil { |
| 456 |
err = s.closeErr |
| 457 |
} |
| 458 |
}) |
| 459 |
return err |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
func (c *Connection) Close() error { |
| 470 |
|
| 471 |
|
| 472 |
c.updateInFlight(func(s *inFlightState) { s.connClosing = true }) |
| 473 |
return c.wait(false) |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
func (c *Connection) readIncoming(ctx context.Context, reader Reader, preempter Preempter) { |
| 479 |
var err error |
| 480 |
for { |
| 481 |
var msg Message |
| 482 |
msg, err = reader.Read(ctx) |
| 483 |
if err != nil { |
| 484 |
break |
| 485 |
} |
| 486 |
|
| 487 |
switch msg := msg.(type) { |
| 488 |
case *Request: |
| 489 |
c.acceptRequest(ctx, msg, preempter) |
| 490 |
|
| 491 |
case *Response: |
| 492 |
c.updateInFlight(func(s *inFlightState) { |
| 493 |
if ac, ok := s.outgoingCalls[msg.ID]; ok { |
| 494 |
delete(s.outgoingCalls, msg.ID) |
| 495 |
ac.retire(msg) |
| 496 |
} else { |
| 497 |
|
| 498 |
} |
| 499 |
}) |
| 500 |
|
| 501 |
default: |
| 502 |
c.internalErrorf("Read returned an unexpected message of type %T", msg) |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
c.updateInFlight(func(s *inFlightState) { |
| 507 |
s.reading = false |
| 508 |
s.readErr = err |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
for id, ac := range s.outgoingCalls { |
| 513 |
ac.retire(&Response{ID: id, Error: err}) |
| 514 |
} |
| 515 |
s.outgoingCalls = nil |
| 516 |
}) |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
func (c *Connection) acceptRequest(ctx context.Context, msg *Request, preempter Preempter) { |
| 522 |
|
| 523 |
|
| 524 |
reqCtx, cancel := context.WithCancel(ctx) |
| 525 |
req := &incomingRequest{ |
| 526 |
Request: msg, |
| 527 |
ctx: reqCtx, |
| 528 |
cancel: cancel, |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
var err error |
| 534 |
c.updateInFlight(func(s *inFlightState) { |
| 535 |
s.incoming++ |
| 536 |
|
| 537 |
if req.IsCall() { |
| 538 |
if s.incomingByID[req.ID] != nil { |
| 539 |
err = fmt.Errorf("%w: request ID %v already in use", ErrInvalidRequest, req.ID) |
| 540 |
req.ID = ID{} |
| 541 |
return |
| 542 |
} |
| 543 |
|
| 544 |
if s.incomingByID == nil { |
| 545 |
s.incomingByID = make(map[ID]*incomingRequest) |
| 546 |
} |
| 547 |
s.incomingByID[req.ID] = req |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
err = s.shuttingDown(ErrServerClosing) |
| 554 |
} |
| 555 |
}) |
| 556 |
if err != nil { |
| 557 |
c.processResult("acceptRequest", req, nil, err) |
| 558 |
return |
| 559 |
} |
| 560 |
|
| 561 |
if preempter != nil { |
| 562 |
result, err := preempter.Preempt(req.ctx, req.Request) |
| 563 |
|
| 564 |
if !errors.Is(err, ErrNotHandled) { |
| 565 |
c.processResult("Preempt", req, result, err) |
| 566 |
return |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
c.updateInFlight(func(s *inFlightState) { |
| 571 |
|
| 572 |
er |
| 573 |
|
| 574 |
|
| 575 |
err = s.shuttingDown(ErrServerClosing) |
| 576 |
if err != nil { |
| 577 |
return |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
s.handlerQueue = append(s.handlerQueue, req) |
| 587 |
if !s.handlerRunning { |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
s.handlerRunning = true |
| 601 |
go c.handleAsync() |
| 602 |
} |
| 603 |
}) |
| 604 |
if err != nil { |
| 605 |
c.processResult("acceptRequest", req, nil, err) |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
func (c *Connection) handleAsync() { |
| 612 |
for { |
| 613 |
var req *incomingRequest |
| 614 |
c.updateInFlight(func(s *inFlightState) { |
| 615 |
if len(s.handlerQueue) > 0 { |
| 616 |
req, s.handlerQueue = s.handlerQueue[0], s.handlerQueue[1:] |
| 617 |
} else { |
| 618 |
s.handlerRunning = false |
| 619 |
} |
| 620 |
}) |
| 621 |
if req == nil { |
| 622 |
return |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
if err := req.ctx.Err(); err != nil { |
| 627 |
c.updateInFlight(func(s *inFlightState) { |
| 628 |
if s.writeErr != nil { |
| 629 |
|
| 630 |
|
| 631 |
err = fmt.Errorf("%w: %v", ErrServerClosing, s.writeErr) |
| 632 |
} |
| 633 |
}) |
| 634 |
c.processResult("handleAsync", req, nil, err) |
| 635 |
continue |
| 636 |
} |
| 637 |
|
| 638 |
releaser := &releaser{ch: make(chan struct{})} |
| 639 |
ctx := context.WithValue(req.ctx, asyncKey, releaser) |
| 640 |
go func() { |
| 641 |
defer releaser.release(true) |
| 642 |
result, err := c.handler.Handle(ctx, req.Request) |
| 643 |
c.processResult(c.handler, req, result, err) |
| 644 |
}() |
| 645 |
<-releaser.ch |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
func (c *Connection) processResult(from any, req *incomingRequest, result any, err error) error { |
| 651 |
switch err { |
| 652 |
case ErrNotHandled, ErrMethodNotFound: |
| 653 |
|
| 654 |
err = fmt.Errorf("%w: %q", ErrMethodNotFound, req.Method) |
| 655 |
} |
| 656 |
|
| 657 |
if result != nil && err != nil { |
| 658 |
c.internalErrorf("%#v returned a non-nil result with a non-nil error for %s:\n%v\n%#v", from, req.Method, err, result) |
| 659 |
result = nil |
| 660 |
} |
| 661 |
|
| 662 |
if req.IsCall() { |
| 663 |
if result == nil && err == nil { |
| 664 |
err = c.internalErrorf("%#v returned a nil result and nil error for a %q Request that requires a Response", from, req.Method) |
| 665 |
} |
| 666 |
|
| 667 |
response, respErr := NewResponse(req.ID, result, err) |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
c.updateInFlight(func(s *inFlightState) { |
| 673 |
delete(s.incomingByID, req.ID) |
| 674 |
}) |
| 675 |
if respErr == nil { |
| 676 |
writeErr := c.write(notDone{req.ctx}, response) |
| 677 |
if err == nil { |
| 678 |
err = writeErr |
| 679 |
} |
| 680 |
} else { |
| 681 |
err = c.internalErrorf("%#v returned a malformed result for %q: %w", from, req.Method, respErr) |
| 682 |
} |
| 683 |
} else { |
| 684 |
if result != nil { |
| 685 |
err = c.internalErrorf("%#v returned a non-nil result for a %q Request without an ID", from, req.Method) |
| 686 |
} else if err != nil { |
| 687 |
err = fmt.Errorf("%w: %q notification failed: %v", ErrInternal, req.Method, err) |
| 688 |
} |
| 689 |
} |
| 690 |
if err != nil { |
| 691 |
|
| 692 |
|
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
req.cancel() |
| 697 |
c.updateInFlight(func(s *inFlightState) { |
| 698 |
if s.incoming == 0 { |
| 699 |
panic("jsonrpc2: processResult called when incoming count is already zero") |
| 700 |
} |
| 701 |
s.incoming-- |
| 702 |
}) |
| 703 |
return nil |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
func (c *Connection) write(ctx context.Context, msg Message) error { |
| 709 |
var err error |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
c.updateInFlight(func(s *inFlightState) { |
| 715 |
err = s.shuttingDown(ErrServerClosing) |
| 716 |
}) |
| 717 |
if err == nil { |
| 718 |
err = c.writer.Write(ctx, msg) |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
if err != nil && ctx.Err() == nil && !errors.Is(err, ErrRejected) { |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
c.updateInFlight(func(s *inFlightState) { |
| 733 |
if s.writeErr == nil { |
| 734 |
s.writeErr = err |
| 735 |
for _, r := range s.incomingByID { |
| 736 |
r.cancel() |
| 737 |
} |
| 738 |
} |
| 739 |
}) |
| 740 |
} |
| 741 |
|
| 742 |
return err |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
func (c *Connection) internalErrorf(format string, args ...any) error { |
| 749 |
err := fmt.Errorf(format, args...) |
| 750 |
if c.onInternalError == nil { |
| 751 |
panic("jsonrpc2: " + err.Error()) |
| 752 |
} |
| 753 |
c.onInternalError(err) |
| 754 |
|
| 755 |
return fmt.Errorf("%w: %v", ErrInternal, err) |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
type notDone struct{ ctx context.Context } |
| 760 |
|
| 761 |
func (ic notDone) Value(key any) any { |
| 762 |
return ic.ctx.Value(key) |
| 763 |
} |
| 764 |
|
| 765 |
func (notDone) Done() <-chan struct{} { return nil } |
| 766 |
func (notDone) Err() error { return nil } |
| 767 |
func (notDone) Deadline() (time.Time, bool) { return time.Time{}, false } |