sendMethod wraps the arguments, adds a response callback, marshals the message and send it over the wire.
(method string, args []interface{}, timeout time.Duration, responseChan chan *response)
| 751 | // sendMethod wraps the arguments, adds a response callback, |
| 752 | // marshals the message and send it over the wire. |
| 753 | func (c *Client) sendMethod(method string, args []interface{}, timeout time.Duration, responseChan chan *response) { |
| 754 | // To clean the sent callback after response is received. |
| 755 | // Send/Receive in a channel to prevent race condition because |
| 756 | // the callback is run in a separate goroutine. |
| 757 | removeCallback := make(chan uint64, 1) |
| 758 | |
| 759 | // When a callback is called it will send the response to this channel. |
| 760 | doneChan := make(chan *response, 1) |
| 761 | |
| 762 | cb := c.makeResponseCallback(doneChan, removeCallback, method, args) |
| 763 | args = c.wrapMethodArgs(args, cb) |
| 764 | |
| 765 | callbacks, errC, err := c.marshalAndSend(method, args) |
| 766 | if err != nil { |
| 767 | responseChan <- &response{ |
| 768 | Result: nil, |
| 769 | Err: &Error{ |
| 770 | Type: "sendError", |
| 771 | Message: err.Error(), |
| 772 | }, |
| 773 | } |
| 774 | return |
| 775 | } |
| 776 | |
| 777 | // nil value of afterTimeout means no timeout, it will not selected in |
| 778 | // select statement |
| 779 | var afterTimeout <-chan time.Time |
| 780 | if timeout > 0 { |
| 781 | afterTimeout = time.After(timeout) |
| 782 | } |
| 783 | |
| 784 | // Waits until the response has came or the connection has disconnected. |
| 785 | go func() { |
| 786 | c.disconnectMu.Lock() |
| 787 | defer c.disconnectMu.Unlock() |
| 788 | |
| 789 | select { |
| 790 | case resp := <-doneChan: |
| 791 | if e, ok := resp.Err.(*Error); ok { |
| 792 | if e.Type == "authenticationError" && strings.Contains(e.Message, "token is expired") { |
| 793 | c.callOnTokenExpireHandlers() |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | responseChan <- resp |
| 798 | case <-c.disconnect: |
| 799 | responseChan <- &response{ |
| 800 | nil, |
| 801 | &Error{ |
| 802 | Type: "disconnect", |
| 803 | Message: "Remote kite has disconnected", |
| 804 | }, |
| 805 | } |
| 806 | case err := <-errC: |
| 807 | if err != nil { |
| 808 | responseChan <- &response{ |
| 809 | nil, |
| 810 | &Error{ |
no test coverage detected