v / vlib / net / http / server_tls_idle.v
52 lines · 47 sloc · 900 bytes · 1d1d649202779cca7b80d5a47268ca7bb6bc3c69
Raw
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.
4module http
5
6import net
7import sync
8
9@[heap]
10struct TlsIdleConnTracker {
11 mu &sync.Mutex = sync.new_mutex()
12mut:
13 handles []int
14 closing bool
15}
16
17fn (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
29fn (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
40fn (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