summaryrefslogtreecommitdiff
path: root/pkg/api/server/idletracker/idletracker.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2020-07-23 11:04:48 +0200
committerGitHub <noreply@github.com>2020-07-23 11:04:48 +0200
commitdade9159e7a0f848e101221274faa3f4f03cf132 (patch)
tree4fc8aa7a9379260c103d86c37cfc0a2ad511a9e9 /pkg/api/server/idletracker/idletracker.go
parent2d24487ba244e5cd900f6aecc5d8896e1354d1ee (diff)
parent2faeb2189f81b6925d80aa8031cb5b19aa8618cb (diff)
downloadpodman-dade9159e7a0f848e101221274faa3f4f03cf132.tar.gz
podman-dade9159e7a0f848e101221274faa3f4f03cf132.tar.bz2
podman-dade9159e7a0f848e101221274faa3f4f03cf132.zip
Merge pull request #7054 from mheon/backports_203
Backports and Release Notes for v2.0.3
Diffstat (limited to 'pkg/api/server/idletracker/idletracker.go')
-rw-r--r--pkg/api/server/idletracker/idletracker.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/pkg/api/server/idletracker/idletracker.go b/pkg/api/server/idletracker/idletracker.go
new file mode 100644
index 000000000..1ee905a99
--- /dev/null
+++ b/pkg/api/server/idletracker/idletracker.go
@@ -0,0 +1,74 @@
+package idletracker
+
+import (
+ "net"
+ "net/http"
+ "sync"
+ "time"
+
+ "github.com/sirupsen/logrus"
+)
+
+type IdleTracker struct {
+ http map[net.Conn]struct{}
+ hijacked int
+ total int
+ mux sync.Mutex
+ timer *time.Timer
+ Duration time.Duration
+}
+
+func NewIdleTracker(idle time.Duration) *IdleTracker {
+ return &IdleTracker{
+ http: make(map[net.Conn]struct{}),
+ Duration: idle,
+ timer: time.NewTimer(idle),
+ }
+}
+
+func (t *IdleTracker) ConnState(conn net.Conn, state http.ConnState) {
+ t.mux.Lock()
+ defer t.mux.Unlock()
+
+ oldActive := t.ActiveConnections()
+ logrus.Debugf("IdleTracker %p:%v %d/%d connection(s)", conn, state, oldActive, t.TotalConnections())
+ switch state {
+ case http.StateNew, http.StateActive:
+ t.http[conn] = struct{}{}
+ // stop the timer if we transitioned from idle
+ if oldActive == 0 {
+ t.timer.Stop()
+ }
+ t.total++
+ case http.StateHijacked:
+ // hijacked connections are handled elsewhere
+ delete(t.http, conn)
+ t.hijacked++
+ case http.StateIdle, http.StateClosed:
+ delete(t.http, conn)
+ // Restart the timer if we've become idle
+ if oldActive > 0 && len(t.http) == 0 {
+ t.timer.Stop()
+ t.timer.Reset(t.Duration)
+ }
+ }
+}
+
+func (t *IdleTracker) TrackHijackedClosed() {
+ t.mux.Lock()
+ defer t.mux.Unlock()
+
+ t.hijacked--
+}
+
+func (t *IdleTracker) ActiveConnections() int {
+ return len(t.http) + t.hijacked
+}
+
+func (t *IdleTracker) TotalConnections() int {
+ return t.total
+}
+
+func (t *IdleTracker) Done() <-chan time.Time {
+ return t.timer.C
+}