Skip to content

Commit 10396ec

Browse files
committed
refactor: extract zbuscontext into its own file and refactor relevant perf tasks to use it consistently
1 parent b5dca57 commit 10396ec

6 files changed

Lines changed: 40 additions & 17 deletions

File tree

cmds/modules/noded/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ func action(cli *cli.Context) error {
167167
if err != nil {
168168
return errors.Wrap(err, "failed to create a new perfMon")
169169
}
170-
170+
zcl, err := zbus.NewRedisClient(msgBrokerCon)
171+
if err != nil {
172+
return errors.Wrap(err, "failed to create a zbus client to the msgBroker")
173+
}
174+
ctx = perf.WithZbusClient(ctx, zcl)
171175
healthcheck.RunNTPCheck(ctx)
172176
perfMon.AddTask(iperf.NewTask())
173177
perfMon.AddTask(cpubench.NewTask())

pkg/perf/cpubench/cpubench_task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (c *CPUBenchmarkTask) Run(ctx context.Context) (interface{}, error) {
5959
if err != nil {
6060
return nil, fmt.Errorf("failed to parse cpubench output: %w", err)
6161
}
62-
client := perf.GetZbusClient(ctx)
62+
client := perf.MustGetZbusClient(ctx)
6363
statistics := stubs.NewStatisticsStub(client)
6464

6565
workloads, err := statistics.Workloads(ctx)

pkg/perf/healthcheck/healthcheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (h *healthcheckTask) Run(ctx context.Context) (interface{}, error) {
6363
log.Debug().Msg("starting health check task")
6464
errs := make(map[string][]string)
6565

66-
cl := perf.GetZbusClient(ctx)
66+
cl := perf.MustGetZbusClient(ctx)
6767
zui := stubs.NewZUIStub(cl)
6868

6969
var wg sync.WaitGroup

pkg/perf/monitor.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (pm *PerformanceMonitor) runTask(ctx context.Context, task Task) error {
7878

7979
// Run adds the tasks to the cron queue and start the scheduler
8080
func (pm *PerformanceMonitor) Run(ctx context.Context) error {
81-
ctx = withZbusClient(ctx, pm.zbusClient)
81+
ctx = WithZbusClient(ctx, pm.zbusClient)
8282
for _, task := range pm.tasks {
8383
task := task
8484
if _, err := pm.scheduler.CronWithSeconds(task.Cron()).Do(func() error {
@@ -105,14 +105,3 @@ func (pm *PerformanceMonitor) Run(ctx context.Context) error {
105105
pm.scheduler.StartAsync()
106106
return nil
107107
}
108-
109-
type zbusClient struct{}
110-
111-
func withZbusClient(ctx context.Context, client zbus.Client) context.Context {
112-
return context.WithValue(ctx, zbusClient{}, client)
113-
}
114-
115-
// GetZbusClient gets zbus client from the given context
116-
func GetZbusClient(ctx context.Context) zbus.Client {
117-
return ctx.Value(zbusClient{}).(zbus.Client)
118-
}

pkg/perf/publicip/publicip_task.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (p *publicIPValidationTask) Run(ctx context.Context) (interface{}, error) {
7878
if err != nil {
7979
return nil, fmt.Errorf("failed to get namespace %s: %w", testNamespace, err)
8080
}
81-
cl := perf.GetZbusClient(ctx)
81+
cl := perf.MustGetZbusClient(ctx)
8282
substrateGateway := stubs.NewSubstrateGatewayStub(cl)
8383
farmID := environment.MustGet().FarmID
8484

@@ -191,7 +191,7 @@ func isLeastValidNode(ctx context.Context, farmID uint32, substrateGateway *stub
191191
if err != nil {
192192
return false, fmt.Errorf("failed to get farm %d nodes: %w", farmID, err)
193193
}
194-
cl := perf.GetZbusClient(ctx)
194+
cl := perf.MustGetZbusClient(ctx)
195195
registrar := stubs.NewRegistrarStub(cl)
196196
var nodeID uint32
197197
err = backoff.Retry(func() error {

pkg/perf/zbusctx.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package perf
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/threefoldtech/zbus"
8+
)
9+
10+
// WithZbusClient adds a zbus.Client to the provided context, returning a new context.
11+
// This allows for the retrieval of the client from the context at a later time.
12+
type zbusClientKey struct{}
13+
14+
func WithZbusClient(ctx context.Context, client zbus.Client) context.Context {
15+
return context.WithValue(ctx, zbusClientKey{}, client)
16+
}
17+
18+
// MustGetZbusClient gets zbus client from the given context
19+
func MustGetZbusClient(ctx context.Context) zbus.Client {
20+
return ctx.Value(zbusClientKey{}).(zbus.Client)
21+
}
22+
23+
// TryGetZbusClient tries to get zbus client from the given context
24+
func TryGetZbusClient(ctx context.Context) (zbus.Client, error) {
25+
zcl, ok := ctx.Value(zbusClientKey{}).(zbus.Client)
26+
if !ok {
27+
return zcl, fmt.Errorf("context does not have zbus client")
28+
}
29+
return zcl, nil
30+
}

0 commit comments

Comments
 (0)