BatchCall sends all given requests as a single batch and waits for the server to return a response for all of them. The wait duration is bounded by the context's deadline. In contrast to CallContext, BatchCallContext only returns errors that have occurred while sending the request. Any error specif
(ctx context.Context, b []BatchElem)
| 339 | // |
| 340 | // Note that batch calls may not be executed atomically on the server side. |
| 341 | func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error { |
| 342 | msgs := make([]*jsonrpcMessage, len(b)) |
| 343 | op := &requestOp{ |
| 344 | ids: make([]json.RawMessage, len(b)), |
| 345 | resp: make(chan *jsonrpcMessage, len(b)), |
| 346 | } |
| 347 | for i, elem := range b { |
| 348 | msg, err := c.newMessage(elem.Method, elem.Args...) |
| 349 | if err != nil { |
| 350 | return err |
| 351 | } |
| 352 | msgs[i] = msg |
| 353 | op.ids[i] = msg.ID |
| 354 | } |
| 355 | |
| 356 | var err error |
| 357 | if c.isHTTP { |
| 358 | err = c.sendBatchHTTP(ctx, op, msgs) |
| 359 | } else { |
| 360 | err = c.send(ctx, op, msgs) |
| 361 | } |
| 362 | |
| 363 | // Wait for all responses to come back. |
| 364 | for n := 0; n < len(b) && err == nil; n++ { |
| 365 | var resp *jsonrpcMessage |
| 366 | resp, err = op.wait(ctx) |
| 367 | if err != nil { |
| 368 | break |
| 369 | } |
| 370 | // Find the element corresponding to this response. |
| 371 | // The element is guaranteed to be present because dispatch |
| 372 | // only sends valid IDs to our channel. |
| 373 | var elem *BatchElem |
| 374 | for i := range msgs { |
| 375 | if bytes.Equal(msgs[i].ID, resp.ID) { |
| 376 | elem = &b[i] |
| 377 | break |
| 378 | } |
| 379 | } |
| 380 | if resp.Error != nil { |
| 381 | elem.Error = resp.Error |
| 382 | continue |
| 383 | } |
| 384 | if len(resp.Result) == 0 { |
| 385 | elem.Error = ErrNoResult |
| 386 | continue |
| 387 | } |
| 388 | elem.Error = json.Unmarshal(resp.Result, elem.Result) |
| 389 | } |
| 390 | return err |
| 391 | } |
| 392 | |
| 393 | // EthSubscribe registers a subscripion under the "eth" namespace. |
| 394 | func (c *Client) EthSubscribe(ctx context.Context, channel interface{}, args ...interface{}) (*ClientSubscription, error) { |
no test coverage detected