github.com/thanos-io/thanos@v0.32.5/pkg/runutil/example_test.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package runutil_test
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"log"
    10  	"time"
    11  
    12  	"github.com/pkg/errors"
    13  	"github.com/thanos-io/thanos/pkg/runutil"
    14  )
    15  
    16  func ExampleRepeat() {
    17  	// It will stop Repeat 10 seconds later.
    18  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    19  	defer cancel()
    20  
    21  	// It will print out "Repeat" every 5 seconds.
    22  	err := runutil.Repeat(5*time.Second, ctx.Done(), func() error {
    23  		fmt.Println("Repeat")
    24  		return nil
    25  	})
    26  	if err != nil {
    27  		log.Fatal(err)
    28  	}
    29  }
    30  
    31  func ExampleRetry() {
    32  	// It will stop Retry 10 seconds later.
    33  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    34  	defer cancel()
    35  
    36  	// It will print out "Retry" every 5 seconds.
    37  	err := runutil.Retry(5*time.Second, ctx.Done(), func() error {
    38  		fmt.Println("Retry")
    39  		return errors.New("Try to retry")
    40  	})
    41  	if err != nil {
    42  		log.Fatal(err)
    43  	}
    44  }