Skip to content

Commit

Permalink
fix: worker to cache metrics result for the http service
Browse files Browse the repository at this point in the history
  • Loading branch information
henrywhitaker3 committed Apr 19, 2024
1 parent 4e7162b commit 0f7cce5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
5 changes: 5 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ tasks:
silent: true
cmds:
- go run main.go query

serve:
desc: Run the http server
cmds:
- go run main.go serve
4 changes: 3 additions & 1 deletion cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ func NewServeCommand(app *app.App) *cobra.Command {
cancel()
}()

cache := http.NewResultCache(app)
http := http.NewHttp(app)

go cache.Work(ctx)
go func() {
if err := http.Serve(); err != nil {
if err := http.Start(); err != nil {
if !errors.Is(err, stdhttp.ErrServerClosed) {
fmt.Println(fmt.Errorf("http server failed: %v", err))
cancel()
Expand Down
11 changes: 8 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ type Service struct {
}

type Config struct {
Port int `yaml:"port"`
Services []Service `yaml:"services"`
Prometheus string `yaml:"prometheus"`
Port int `yaml:"port"`
Services []Service `yaml:"services"`
Prometheus string `yaml:"prometheus"`
Refresh time.Duration `yaml:"refresh"`
}

func Load(path string) (*Config, error) {
Expand Down Expand Up @@ -61,6 +62,10 @@ func setDefaults(conf *Config) {
svc.Query.Name = "main"
conf.Services[i] = svc
}

if conf.Refresh == 0 {
conf.Refresh = time.Second * 30
}
}

func (c *Config) Validate() error {
Expand Down
2 changes: 1 addition & 1 deletion internal/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewHttp(app *app.App) *Http {
}
}

func (h *Http) Serve() error {
func (h *Http) Start() error {
return h.e.Start(fmt.Sprintf(":%d", h.app.Config.Port))
}

Expand Down
41 changes: 34 additions & 7 deletions internal/http/results.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package http

import (
"context"
"log"
"sync"
"time"

"github.com/henrywhitaker3/prompage/internal/app"
"github.com/henrywhitaker3/prompage/internal/collector"
"github.com/henrywhitaker3/prompage/internal/config"
"github.com/henrywhitaker3/prompage/internal/querier"
)

type Result struct {
Expand All @@ -14,15 +17,39 @@ type Result struct {
}

type ResultCache struct {
mu *sync.Mutex
querier *querier.Querier
results map[string][]Result
mu *sync.Mutex
collector *collector.Collector
interval time.Duration
results []collector.Result
}

func NewResultCache(app *app.App) *ResultCache {
return &ResultCache{
mu: &sync.Mutex{},
querier: app.Querier,
results: map[string][]Result{},
mu: &sync.Mutex{},
collector: app.Collector,
interval: app.Config.Refresh,
results: []collector.Result{},
}
}

func (c *ResultCache) Work(ctx context.Context) {
c.mu.Lock()
c.results = c.collector.Collect(ctx)
c.mu.Unlock()

ticker := time.NewTicker(c.interval)
defer ticker.Stop()

log.Printf("collecitng metrics every %s", c.interval)

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
c.mu.Lock()
c.results = c.collector.Collect(ctx)
c.mu.Unlock()
}
}
}

0 comments on commit 0f7cce5

Please sign in to comment.