net.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package common
  20. import (
  21. "container/list"
  22. "net"
  23. "sync"
  24. "sync/atomic"
  25. "time"
  26. "github.com/Psiphon-Inc/goarista/monotime"
  27. )
  28. // Conns is a synchronized list of Conns that is used to coordinate
  29. // interrupting a set of goroutines establishing connections, or
  30. // close a set of open connections, etc.
  31. // Once the list is closed, no more items may be added to the
  32. // list (unless it is reset).
  33. type Conns struct {
  34. mutex sync.Mutex
  35. isClosed bool
  36. conns map[net.Conn]bool
  37. }
  38. func (conns *Conns) Reset() {
  39. conns.mutex.Lock()
  40. defer conns.mutex.Unlock()
  41. conns.isClosed = false
  42. conns.conns = make(map[net.Conn]bool)
  43. }
  44. func (conns *Conns) Add(conn net.Conn) bool {
  45. conns.mutex.Lock()
  46. defer conns.mutex.Unlock()
  47. if conns.isClosed {
  48. return false
  49. }
  50. if conns.conns == nil {
  51. conns.conns = make(map[net.Conn]bool)
  52. }
  53. conns.conns[conn] = true
  54. return true
  55. }
  56. func (conns *Conns) Remove(conn net.Conn) {
  57. conns.mutex.Lock()
  58. defer conns.mutex.Unlock()
  59. delete(conns.conns, conn)
  60. }
  61. func (conns *Conns) CloseAll() {
  62. conns.mutex.Lock()
  63. defer conns.mutex.Unlock()
  64. conns.isClosed = true
  65. for conn, _ := range conns.conns {
  66. conn.Close()
  67. }
  68. conns.conns = make(map[net.Conn]bool)
  69. }
  70. // IPAddressFromAddr is a helper which extracts an IP address
  71. // from a net.Addr or returns "" if there is no IP address.
  72. func IPAddressFromAddr(addr net.Addr) string {
  73. ipAddress := ""
  74. if addr != nil {
  75. host, _, err := net.SplitHostPort(addr.String())
  76. if err == nil {
  77. ipAddress = host
  78. }
  79. }
  80. return ipAddress
  81. }
  82. // LRUConns is a concurrency-safe list of net.Conns ordered
  83. // by recent activity. Its purpose is to facilitate closing
  84. // the oldest connection in a set of connections.
  85. //
  86. // New connections added are referenced by a LRUConnsEntry,
  87. // which is used to Touch() active connections, which
  88. // promotes them to the front of the order and to Remove()
  89. // connections that are no longer LRU candidates.
  90. //
  91. // CloseOldest() will remove the oldest connection from the
  92. // list and call net.Conn.Close() on the connection.
  93. //
  94. // After an entry has been removed, LRUConnsEntry Touch()
  95. // and Remove() will have no effect.
  96. type LRUConns struct {
  97. mutex sync.Mutex
  98. list *list.List
  99. }
  100. // NewLRUConns initializes a new LRUConns.
  101. func NewLRUConns() *LRUConns {
  102. return &LRUConns{list: list.New()}
  103. }
  104. // Add inserts a net.Conn as the freshest connection
  105. // in a LRUConns and returns an LRUConnsEntry to be
  106. // used to freshen the connection or remove the connection
  107. // from the LRU list.
  108. func (conns *LRUConns) Add(conn net.Conn) *LRUConnsEntry {
  109. conns.mutex.Lock()
  110. defer conns.mutex.Unlock()
  111. return &LRUConnsEntry{
  112. lruConns: conns,
  113. element: conns.list.PushFront(conn),
  114. }
  115. }
  116. // CloseOldest closes the oldest connection in a
  117. // LRUConns. It calls net.Conn.Close() on the
  118. // connection.
  119. func (conns *LRUConns) CloseOldest() {
  120. conns.mutex.Lock()
  121. oldest := conns.list.Back()
  122. if oldest != nil {
  123. conns.list.Remove(oldest)
  124. }
  125. // Release mutex before closing conn
  126. conns.mutex.Unlock()
  127. if oldest != nil {
  128. oldest.Value.(net.Conn).Close()
  129. }
  130. }
  131. // LRUConnsEntry is an entry in a LRUConns list.
  132. type LRUConnsEntry struct {
  133. lruConns *LRUConns
  134. element *list.Element
  135. }
  136. // Remove deletes the connection referenced by the
  137. // LRUConnsEntry from the associated LRUConns.
  138. // Has no effect if the entry was not initialized
  139. // or previously removed.
  140. func (entry *LRUConnsEntry) Remove() {
  141. if entry.lruConns == nil || entry.element == nil {
  142. return
  143. }
  144. entry.lruConns.mutex.Lock()
  145. defer entry.lruConns.mutex.Unlock()
  146. entry.lruConns.list.Remove(entry.element)
  147. }
  148. // Touch promotes the connection referenced by the
  149. // LRUConnsEntry to the front of the associated LRUConns.
  150. // Has no effect if the entry was not initialized
  151. // or previously removed.
  152. func (entry *LRUConnsEntry) Touch() {
  153. if entry.lruConns == nil || entry.element == nil {
  154. return
  155. }
  156. entry.lruConns.mutex.Lock()
  157. defer entry.lruConns.mutex.Unlock()
  158. entry.lruConns.list.MoveToFront(entry.element)
  159. }
  160. // Closer defines the interface to a type, typically
  161. // a net.Conn, that can be closed.
  162. type Closer interface {
  163. IsClosed() bool
  164. }
  165. // ActivityMonitoredConn wraps a net.Conn, adding logic to deal with
  166. // events triggered by I/O activity.
  167. //
  168. // When an inactivity timeout is specified, the network I/O will
  169. // timeout after the specified period of read inactivity. Optionally,
  170. // for the purpose of inactivity only, ActivityMonitoredConn will also
  171. // consider the connection active when data is written to it.
  172. //
  173. // When a LRUConnsEntry is specified, then the LRU entry is promoted on
  174. // either a successful read or write.
  175. //
  176. // When an ActivityUpdater is set, then its UpdateActivity method is
  177. // called on each read and write with the number of bytes transferred.
  178. // The durationNanoseconds, which is the time since the last read, is
  179. // reported only on reads.
  180. //
  181. type ActivityMonitoredConn struct {
  182. // Note: 64-bit ints used with atomic operations are at placed
  183. // at the start of struct to ensure 64-bit alignment.
  184. // (https://golang.org/pkg/sync/atomic/#pkg-note-BUG)
  185. monotonicStartTime int64
  186. lastReadActivityTime int64
  187. realStartTime time.Time
  188. net.Conn
  189. inactivityTimeout time.Duration
  190. activeOnWrite bool
  191. activityUpdater ActivityUpdater
  192. lruEntry *LRUConnsEntry
  193. }
  194. // ActivityUpdater defines an interface for receiving updates for
  195. // ActivityMonitoredConn activity. Values passed to UpdateProgress are
  196. // bytes transferred and conn duration since the previous UpdateProgress.
  197. type ActivityUpdater interface {
  198. UpdateProgress(bytesRead, bytesWritten int64, durationNanoseconds int64)
  199. }
  200. // NewActivityMonitoredConn creates a new ActivityMonitoredConn.
  201. func NewActivityMonitoredConn(
  202. conn net.Conn,
  203. inactivityTimeout time.Duration,
  204. activeOnWrite bool,
  205. activityUpdater ActivityUpdater,
  206. lruEntry *LRUConnsEntry) (*ActivityMonitoredConn, error) {
  207. if inactivityTimeout > 0 {
  208. err := conn.SetDeadline(time.Now().Add(inactivityTimeout))
  209. if err != nil {
  210. return nil, ContextError(err)
  211. }
  212. }
  213. now := int64(monotime.Now())
  214. return &ActivityMonitoredConn{
  215. Conn: conn,
  216. inactivityTimeout: inactivityTimeout,
  217. activeOnWrite: activeOnWrite,
  218. realStartTime: time.Now(),
  219. monotonicStartTime: now,
  220. lastReadActivityTime: now,
  221. activityUpdater: activityUpdater,
  222. lruEntry: lruEntry,
  223. }, nil
  224. }
  225. // GetStartTime gets the time when the ActivityMonitoredConn was
  226. // initialized. Reported time is UTC.
  227. func (conn *ActivityMonitoredConn) GetStartTime() time.Time {
  228. return conn.realStartTime.UTC()
  229. }
  230. // GetActiveDuration returns the time elapsed between the initialization
  231. // of the ActivityMonitoredConn and the last Read. Only reads are used
  232. // for this calculation since writes may succeed locally due to buffering.
  233. func (conn *ActivityMonitoredConn) GetActiveDuration() time.Duration {
  234. return time.Duration(atomic.LoadInt64(&conn.lastReadActivityTime) - conn.monotonicStartTime)
  235. }
  236. // GetLastActivityTime returns the arbitrary monotonic time of the last Read.
  237. func (conn *ActivityMonitoredConn) GetLastActivityMonotime() monotime.Time {
  238. return monotime.Time(atomic.LoadInt64(&conn.lastReadActivityTime))
  239. }
  240. func (conn *ActivityMonitoredConn) Read(buffer []byte) (int, error) {
  241. n, err := conn.Conn.Read(buffer)
  242. if err == nil {
  243. if conn.inactivityTimeout > 0 {
  244. err = conn.Conn.SetDeadline(time.Now().Add(conn.inactivityTimeout))
  245. if err != nil {
  246. return n, ContextError(err)
  247. }
  248. }
  249. readActivityTime := int64(monotime.Now())
  250. if conn.activityUpdater != nil {
  251. conn.activityUpdater.UpdateProgress(
  252. int64(n), 0, readActivityTime-atomic.LoadInt64(&conn.lastReadActivityTime))
  253. }
  254. if conn.lruEntry != nil {
  255. conn.lruEntry.Touch()
  256. }
  257. atomic.StoreInt64(&conn.lastReadActivityTime, readActivityTime)
  258. }
  259. // Note: no context error to preserve error type
  260. return n, err
  261. }
  262. func (conn *ActivityMonitoredConn) Write(buffer []byte) (int, error) {
  263. n, err := conn.Conn.Write(buffer)
  264. if err == nil && conn.activeOnWrite {
  265. if conn.inactivityTimeout > 0 {
  266. err = conn.Conn.SetDeadline(time.Now().Add(conn.inactivityTimeout))
  267. if err != nil {
  268. return n, ContextError(err)
  269. }
  270. }
  271. if conn.activityUpdater != nil {
  272. conn.activityUpdater.UpdateProgress(0, int64(n), 0)
  273. }
  274. if conn.lruEntry != nil {
  275. conn.lruEntry.Touch()
  276. }
  277. }
  278. // Note: no context error to preserve error type
  279. return n, err
  280. }
  281. // IsClosed implements the Closer iterface. The return value
  282. // indicates whether the underlying conn has been closed.
  283. func (conn *ActivityMonitoredConn) IsClosed() bool {
  284. closer, ok := conn.Conn.(Closer)
  285. if !ok {
  286. return false
  287. }
  288. return closer.IsClosed()
  289. }