Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graceful shutdown #108

Merged
merged 4 commits into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions daemon/algod/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ func (s *Server) Stop() {
// Attempt to log a shutdown event before we exit...
s.log.Event(telemetryspec.ApplicationState, telemetryspec.ShutdownEvent)

s.node.Stop()

err := server.Shutdown(context.Background())
if err != nil {
s.log.Error(err)
Expand Down
17 changes: 10 additions & 7 deletions network/wsNetwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ func (nc *nopConn) NextReader() (int, io.Reader, error) {
func (nc *nopConn) WriteMessage(int, []byte) error {
return nil
}
func (nc *nopConn) WriteControl(int, []byte, time.Time) error {
return nil
}
func (nc *nopConn) SetReadLimit(limit int64) {
}
func (nc *nopConn) CloseWithoutFlush() error {
Expand Down Expand Up @@ -1061,14 +1064,14 @@ func TestWebsocketNetworkManyIdle(t *testing.T) {
// TODO: test funcion when some message handler is slow?

func TestWebsocketNetwork_updateUrlHost(t *testing.T) {
tlog := logging.TestingLog(t)
type fields struct {
listener net.Listener
server http.Server
router *mux.Router
scheme string
upgrader websocket.Upgrader
config config.Local
log logging.Logger
readBuffer chan IncomingMessage
wg sync.WaitGroup
handlers Multiplexer
Expand Down Expand Up @@ -1099,11 +1102,11 @@ func TestWebsocketNetwork_updateUrlHost(t *testing.T) {
originalAddress string
host string
}
testFields1 := fields{log: logging.NewLogger()}
testFields2 := fields{log: logging.NewLogger()}
testFields3 := fields{log: logging.NewLogger()}
testFields4 := fields{log: logging.NewLogger()}
testFields5 := fields{log: logging.NewLogger()}
testFields1 := fields{}
testFields2 := fields{}
testFields3 := fields{}
testFields4 := fields{}
testFields5 := fields{}

tests := []struct {
name string
Expand Down Expand Up @@ -1164,7 +1167,7 @@ func TestWebsocketNetwork_updateUrlHost(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wn := &WebsocketNetwork{
log: tt.fields.log,
log: tlog,
}
gotNewAddress, err := wn.updateURLHost(tt.args.originalAddress, net.ParseIP(tt.args.host))
if (err != nil) != tt.wantErr {
Expand Down
11 changes: 11 additions & 0 deletions network/wsPeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type wsPeerWebsocketConn interface {
RemoteAddr() net.Addr
NextReader() (int, io.Reader, error)
WriteMessage(int, []byte) error
WriteControl(int, []byte, time.Time) error
SetReadLimit(int64)
CloseWithoutFlush() error
}
Expand Down Expand Up @@ -258,6 +259,15 @@ func (wp *wsPeer) readLoop() {
msg := IncomingMessage{}
mtype, reader, err := wp.conn.NextReader()
if err != nil {
if ce, ok := err.(*websocket.CloseError); ok {
switch ce.Code {
case websocket.CloseNormalClosure, websocket.CloseGoingAway:
// deliberate close, no error
return
default:
// fall through to reportReadErr
}
}
wp.reportReadErr(err)
return
}
Expand Down Expand Up @@ -476,6 +486,7 @@ func (wp *wsPeer) Close() {
atomic.StoreInt32(&wp.didSignalClose, 1)
if atomic.CompareAndSwapInt32(&wp.didInnerClose, 0, 1) {
close(wp.closing)
wp.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(5*time.Second))
Copy link
Contributor

@tsachiherman tsachiherman Jul 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to have this function receive a context, and to have the WriteControl respect that ( somehow ).
We don't want to have a "stuck" endpoint to hang our shutdown process.

sorry - I know it's not easy to implement ;-(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has a deadline (now + 5 seconds). How this is called in wsNetwork.go will start a thread per peer, so they'll all wait those 5 seconds in parallel and so at most 5 seconds after WebsocketNetwork.Close() is called it should be done.
I agree in principle, it would be neat to upgrade all of websocket to Write and Read with context. Can we punt for a separate soonish TODO issue?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course. This change is a great improvement on its own and shouldn't be deferred.
We can make it even better tomorrow ;-)

wp.conn.CloseWithoutFlush()
}
}
Expand Down
42 changes: 16 additions & 26 deletions vendor/github.com/algorand/websocket/conn.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/algorand/websocket/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/github.com/algorand/websocket/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions vendor/github.com/algorand/websocket/join.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.