blob: 99a644acf62b8739665bcd704f76838d50f38933 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
package server
import (
"context"
"fmt"
"sync"
"time"
"github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/io/dlog"
)
// Used to collect and display various server stats.
type stats struct {
mutex sync.Mutex
currentConnections int
lifetimeConnections uint64
}
func (s *stats) incrementConnections() {
defer s.logServerStats()
s.mutex.Lock()
s.currentConnections++
s.lifetimeConnections++
s.mutex.Unlock()
}
func (s *stats) decrementConnections() {
defer s.logServerStats()
s.mutex.Lock()
s.currentConnections--
s.mutex.Unlock()
}
func (s *stats) hasConnections() bool {
s.mutex.Lock()
currentConnections := s.currentConnections
s.mutex.Unlock()
has := currentConnections > 0
dlog.Server.Info("stats", "Server with open connections?",
has, currentConnections)
return has
}
func (s *stats) logServerStats() {
s.mutex.Lock()
defer s.mutex.Unlock()
data := make(map[string]interface{})
data["currentConnections"] = s.currentConnections
data["lifetimeConnections"] = s.lifetimeConnections
dlog.Server.Mapreduce("STATS", data)
}
func (s *stats) serverLimitExceeded() error {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.currentConnections >= config.Server.MaxConnections {
return fmt.Errorf("Exceeded max allowed concurrent connections of %d",
config.Server.MaxConnections)
}
return nil
}
func (s *stats) start(ctx context.Context) {
for {
select {
case <-time.NewTimer(time.Second * 10).C:
s.logServerStats()
case <-ctx.Done():
return
}
}
}
func (s *stats) waitForConnections() {
for {
select {
case <-time.NewTimer(time.Second).C:
if !s.hasConnections() {
return
}
}
}
}
|