2019-08-16 20:03:11 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
|
|
|
)
|
|
|
|
|
2019-09-24 14:56:50 +00:00
|
|
|
const defaultReadDuration = ttfbTimeout
|
|
|
|
const defaultWriteDuration = 10 * time.Second // RESP_TIMEOUT
|
|
|
|
|
2019-08-16 20:03:11 +00:00
|
|
|
func setRPCStreamDeadlines(stream network.Stream) {
|
2019-09-24 14:56:50 +00:00
|
|
|
setStreamReadDeadline(stream, defaultReadDuration)
|
|
|
|
setStreamWriteDeadline(stream, defaultWriteDuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setStreamReadDeadline(stream network.Stream, duration time.Duration) {
|
2019-09-25 17:00:04 +00:00
|
|
|
// libp2p uses the system clock time for determining the deadline so we use
|
|
|
|
// time.Now() instead of the synchronized roughtime.Now().
|
|
|
|
stream.SetReadDeadline(time.Now().Add(duration))
|
2019-09-24 14:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setStreamWriteDeadline(stream network.Stream, duration time.Duration) {
|
2019-09-25 17:00:04 +00:00
|
|
|
// libp2p uses the system clock time for determining the deadline so we use
|
|
|
|
// time.Now() instead of the synchronized roughtime.Now().
|
|
|
|
stream.SetWriteDeadline(time.Now().Add(duration))
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|