Repository: etcd-io/etcd
File: tests/antithesis/test-template/robustness/traffic/main.go (Go)

Code before:
		defer c.Close()
		traffic.EtcdPutDeleteLease.RunCompactLoop(ctx, c, traffic.DefaultCompactionPeriod, finish)
		mux.Lock()
		reports = append(reports, c.Report())
		mux.Unlock()
	}(compactClient)
	for _, h := range hosts {
		c := connect([]string{h}, ids, baseTime)
		wg.Add(1)
		go func(c *client.RecordingClient, lg *zap.Logger) {
			defer wg.Done()
			defer c.Close()
			runDefragLoop(ctx, lg, c, traffic.DefaultCompactionPeriod, finish)
			mux.Lock()
			reports = append(reports, c.Report())
			mux.Unlock()
		}(c, lg)
	}
	wg.Wait()
	return reports
}

func runDefragLoop(ctx context.Context, lg *zap.Logger, c *client.RecordingClient, period time.Duration, finish <-chan struct{}) {
	for {
		jittered := time.Duration(robustnessrand.RandRange(int64(period-period/2), int64(period+period/2)))
		lg.Info("next defragment run", zap.String("host", c.Endpoints()[0]), zap.Time("at", time.Now().Add(jittered)))
		ticker := time.NewTicker(jittered)
		defer ticker.Stop()

		select {
		case <-ctx.Done():
			return
		case <-finish:
			return
		case <-ticker.C:
		}
		dctx, cancel := context.WithTimeout(ctx, traffic.RequestTimeout)
		_, err := c.Defragment(dctx)
		cancel()
		if err != nil {
			continue
		}
	}
}

func connect(endpoints []string, ids identity.Provider, baseTime time.Time) *client.RecordingClient {
	cli, err := client.NewRecordingClient(endpoints, ids, baseTime)
	if err != nil {
		// Antithesis Assertion: client should always be able to connect to an etcd host
		assert.Unreachable("Client failed to connect to an etcd host", map[string]any{"endpoints": endpoints, "error": err})
		os.Exit(1)

Review comment (suggestion):
```suggestion
```
No need to log this, it will cause too much log volume, defrags should be visible in the report. 

Code after:
		defer c.Close()
		traffic.EtcdPutDeleteLease.RunCompactLoop(ctx, c, traffic.DefaultCompactionPeriod, finish)
		mux.Lock()
		reports = append(reports, c.Report())
		mux.Unlock()
	}(compactClient)
	defragPeriod := traffic.DefaultCompactionPeriod * time.Duration(len(hosts))
	for _, h := range hosts {
		c := connect([]string{h}, ids, baseTime)
		wg.Add(1)
		go func(c *client.RecordingClient) {
			defer wg.Done()
			defer c.Close()
			runDefragLoop(ctx, c, defragPeriod, finish)
			mux.Lock()
			reports = append(reports, c.Report())
			mux.Unlock()
		}(c)
	}
	wg.Wait()
	return reports
}

func runDefragLoop(ctx context.Context, c *client.RecordingClient, period time.Duration, finish <-chan struct{}) {
	jittered := time.Duration(robustnessrand.RandRange(int64(period-period/2), int64(period+period/2)))
	ticker := time.NewTicker(jittered)
	defer ticker.Stop()
	for {
		select {
		case <-ctx.Done():
			return
		case <-finish:
			return
		case <-ticker.C:
		}
		dctx, cancel := context.WithTimeout(ctx, traffic.RequestTimeout)
		_, err := c.Defragment(dctx)
		cancel()
		if err != nil {
			continue
		}
	}
}

func connect(endpoints []string, ids identity.Provider, baseTime time.Time) *client.RecordingClient {
	cli, err := client.NewRecordingClient(endpoints, ids, baseTime)
	if err != nil {
		// Antithesis Assertion: client should always be able to connect to an etcd host
		assert.Unreachable("Client failed to connect to an etcd host", map[string]any{"endpoints": endpoints, "error": err})
		os.Exit(1)
	}