From 5799d5d1fa4e39280ebafe748e194aafba4cd546 Mon Sep 17 00:00:00 2001 From: Sergey Bevzenko Date: Thu, 7 Mar 2024 08:34:58 +0100 Subject: [PATCH] refactoring; remove ConnectGunConfig{} && small renaming --- components/guns/http/base.go | 2 +- components/guns/http/connect.go | 42 +++++++--------------------- components/guns/http/http.go | 26 ++++------------- components/guns/http/http_test.go | 8 +++--- components/guns/http_scenario/gun.go | 4 +-- components/phttp/import/import.go | 4 +-- 6 files changed, 25 insertions(+), 61 deletions(-) diff --git a/components/guns/http/base.go b/components/guns/http/base.go index afbd438da..2f08ff4bf 100644 --- a/components/guns/http/base.go +++ b/components/guns/http/base.go @@ -85,7 +85,7 @@ type BaseGun struct { var _ Gun = (*BaseGun)(nil) var _ io.Closer = (*BaseGun)(nil) -func (b *BaseGun) WarmUp(opts *warmup.Options) (any, error) { +func (b *BaseGun) WarmUp(_ *warmup.Options) (any, error) { return nil, nil } diff --git a/components/guns/http/connect.go b/components/guns/http/connect.go index 4881c315f..bf1e15030 100644 --- a/components/guns/http/connect.go +++ b/components/guns/http/connect.go @@ -10,19 +10,11 @@ import ( "net/url" "github.com/pkg/errors" - "github.com/yandex/pandora/core/warmup" "github.com/yandex/pandora/lib/netutil" "go.uber.org/zap" ) -type ConnectGunConfig struct { - Base BaseGunConfig `config:",squash"` - Client ClientConfig `config:",squash"` - Target string `validate:"endpoint,required"` - SSL bool // As in HTTP gun, defines scheme for http requests. -} - -func NewConnectGun(cfg ConnectGunConfig, answLog *zap.Logger) *ConnectGun { +func NewConnectGun(cfg HTTPGunConfig, answLog *zap.Logger) *BaseGun { scheme := "http" if cfg.SSL { scheme = "https" @@ -34,33 +26,19 @@ func NewConnectGun(cfg ConnectGunConfig, answLog *zap.Logger) *ConnectGun { hostname: "", targetResolved: cfg.Target, } - var g ConnectGun - g = ConnectGun{ - BaseGun: BaseGun{ - Config: cfg.Base, - OnClose: func() error { - client.CloseIdleConnections() - return nil - }, - AnswLog: answLog, - client: wrappedClient, + return &BaseGun{ + Config: cfg.Base, + OnClose: func() error { + client.CloseIdleConnections() + return nil }, + AnswLog: answLog, + client: wrappedClient, } - return &g -} - -type ConnectGun struct { - BaseGun -} - -var _ Gun = (*ConnectGun)(nil) - -func (g *ConnectGun) WarmUp(opts *warmup.Options) (any, error) { - return nil, nil } -func DefaultConnectGunConfig() ConnectGunConfig { - return ConnectGunConfig{ +func DefaultConnectGunConfig() HTTPGunConfig { + return HTTPGunConfig{ SSL: false, Client: DefaultClientConfig(), Base: DefaultBaseGunConfig(), diff --git a/components/guns/http/http.go b/components/guns/http/http.go index dfbd7417f..6ddab579e 100644 --- a/components/guns/http/http.go +++ b/components/guns/http/http.go @@ -2,7 +2,6 @@ package phttp import ( "github.com/pkg/errors" - "github.com/yandex/pandora/core/warmup" "go.uber.org/zap" ) @@ -13,8 +12,8 @@ type HTTPGunConfig struct { SSL bool } -func NewHTTPGun(conf HTTPGunConfig, answLog *zap.Logger, targetResolved string) *BaseGun { - return NewClientGun(HTTP1ClientConstructor, conf, answLog, targetResolved) +func NewHTTP1Gun(conf HTTPGunConfig, answLog *zap.Logger, targetResolved string) *BaseGun { + return newHTTPGun(HTTP1ClientConstructor, conf, answLog, targetResolved) } var HTTP1ClientConstructor clientConstructor = func(clientConfig ClientConfig, target string) Client { @@ -29,7 +28,7 @@ func NewHTTP2Gun(conf HTTPGunConfig, answLog *zap.Logger, targetResolved string) // Open issue on github if you really need this feature. return nil, errors.New("HTTP/2.0 over TCP is not supported. Please leave SSL option true by default.") } - return NewClientGun(HTTP2ClientConstructor, conf, answLog, targetResolved), nil + return newHTTPGun(HTTP2ClientConstructor, conf, answLog, targetResolved), nil } var HTTP2ClientConstructor clientConstructor = func(clientConfig ClientConfig, target string) Client { @@ -39,7 +38,7 @@ var HTTP2ClientConstructor clientConstructor = func(clientConfig ClientConfig, t return &panicOnHTTP1Client{Client: client} } -func NewClientGun(clientConstructor clientConstructor, cfg HTTPGunConfig, answLog *zap.Logger, targetResolved string) *BaseGun { +func newHTTPGun(clientConstructor clientConstructor, cfg HTTPGunConfig, answLog *zap.Logger, targetResolved string) *BaseGun { scheme := "http" if cfg.SSL { scheme = "https" @@ -51,7 +50,7 @@ func NewClientGun(clientConstructor clientConstructor, cfg HTTPGunConfig, answLo targetResolved: targetResolved, scheme: scheme, } - g := BaseGun{ + return &BaseGun{ Config: cfg.Base, OnClose: func() error { client.CloseIdleConnections() @@ -63,24 +62,11 @@ func NewClientGun(clientConstructor clientConstructor, cfg HTTPGunConfig, answLo targetResolved: targetResolved, client: wrappedClient, } - return &g -} - -type HTTPGun struct { - BaseGun -} - -var _ Gun = (*HTTPGun)(nil) - -func (g *HTTPGun) WarmUp(opts *warmup.Options) (any, error) { - return nil, nil } func DefaultHTTPGunConfig() HTTPGunConfig { return HTTPGunConfig{ - - SSL: false, - + SSL: false, Base: DefaultBaseGunConfig(), Client: DefaultClientConfig(), } diff --git a/components/guns/http/http_test.go b/components/guns/http/http_test.go index 775ffce4f..907801252 100644 --- a/components/guns/http/http_test.go +++ b/components/guns/http/http_test.go @@ -42,7 +42,7 @@ func TestBaseGun_integration(t *testing.T) { conf.Target = host + ":80" targetResolved := strings.TrimPrefix(server.URL, "http://") results := &netsample.TestAggregator{} - httpGun := NewHTTPGun(conf, log, targetResolved) + httpGun := NewHTTP1Gun(conf, log, targetResolved) _ = httpGun.Bind(results, testDeps()) am := newAmmoReq(t, expectedReq) @@ -90,7 +90,7 @@ func TestHTTP(t *testing.T) { conf := DefaultHTTPGunConfig() conf.Target = server.Listener.Addr().String() conf.SSL = tt.https - gun := NewHTTPGun(conf, log, conf.Target) + gun := NewHTTP1Gun(conf, log, conf.Target) var aggr netsample.TestAggregator _ = gun.Bind(&aggr, testDeps()) gun.Shoot(newAmmoURL(t, "/")) @@ -131,7 +131,7 @@ func TestHTTP_Redirect(t *testing.T) { conf := DefaultHTTPGunConfig() conf.Target = server.Listener.Addr().String() conf.Client.Redirect = tt.redirect - gun := NewHTTPGun(conf, log, conf.Target) + gun := NewHTTP1Gun(conf, log, conf.Target) var aggr netsample.TestAggregator _ = gun.Bind(&aggr, testDeps()) gun.Shoot(newAmmoURL(t, "/redirect")) @@ -169,7 +169,7 @@ func TestHTTP_notSupportHTTP2(t *testing.T) { conf := DefaultHTTPGunConfig() conf.Target = server.Listener.Addr().String() conf.SSL = true - gun := NewHTTPGun(conf, log, conf.Target) + gun := NewHTTP1Gun(conf, log, conf.Target) var results netsample.TestAggregator _ = gun.Bind(&results, testDeps()) gun.Shoot(newAmmoURL(t, "/")) diff --git a/components/guns/http_scenario/gun.go b/components/guns/http_scenario/gun.go index 22b689329..be03c71dd 100644 --- a/components/guns/http_scenario/gun.go +++ b/components/guns/http_scenario/gun.go @@ -64,7 +64,7 @@ func (g *BaseGun) Bind(aggregator netsample.Aggregator, deps core.GunDeps) error return nil } -// Shoot is thread safe iff Do and Connect hooks are thread safe. +// Shoot is thread safe if Do and Connect hooks are thread safe. func (g *BaseGun) Shoot(ammo *Scenario) { if g.Aggregator == nil { zap.L().Panic("must bind before shoot") @@ -176,7 +176,7 @@ func (g *BaseGun) shootStep(step Request, sample *netsample.Sample, ammoName str timings, req := g.initTracing(req, sample) - resp, err := g.Do(req) + resp, err := g.client.Do(req) g.saveTrace(timings, sample, resp) diff --git a/components/phttp/import/import.go b/components/phttp/import/import.go index fd43e87ad..d27520f04 100644 --- a/components/phttp/import/import.go +++ b/components/phttp/import/import.go @@ -23,7 +23,7 @@ func Import(fs afero.Fs) { register.Gun("http", func(conf phttp.HTTPGunConfig) func() core.Gun { targetResolved, _ := PreResolveTargetAddr(&conf.Client, conf.Target) answLog := answlog.Init(conf.Base.AnswLog.Path, conf.Base.AnswLog.Enabled) - return func() core.Gun { return phttp.WrapGun(phttp.NewHTTPGun(conf, answLog, targetResolved)) } + return func() core.Gun { return phttp.WrapGun(phttp.NewHTTP1Gun(conf, answLog, targetResolved)) } }, phttp.DefaultHTTPGunConfig) register.Gun("http2", func(conf phttp.HTTPGunConfig) func() (core.Gun, error) { @@ -35,7 +35,7 @@ func Import(fs afero.Fs) { } }, phttp.DefaultHTTP2GunConfig) - register.Gun("connect", func(conf phttp.ConnectGunConfig) func() core.Gun { + register.Gun("connect", func(conf phttp.HTTPGunConfig) func() core.Gun { conf.Target, _ = PreResolveTargetAddr(&conf.Client, conf.Target) answLog := answlog.Init(conf.Base.AnswLog.Path, conf.Base.AnswLog.Enabled) return func() core.Gun {