Skip to content

Commit

Permalink
refactoring; remove ConnectGunConfig{} && small renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
oke11o committed Mar 7, 2024
1 parent 6960b80 commit 5799d5d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 61 deletions.
2 changes: 1 addition & 1 deletion components/guns/http/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
42 changes: 10 additions & 32 deletions components/guns/http/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(),
Expand Down
26 changes: 6 additions & 20 deletions components/guns/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package phttp

import (
"github.com/pkg/errors"
"github.com/yandex/pandora/core/warmup"
"go.uber.org/zap"
)

Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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"
Expand All @@ -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()
Expand All @@ -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(),
}
Expand Down
8 changes: 4 additions & 4 deletions components/guns/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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, "/"))
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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, "/"))
Expand Down
4 changes: 2 additions & 2 deletions components/guns/http_scenario/gun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions components/phttp/import/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit 5799d5d

Please sign in to comment.