Skip to content

Commit

Permalink
more granular end connection logging
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhawkins committed Jul 16, 2019
1 parent 99abff1 commit 60b9767
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 23 deletions.
37 changes: 15 additions & 22 deletions app.go
Expand Up @@ -151,7 +151,7 @@ func (a *App) run(ctx context.Context) error {
a.nextPartner = nextPartner
a.cancelMu.Unlock()

skipReason, err := a.connect(connCtx)
endReason, err := a.connect(connCtx)
// HACK(maxhawkins): these errors get returned when the context passed
// into match is canceled, so we ignore them. There's probably a more elegant
// way to close the websocket without all this error munging.
Expand All @@ -178,12 +178,7 @@ func (a *App) run(ctx context.Context) error {
}
}

if skipReason != "" {
a.renderer.Dispatch(ui.LogEvent{
Level: ui.LogLevelInfo,
Text: skipReason,
})
}
a.renderer.Dispatch(ui.ConnEndedEvent{endReason})
a.renderer.Dispatch(ui.FrameEvent(nil))

time.Sleep(100 * time.Millisecond)
Expand Down Expand Up @@ -284,7 +279,7 @@ func (a *App) checkConnection(ctx context.Context) error {
}
}

func (a *App) connect(ctx context.Context) (endReason string, err error) {
func (a *App) connect(ctx context.Context) (ui.EndConnReason, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

Expand All @@ -295,18 +290,18 @@ func (a *App) connect(ctx context.Context) (endReason string, err error) {
connectTimeout.Stop()

if err := a.checkConnection(ctx); err != nil {
return "", err
return ui.EndConnSetupError, err
}

ended := make(chan string)
ended := make(chan ui.EndConnReason)

conn, err := NewConn(webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{URLs: []string{fmt.Sprintf("stun:%s", a.STUNServer)}},
},
})
if err != nil {
return "", err
return ui.EndConnSetupError, err
}
a.conn = conn

Expand All @@ -327,7 +322,7 @@ func (a *App) connect(ctx context.Context) (endReason string, err error) {

conn.OnBye = func() {
a.renderer.Dispatch(ui.FrameEvent(nil))
ended <- "Partner left"
ended <- ui.EndConnGone
}
conn.OnMessage = func(s string) {
a.renderer.Dispatch(ui.ReceivedChatEvent(s))
Expand All @@ -346,7 +341,7 @@ func (a *App) connect(ctx context.Context) (endReason string, err error) {
})

case webrtc.ICEConnectionStateFailed:
ended <- "Lost connection"
ended <- ui.EndConnDisconnected
}
}
conn.OnDataOpen = func() {
Expand All @@ -357,7 +352,7 @@ func (a *App) connect(ctx context.Context) (endReason string, err error) {

dec, err := vpx.NewDecoder(320, 240)
if err != nil {
return "", err
return ui.EndConnSetupError, err
}
conn.OnFrame = func(frame []byte) {
frameTimeout.Reset(5 * time.Second)
Expand All @@ -381,10 +376,10 @@ func (a *App) connect(ctx context.Context) (endReason string, err error) {

err = Match(ctx, a.signalerURL, conn.pc)
if err == errMatchFailed {
return "", nil
return ui.EndConnMatchError, nil
}
if err != nil {
return "", err
return ui.EndConnMatchError, err
}

connectTimeout.Reset(10 * time.Second)
Expand All @@ -394,20 +389,18 @@ func (a *App) connect(ctx context.Context) (endReason string, err error) {
Text: "Found match. Connecting...",
})

var reason string
var reason ui.EndConnReason
select {
case <-ctx.Done():
reason = ""
reason = ui.EndConnNormal
case <-connectTimeout.C:
reason = "Connection timed out"
reason = ui.EndConnTimedOut
case <-frameTimeout.C:
reason = "Lost connection"
reason = ui.EndConnDisconnected
case r := <-ended:
reason = r
}

a.renderer.Dispatch(ui.ConnEndedEvent{reason})

return reason, nil
}

Expand Down
2 changes: 1 addition & 1 deletion ui/events.go
Expand Up @@ -44,7 +44,7 @@ type ConnStartedEvent struct{}
// ConnEndedEvent fires when the connection with your partner has been lost
type ConnEndedEvent struct {
// The reason for the disconnection
Reason string
Reason EndConnReason
}

// SetPageEvent transitions to the specified page
Expand Down
40 changes: 40 additions & 0 deletions ui/reducer.go
Expand Up @@ -143,6 +143,46 @@ func messagesReducer(s []Message, event Event) []Message {
Text: string(e),
})

case ConnEndedEvent:
var msg Message

switch e.Reason {

// Error handler will catch this
case EndConnSetupError:
return s

// We ignore match errors
case EndConnMatchError:
return s

case EndConnNormal:
msg = Message{
Type: MessageTypeInfo,
Text: "Skipping...",
}

case EndConnTimedOut:
msg = Message{
Type: MessageTypeError,
Text: "Connection timed out.",
}

case EndConnDisconnected:
msg = Message{
Type: MessageTypeError,
Text: "Lost connection.",
}

case EndConnGone:
msg = Message{
Type: MessageTypeInfo,
Text: "Your partner left the chat.",
}
}

return append(s, msg)

case ConnStartedEvent:
return append(s, Message{
Type: MessageTypeInfo,
Expand Down
17 changes: 17 additions & 0 deletions ui/state.go
Expand Up @@ -42,3 +42,20 @@ type Message struct {
User string
Text string
}

type EndConnReason int

const (
// User closed connection
EndConnNormal EndConnReason = iota
// Error during connection setup
EndConnSetupError
// Error during matching
EndConnMatchError
// Connection timed out
EndConnTimedOut
// Lost connection with partner
EndConnDisconnected
// Partner left
EndConnGone
)

0 comments on commit 60b9767

Please sign in to comment.