| 1 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module http |
| 5 | |
| 6 | import net |
| 7 | import sync |
| 8 | |
| 9 | @[heap] |
| 10 | struct TlsIdleConnTracker { |
| 11 | mu &sync.Mutex = sync.new_mutex() |
| 12 | mut: |
| 13 | handles []int |
| 14 | closing bool |
| 15 | } |
| 16 | |
| 17 | fn (mut t TlsIdleConnTracker) mark_idle(handle int) bool { |
| 18 | t.mu.lock() |
| 19 | defer { |
| 20 | t.mu.unlock() |
| 21 | } |
| 22 | if t.closing { |
| 23 | return false |
| 24 | } |
| 25 | t.handles << handle |
| 26 | return true |
| 27 | } |
| 28 | |
| 29 | fn (mut t TlsIdleConnTracker) unmark_idle(handle int) { |
| 30 | t.mu.lock() |
| 31 | defer { |
| 32 | t.mu.unlock() |
| 33 | } |
| 34 | idx := t.handles.index(handle) |
| 35 | if idx >= 0 { |
| 36 | t.handles.delete(idx) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | fn (mut t TlsIdleConnTracker) close_idle() { |
| 41 | t.mu.lock() |
| 42 | t.closing = true |
| 43 | handles := t.handles.clone() |
| 44 | t.handles.clear() |
| 45 | t.mu.unlock() |
| 46 | for handle in handles { |
| 47 | net.shutdown(handle) |
| 48 | $if windows { |
| 49 | net.close(handle) or {} |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |