Giter Site home page Giter Site logo

heartbeat's Introduction

Heartbeat

heartbeat is useful when you want to terminate a context-aware long-running task that is not making any progress. If the task duration is variable or unknown, using context.WithTimeout with a fixed value is not ideal. Instead, heartbeat can cancel your task when a certain period passes since the last reported progress.

Examples of the use cases:

  • External processes with progress lines printed to stdout (yt-dlp, ffmpeg, latex, etc.)
  • Network streams or asynchronous API endpoints (e.g. chat completion endpoint in OpenAI.)

Installation

go get -u ytils.dev/heartbeat@latest

Example

Check the documentation for more details.

In the following example we start traceroute(8) and keep it alive as long as it prints something to stdout within a minute:

package main

func main() {
  ctx := context.Background()

  hb := heartbeat.New(ctx, time.Minute, &heartbeat.Options{
    CheckInterval: 10 * time.Second,
    CheckHook: func(timeout, idle, left time.Duration) {
      fmt.Printf("last activity was %s ago\n", idle.Truncate(time.Second))
    },
  })
  defer hb.Close() // to avoid goroutine leaks

  // The command will terminate if the context is canceled
  cmd := exec.CommandContext(hb.Ctx(), "traceroute", "google.com")
  stdout, _ := cmd.StdoutPipe()
  if err := cmd.Start(); err != nil {
    panic(err)
  }

  // Show the output and keep the context alive
  sc := bufio.NewScanner(stdout)
  for sc.Scan() {
    fmt.Println(sc.Text())
    hb.Beat()
  }

  <-hb.Ctx().Done()
  fmt.Println("the context is canceled!")
}

The output should look like this:

 1  192.168.88.1 (192.168.88.1)  2.101 ms  1.774 ms  1.722 ms
2  192.168.1.254 (192.168.1.254)  2.223 ms  2.247 ms  2.119 ms
last activity was 9s ago
3  * * *
last activity was 4s ago
last activity was 14s ago
4  * * *
5  31.55.186.188 (31.55.186.188)  8.967 ms  7.838 ms

<truncated>

lhr25s33-in-f14.1e100.net (142.250.187.206)  8.438 ms
last activity was 4s ago
last activity was 14s ago
last activity was 24s ago
last activity was 34s ago
last activity was 44s ago
last activity was 54s ago
the context is canceled!

heartbeat's People

Contributors

mkuznets avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.