mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Add timeout for webRTCConn
This commit is contained in:
parent
6007d5e08e
commit
5c23fcf14a
1 changed files with 22 additions and 1 deletions
|
@ -32,20 +32,41 @@ type webRTCConn struct {
|
|||
|
||||
bytesLogger bytesLogger
|
||||
eventLogger event.SnowflakeEventReceiver
|
||||
|
||||
inactivityTimeout time.Duration
|
||||
activity chan struct{}
|
||||
}
|
||||
|
||||
func newWebRTCConn(pc *webrtc.PeerConnection, dc *webrtc.DataChannel, pr *io.PipeReader, eventLogger event.SnowflakeEventReceiver) (*webRTCConn) {
|
||||
func newWebRTCConn(pc *webrtc.PeerConnection, dc *webrtc.DataChannel, pr *io.PipeReader, eventLogger event.SnowflakeEventReceiver) *webRTCConn {
|
||||
conn := &webRTCConn{pc: pc, dc: dc, pr: pr, eventLogger: eventLogger}
|
||||
conn.bytesLogger = newBytesSyncLogger()
|
||||
conn.activity = make(chan struct{}, 100)
|
||||
conn.inactivityTimeout = 30 * time.Second
|
||||
go conn.timeoutLoop()
|
||||
return conn
|
||||
}
|
||||
|
||||
func (c *webRTCConn) timeoutLoop() {
|
||||
for {
|
||||
select {
|
||||
case <-time.After(c.inactivityTimeout):
|
||||
c.Close()
|
||||
log.Println("Closed connection due to inactivity")
|
||||
return
|
||||
case <-c.activity:
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (c *webRTCConn) Read(b []byte) (int, error) {
|
||||
return c.pr.Read(b)
|
||||
}
|
||||
|
||||
func (c *webRTCConn) Write(b []byte) (int, error) {
|
||||
c.bytesLogger.AddInbound(int64(len(b)))
|
||||
c.activity <- struct{}{}
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
if c.dc != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue