FlushTimeout allows a Flush operation to have an associated timeout.
(timeout time.Duration)
| 5796 | |
| 5797 | // FlushTimeout allows a Flush operation to have an associated timeout. |
| 5798 | func (nc *Conn) FlushTimeout(timeout time.Duration) (err error) { |
| 5799 | if nc == nil { |
| 5800 | return ErrInvalidConnection |
| 5801 | } |
| 5802 | if timeout <= 0 { |
| 5803 | return ErrBadTimeout |
| 5804 | } |
| 5805 | |
| 5806 | nc.mu.Lock() |
| 5807 | if nc.isClosed() { |
| 5808 | nc.mu.Unlock() |
| 5809 | return ErrConnectionClosed |
| 5810 | } |
| 5811 | t := globalTimerPool.Get(timeout) |
| 5812 | defer globalTimerPool.Put(t) |
| 5813 | |
| 5814 | // Create a buffered channel to prevent chan send to block |
| 5815 | // in processPong() if this code here times out just when |
| 5816 | // PONG was received. |
| 5817 | ch := make(chan struct{}, 1) |
| 5818 | nc.sendPing(ch) |
| 5819 | nc.mu.Unlock() |
| 5820 | |
| 5821 | select { |
| 5822 | case _, ok := <-ch: |
| 5823 | if !ok { |
| 5824 | err = ErrConnectionClosed |
| 5825 | } else { |
| 5826 | close(ch) |
| 5827 | } |
| 5828 | case <-t.C: |
| 5829 | err = ErrTimeout |
| 5830 | } |
| 5831 | |
| 5832 | if err != nil { |
| 5833 | nc.removeFlushEntry(ch) |
| 5834 | } |
| 5835 | return |
| 5836 | } |
| 5837 | |
| 5838 | // RTT calculates the round trip time between this client and the server. |
| 5839 | func (nc *Conn) RTT() (time.Duration, error) { |