Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
sfu
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alain Takoudjou
sfu
Commits
e37e2703
Commit
e37e2703
authored
Sep 12, 2020
by
Juliusz Chroboczek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement graceful server shutdown.
This gives the user a friendly message when the server shuts down.
parent
7707775c
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
41 additions
and
7 deletions
+41
-7
client.go
client.go
+4
-0
group.go
group.go
+11
-1
sfu.go
sfu.go
+2
-1
webclient.go
webclient.go
+7
-3
webserver.go
webserver.go
+17
-2
No files found.
client.go
View file @
e37e2703
...
...
@@ -12,3 +12,7 @@ type client interface {
pushConn
(
id
string
,
conn
upConnection
,
tracks
[]
upTrack
,
label
string
)
error
pushClient
(
id
,
username
string
,
add
bool
)
error
}
type
kickable
interface
{
kick
(
message
string
)
error
}
group.go
View file @
e37e2703
...
...
@@ -64,7 +64,7 @@ type connectionFailedAction struct {
type
permissionsChangedAction
struct
{}
type
kickAction
struct
{
type
kickAction
struct
{
message
string
}
...
...
@@ -288,6 +288,16 @@ func (g *group) Range(f func(c client) bool) {
}
}
func
(
g
*
group
)
shutdown
(
message
string
)
{
g
.
Range
(
func
(
c
client
)
bool
{
cc
,
ok
:=
c
.
(
kickable
)
if
ok
{
cc
.
kick
(
message
)
}
return
true
})
}
const
maxChatHistory
=
20
func
(
g
*
group
)
clearChatHistory
()
{
...
...
sfu.go
View file @
e37e2703
...
...
@@ -87,6 +87,7 @@ func main() {
webserver
()
terminate
:=
make
(
chan
os
.
Signal
,
1
)
signal
.
Notify
(
terminate
,
syscall
.
SIGINT
)
signal
.
Notify
(
terminate
,
syscall
.
SIGINT
,
syscall
.
SIGTERM
)
<-
terminate
shutdown
()
}
webclient.go
View file @
e37e2703
...
...
@@ -943,6 +943,10 @@ func setPermissions(g *group, id string, perm string) error {
return
c
.
action
(
permissionsChangedAction
{})
}
func
(
c
*
webClient
)
kick
(
message
string
)
error
{
return
c
.
action
(
kickAction
{
message
})
}
func
kickClient
(
g
*
group
,
id
string
,
message
string
)
error
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
...
...
@@ -952,12 +956,12 @@ func kickClient(g *group, id string, message string) error {
return
userError
(
"no such user"
)
}
c
,
ok
:=
client
.
(
*
webClient
)
c
,
ok
:=
client
.
(
kickable
)
if
!
ok
{
return
userError
(
"this
is not a real user
"
)
return
userError
(
"this
client is not kickable
"
)
}
return
c
.
action
(
kickAction
{
message
}
)
return
c
.
kick
(
message
)
}
func
handleClientMessage
(
c
*
webClient
,
m
clientMessage
)
error
{
...
...
webserver.go
View file @
e37e2703
...
...
@@ -2,6 +2,7 @@ package main
import
(
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
...
...
@@ -19,6 +20,8 @@ import (
"github.com/gorilla/websocket"
)
var
server
*
http
.
Server
func
webserver
()
{
http
.
Handle
(
"/"
,
mungeHandler
{
http
.
FileServer
(
http
.
Dir
(
staticRoot
))})
http
.
HandleFunc
(
"/group/"
,
groupHandler
)
...
...
@@ -39,17 +42,24 @@ func webserver() {
http
.
HandleFunc
(
"/stats"
,
statsHandler
)
go
func
()
{
server
:
=
&
http
.
Server
{
server
=
&
http
.
Server
{
Addr
:
httpAddr
,
ReadHeaderTimeout
:
60
*
time
.
Second
,
IdleTimeout
:
120
*
time
.
Second
,
}
server
.
RegisterOnShutdown
(
func
()
{
groups
.
mu
.
Lock
()
defer
groups
.
mu
.
Unlock
()
for
_
,
g
:=
range
groups
.
groups
{
go
g
.
shutdown
(
"server is shutting down"
)
}
})
var
err
error
err
=
server
.
ListenAndServeTLS
(
filepath
.
Join
(
dataDir
,
"cert.pem"
),
filepath
.
Join
(
dataDir
,
"key.pem"
),
)
if
err
!=
nil
{
if
err
!=
nil
&&
err
!=
http
.
ErrServerClosed
{
log
.
Printf
(
"ListenAndServeTLS: %v"
,
err
)
}
}()
...
...
@@ -457,3 +467,8 @@ func serveGroupRecordings(w http.ResponseWriter, r *http.Request, f *os.File, gr
fmt
.
Fprintf
(
w
,
"</table>
\n
"
)
fmt
.
Fprintf
(
w
,
"</body></html>
\n
"
)
}
func
shutdown
()
{
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
2
*
time
.
Second
)
server
.
Shutdown
(
ctx
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment