Skip to content

Commit

Permalink
Merge pull request #47 from owulveryck/cancelation
Browse files Browse the repository at this point in the history
feat: ticker instead of ticks to avoir leaking
  • Loading branch information
owulveryck authored Jul 4, 2023
2 parents d076761 + cbceffa commit 798ee9b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
24 changes: 12 additions & 12 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ func handleWebSocket(w http.ResponseWriter, r *http.Request) {
defer func() {
<-waitingQueue
}()
// Generate a random integer between 0 and 100
tick := time.Tick(time.Duration(c.Rate) * time.Millisecond) // Create a tick channel that emits a value every 200 milliseconds
//ctx, cancel := context.WithTimeout(r.Context(), 1*time.Hour)
ctx, cancel := context.WithTimeout(r.Context(), 1*time.Hour)
defer cancel()
ticker := time.NewTicker(time.Duration(c.Rate) * time.Millisecond) // Create a tick channel that emits a value every 200 milliseconds
if c.Dev {
tick = time.Tick(2000 * time.Millisecond) // Create a tick channel that emits a value every 200 milliseconds
ticker = time.NewTicker(2000 * time.Millisecond) // Create a tick channel that emits a value every 200 milliseconds
}
timeout := time.Tick(1 * time.Hour)
defer ticker.Stop()

// Create a context with a cancellation function
options := &websocket.AcceptOptions{
Expand Down Expand Up @@ -55,23 +57,21 @@ func handleWebSocket(w http.ResponseWriter, r *http.Request) {

for {
select {
case <-timeout:
case <-ctx.Done():
conn.Close(websocket.StatusNormalClosure, "timeout")
return
case <-r.Context().Done():
return
case <-tick:
ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second)
defer cancel()
case <-ticker.C:
_, err := file.ReadAt(imageData, pointerAddr)
if err != nil {
log.Fatal(err)
}
uint8Array := encodeRLE(imageData)

err = conn.Write(ctx, websocket.MessageBinary, uint8Array)
err = conn.Write(r.Context(), websocket.MessageBinary, uint8Array)
if err != nil {
// log.Println(err)
if websocket.CloseStatus(err) != websocket.StatusNormalClosure {
log.Printf("expected to be disconnected with StatusNormalClosure but got: %v", err)
}
return
}
}
Expand Down
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@
socket.onclose = function(event) {
console.log("WebSocket connection closed with code: " + event.code);
if (event.code == 1000) {
waiting("reMarkable has closed the connexion probably due to timeout");
console.log("normal closure");
waiting("reMarkable has closed the connexion; please refresh");
return;
}
// Retry the connection with exponential backoff
Expand Down

0 comments on commit 798ee9b

Please sign in to comment.