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

Code before:
		tf:                 tf,
		reporting:          reporter{&wg, &mux, &reports},
		ids:                ids,
		baseTime:           baseTime,
		finish:             closeAfter(ctx, duration),
		storage:            identity.NewLeaseIDStorage(),
		limiter:            rate.NewLimiter(rate.Limit(profile.MaximalQPS), profile.BurstableQPS),
		concurrencyLimiter: traffic.NewConcurrencyLimiter(profile.MaxNonUniqueRequestConcurrency),
		keyStore:           traffic.NewKeyStore(10, "key"),
	}

	sim.runSingleMemberTraffic(ctx)
	sim.runAllMemberTraffic(ctx)
	sim.runCompaction(ctx)
	sim.runDefragment(ctx)
	wg.Wait()
	return reports
}

type simulator struct {
	hosts              []string
	tf                 traffic.Traffic
	reporting          reporter
	ids                identity.Provider
	baseTime           time.Time
	finish             <-chan struct{}
	storage            identity.LeaseIDStorage
	limiter            *rate.Limiter
	concurrencyLimiter traffic.ConcurrencyLimiter
	keyStore           *traffic.KeyStore
}

func (s *simulator) runSingleMemberTraffic(ctx context.Context) {
	for i := range profile.MemberClientCount {
		c := connect([]string{s.hosts[i%len(s.hosts)]}, s.ids, s.baseTime)
		go s.reporting.makeReportOf(trafficLoad(ctx, s.tf, s.limiter, s.ids, s.storage, s.concurrencyLimiter, s.keyStore, s.finish)).usingClient(c)
	}
}

func (s *simulator) runAllMemberTraffic(ctx context.Context) {
	for range profile.ClusterClientCount {
		c := connect(s.hosts, s.ids, s.baseTime)
		go s.reporting.makeReportOf(trafficLoad(ctx, s.tf, s.limiter, s.ids, s.storage, s.concurrencyLimiter, s.keyStore, s.finish)).usingClient(c)
	}
}

func (s *simulator) runCompaction(ctx context.Context) {
	compactClient := connect(s.hosts, s.ids, s.baseTime)
	go s.reporting.makeReportOf(compaction(ctx, s.tf, traffic.DefaultCompactionPeriod, s.finish)).usingClient(compactClient)
}


Review comment (suggestion):
You should watch out when creating struct based on collection of argument variables, not all variables are equal. 
Don't think `finish` make sense here. It's more like context, and should be passed like it. 

Code after:
	storage := identity.NewLeaseIDStorage()
	limiter := rate.NewLimiter(rate.Limit(profile.MaximalQPS), profile.BurstableQPS)
	concurrencyLimiter := traffic.NewConcurrencyLimiter(profile.MaxNonUniqueRequestConcurrency)
	finish := closeAfter(ctx, duration)
	keyStore := traffic.NewKeyStore(10, "key")
	for i := range profile.MemberClientCount {
		c := connect(clientSet, []string{hosts[i%len(hosts)]})
		wg.Add(1)
		go func(c *client.RecordingClient) {
			defer wg.Done()
			defer c.Close()
			tf.RunTrafficLoop(ctx, c, limiter,
				clientSet.IdentityProvider(),
				storage,
				concurrencyLimiter,
				keyStore,
				finish,
			)
		}(c)
	}
	for range profile.ClusterClientCount {
		c := connect(clientSet, hosts)
		wg.Add(1)
		go func(c *client.RecordingClient) {
			defer wg.Done()
			defer c.Close()
			tf.RunTrafficLoop(ctx, c, limiter,
				clientSet.IdentityProvider(),
				storage,
				concurrencyLimiter,
				keyStore,
				finish,
			)
		}(c)
	}
	wg.Add(1)
	compactClient := connect(clientSet, hosts)
	go func(c *client.RecordingClient) {
		defer wg.Done()
		defer c.Close()
		tf.RunCompactLoop(ctx, c, traffic.DefaultCompactionPeriod, finish)
	}(compactClient)
	defragPeriod := traffic.DefaultCompactionPeriod * time.Duration(len(hosts))
	for _, h := range hosts {
		c := connect(clientSet, []string{h})
		wg.Add(1)
		go func(c *client.RecordingClient) {
			defer wg.Done()
			defer c.Close()
			runDefragLoop(ctx, c, defragPeriod, finish)
		}(c)