github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/util/updates/updates.go (about)

     1  package updates
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"net/http"
     7  	"sync"
     8  	"time"
     9  
    10  	"github.com/sirupsen/logrus"
    11  )
    12  
    13  var latestVersionStruct struct {
    14  	LatestVersion string `json:"latest_version"`
    15  }
    16  
    17  var runOnce sync.Once
    18  var m sync.RWMutex
    19  
    20  func LatestVersionJSON() string {
    21  	m.RLock()
    22  	defer m.RUnlock()
    23  	b, _ := json.Marshal(latestVersionStruct)
    24  	return string(b)
    25  }
    26  
    27  func updateLatestVersion() error {
    28  	resp, err := http.Get("https://pyroscope.io/latest-version.json")
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	b, err := io.ReadAll(resp.Body)
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	m.Lock()
    39  	defer m.Unlock()
    40  	err = json.Unmarshal(b, &latestVersionStruct)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  func StartVersionUpdateLoop() {
    49  	runOnce.Do(func() {
    50  		go func() {
    51  			for {
    52  				err := updateLatestVersion()
    53  				if err != nil {
    54  					logrus.WithError(err).Warn("failed to get update the latest version")
    55  				}
    56  				time.Sleep(24 * time.Hour)
    57  			}
    58  		}()
    59  	})
    60  }