@@ -18,39 +18,36 @@ import (
18
18
"github.com/creativecreature/pulse/proxy"
19
19
)
20
20
21
- const (
22
- HeartbeatTTL = time .Minute * 10
23
- heartbeatInterval = time .Second * 45
24
- )
25
-
26
21
type Server struct {
27
- name string
28
- activeEditorID string
29
- activeSessions map [string ]* pulse.CodingSession
30
- lastHeartbeat int64
31
- clock pulse.Clock
32
- fileReader FileReader
33
- log * log.Logger
34
- mutex sync.Mutex
35
- storage pulse.TemporaryStorage
22
+ name string
23
+ activeEditorID string
24
+ activeSessions map [string ]* pulse.CodingSession
25
+ lastHeartbeat int64
26
+ stopHeartbeatChecks chan struct {}
27
+ clock pulse.Clock
28
+ fileReader FileReader
29
+ log * log.Logger
30
+ mutex sync.Mutex
31
+ storage pulse.TemporaryStorage
36
32
}
37
33
38
34
// New creates a new server.
39
35
func New (serverName string , opts ... Option ) (* Server , error ) {
40
- a := & Server {
41
- name : serverName ,
42
- activeSessions : make (map [string ]* pulse.CodingSession ),
43
- clock : pulse .NewClock (),
44
- fileReader : filereader .New (),
36
+ s := & Server {
37
+ name : serverName ,
38
+ activeSessions : make (map [string ]* pulse.CodingSession ),
39
+ clock : pulse .NewClock (),
40
+ stopHeartbeatChecks : make (chan struct {}),
41
+ fileReader : filereader .New (),
45
42
}
46
43
for _ , opt := range opts {
47
- err := opt (a )
44
+ err := opt (s )
48
45
if err != nil {
49
46
return & Server {}, err
50
47
}
51
48
}
52
-
53
- return a , nil
49
+ s . startHeartbeatChecks ()
50
+ return s , nil
54
51
}
55
52
56
53
// createSession creates a new session and sets it as the current session.
@@ -81,8 +78,7 @@ func (s *Server) setActiveBuffer(gitFile pulse.GitFile) {
81
78
)
82
79
}
83
80
84
- func (s * Server ) saveAllSessions () {
85
- now := s .clock .GetTime ()
81
+ func (s * Server ) saveAllSessions (endedAt int64 ) {
86
82
s .log .Debug ("Saving all sessions." )
87
83
88
84
for _ , session := range s .activeSessions {
@@ -95,7 +91,7 @@ func (s *Server) saveAllSessions() {
95
91
return
96
92
}
97
93
98
- finishedSession := session .End (now )
94
+ finishedSession := session .End (endedAt )
99
95
err := s .storage .Write (finishedSession )
100
96
if err != nil {
101
97
s .log .Error (err )
@@ -177,20 +173,6 @@ func (s *Server) startServer(port string) (*http.Server, error) {
177
173
return httpServer , nil
178
174
}
179
175
180
- // HeartbeatCheck runs a heartbeat ticker that ensures that
181
- // the current session is not idle for more than ten minutes.
182
- func (s * Server ) HeartbeatCheck () func () {
183
- s .log .Info ("Starting the heartbeat checks." )
184
- ticker , stop := s .clock .NewTicker (heartbeatInterval )
185
- go func () {
186
- for range ticker {
187
- s .CheckHeartbeat ()
188
- }
189
- }()
190
-
191
- return stop
192
- }
193
-
194
176
// Start starts the server on the given port.
195
177
func (s * Server ) Start (port string ) error {
196
178
s .log .Info ("Starting up..." )
@@ -199,9 +181,6 @@ func (s *Server) Start(port string) error {
199
181
return err
200
182
}
201
183
202
- // Start the ECG. It will end inactive sessions.
203
- stopHeartbeat := s .HeartbeatCheck ()
204
-
205
184
// Catch shutdown signals from the OS
206
185
quit := make (chan os.Signal , 1 )
207
186
signal .Notify (quit , syscall .SIGINT , syscall .SIGTERM )
@@ -211,15 +190,15 @@ func (s *Server) Start(port string) error {
211
190
s .log .Info ("Received shutdown signal" , "signal" , sig .String ())
212
191
213
192
// Stop the heartbeat checks and shutdown the http server.
214
- stopHeartbeat ()
193
+ s . stopHeartbeatChecks <- struct {}{}
215
194
err = httpServer .Shutdown (context .Background ())
216
195
if err != nil {
217
196
s .log .Error (err )
218
197
return err
219
198
}
220
199
221
200
// Save the all sessions before shutting down.
222
- s .saveAllSessions ()
201
+ s .saveAllSessions (s . clock . GetTime () )
223
202
s .log .Info ("Shutting down." )
224
203
225
204
return nil
0 commit comments