Skip to content

Commit

Permalink
refactoring; remove GunConfig{}. Use only HTTPGunConfig{}
Browse files Browse the repository at this point in the history
  • Loading branch information
oke11o committed Mar 6, 2024
1 parent 6335f8d commit a34bcde
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 80 deletions.
12 changes: 6 additions & 6 deletions components/guns/http/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
)

type ConnectGunConfig struct {
Target string `validate:"endpoint,required"`
SSL bool // As in HTTP gun, defines scheme for http requests.
Client ClientConfig `config:",squash"`
BaseGunConfig `config:",squash"`
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(conf ConnectGunConfig, answLog *zap.Logger) *ConnectGun {
Expand All @@ -31,7 +31,7 @@ func NewConnectGun(conf ConnectGunConfig, answLog *zap.Logger) *ConnectGun {
var g ConnectGun
g = ConnectGun{
BaseGun: BaseGun{
Config: conf.BaseGunConfig,
Config: conf.Base,
Do: g.Do,
OnClose: func() error {
client.CloseIdleConnections()
Expand Down Expand Up @@ -68,7 +68,7 @@ func DefaultConnectGunConfig() ConnectGunConfig {
}
}

func newConnectClient(conf ClientConfig, target string) Client {
var newConnectClient clientConstructor = func(conf ClientConfig, target string) Client {
transport := NewTransport(
conf.Transport,
newConnectDialFunc(
Expand Down
60 changes: 23 additions & 37 deletions components/guns/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,49 @@ import (
"go.uber.org/zap"
)

type GunConfig struct {
Target string `validate:"endpoint,required"`
SSL bool
Base BaseGunConfig `config:",squash"`
}

type HTTPGunConfig struct {
Gun GunConfig `config:",squash"`
Client ClientConfig `config:",squash"`
}

type HTTP2GunConfig struct {
Gun GunConfig `config:",squash"`
Client ClientConfig `config:",squash"`
Base BaseGunConfig `config:",squash"`
Client ClientConfig `config:",squash"`
Target string `validate:"endpoint,required"`
SSL bool
}

func NewHTTPGun(conf HTTPGunConfig, answLog *zap.Logger, targetResolved string) *HTTPGun {
return NewClientGun(HTTP1ClientConstructor, conf.Client, conf.Gun, answLog, targetResolved)
return NewClientGun(HTTP1ClientConstructor, conf, answLog, targetResolved)
}

func HTTP1ClientConstructor(clientConfig ClientConfig, target string) Client {
var HTTP1ClientConstructor clientConstructor = func(clientConfig ClientConfig, target string) Client {
transport := NewTransport(clientConfig.Transport, NewDialer(clientConfig.Dialer).DialContext, target)
client := newClient(transport, clientConfig.Redirect)
return client
}

// NewHTTP2Gun return simple HTTP/2 gun that can shoot sequentially through one connection.
func NewHTTP2Gun(conf HTTP2GunConfig, answLog *zap.Logger, targetResolved string) (*HTTPGun, error) {
if !conf.Gun.SSL {
func NewHTTP2Gun(conf HTTPGunConfig, answLog *zap.Logger, targetResolved string) (*HTTPGun, error) {
if !conf.SSL {
// 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.Client, conf.Gun, answLog, targetResolved), nil
return NewClientGun(HTTP2ClientConstructor, conf, answLog, targetResolved), nil
}

func HTTP2ClientConstructor(clientConfig ClientConfig, target string) Client {
var HTTP2ClientConstructor clientConstructor = func(clientConfig ClientConfig, target string) Client {
transport := NewHTTP2Transport(clientConfig.Transport, NewDialer(clientConfig.Dialer).DialContext, target)
client := newClient(transport, clientConfig.Redirect)
// Will panic and cancel shooting whet target doesn't support HTTP/2.
return &panicOnHTTP1Client{Client: client}
}

func NewClientGun(clientConstructor clientConstructor, clientCfg ClientConfig, gunCfg GunConfig, answLog *zap.Logger, targetResolved string) *HTTPGun {
client := clientConstructor(clientCfg, gunCfg.Target)
func NewClientGun(clientConstructor clientConstructor, cfg HTTPGunConfig, answLog *zap.Logger, targetResolved string) *HTTPGun {
client := clientConstructor(cfg.Client, cfg.Target)
scheme := "http"
if gunCfg.SSL {
if cfg.SSL {
scheme = "https"
}
var g HTTPGun
g = HTTPGun{
BaseGun: BaseGun{
Config: gunCfg.Base,
Config: cfg.Base,
Do: g.Do,
OnClose: func() error {
client.CloseIdleConnections()
Expand All @@ -68,7 +59,7 @@ func NewClientGun(clientConstructor clientConstructor, clientCfg ClientConfig, g
AnswLog: answLog,

scheme: scheme,
hostname: getHostWithoutPort(gunCfg.Target),
hostname: getHostWithoutPort(cfg.Target),
targetResolved: targetResolved,
client: client,
},
Expand Down Expand Up @@ -98,23 +89,18 @@ func (g *HTTPGun) Do(req *http.Request) (*http.Response, error) {

func DefaultHTTPGunConfig() HTTPGunConfig {
return HTTPGunConfig{
Gun: DefaultClientGunConfig(),
Client: DefaultClientConfig(),
}
}

func DefaultHTTP2GunConfig() HTTP2GunConfig {
conf := HTTP2GunConfig{
SSL: false,

Base: DefaultBaseGunConfig(),
Client: DefaultClientConfig(),
Gun: DefaultClientGunConfig(),
}
conf.Gun.SSL = true
return conf
}

func DefaultClientGunConfig() GunConfig {
return GunConfig{
SSL: false,
Base: DefaultBaseGunConfig(),
func DefaultHTTP2GunConfig() HTTPGunConfig {
return HTTPGunConfig{
Client: DefaultClientConfig(),
Base: DefaultBaseGunConfig(),
SSL: true,
}
}
32 changes: 16 additions & 16 deletions components/guns/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestBaseGun_integration(t *testing.T) {
defer server.Close()
log := zap.NewNop()
conf := DefaultHTTPGunConfig()
conf.Gun.Target = host + ":80"
conf.Target = host + ":80"
targetResolved := strings.TrimPrefix(server.URL, "http://")
results := &netsample.TestAggregator{}
httpGun := NewHTTPGun(conf, log, targetResolved)
Expand Down Expand Up @@ -88,9 +88,9 @@ func TestHTTP(t *testing.T) {
defer server.Close()
log := zap.NewNop()
conf := DefaultHTTPGunConfig()
conf.Gun.Target = server.Listener.Addr().String()
conf.Gun.SSL = tt.https
gun := NewHTTPGun(conf, log, conf.Gun.Target)
conf.Target = server.Listener.Addr().String()
conf.SSL = tt.https
gun := NewHTTPGun(conf, log, conf.Target)
var aggr netsample.TestAggregator
_ = gun.Bind(&aggr, testDeps())
gun.Shoot(newAmmoURL(t, "/"))
Expand Down Expand Up @@ -129,9 +129,9 @@ func TestHTTP_Redirect(t *testing.T) {
defer server.Close()
log := zap.NewNop()
conf := DefaultHTTPGunConfig()
conf.Gun.Target = server.Listener.Addr().String()
conf.Target = server.Listener.Addr().String()
conf.Client.Redirect = tt.redirect
gun := NewHTTPGun(conf, log, conf.Gun.Target)
gun := NewHTTPGun(conf, log, conf.Target)
var aggr netsample.TestAggregator
_ = gun.Bind(&aggr, testDeps())
gun.Shoot(newAmmoURL(t, "/redirect"))
Expand Down Expand Up @@ -167,9 +167,9 @@ func TestHTTP_notSupportHTTP2(t *testing.T) {

log := zap.NewNop()
conf := DefaultHTTPGunConfig()
conf.Gun.Target = server.Listener.Addr().String()
conf.Gun.SSL = true
gun := NewHTTPGun(conf, log, conf.Gun.Target)
conf.Target = server.Listener.Addr().String()
conf.SSL = true
gun := NewHTTPGun(conf, log, conf.Target)
var results netsample.TestAggregator
_ = gun.Bind(&results, testDeps())
gun.Shoot(newAmmoURL(t, "/"))
Expand All @@ -190,8 +190,8 @@ func TestHTTP2(t *testing.T) {
defer server.Close()
log := zap.NewNop()
conf := DefaultHTTP2GunConfig()
conf.Gun.Target = server.Listener.Addr().String()
gun, _ := NewHTTP2Gun(conf, log, conf.Gun.Target)
conf.Target = server.Listener.Addr().String()
gun, _ := NewHTTP2Gun(conf, log, conf.Target)
var results netsample.TestAggregator
_ = gun.Bind(&results, testDeps())
gun.Shoot(newAmmoURL(t, "/"))
Expand All @@ -205,8 +205,8 @@ func TestHTTP2(t *testing.T) {
defer server.Close()
log := zap.NewNop()
conf := DefaultHTTP2GunConfig()
conf.Gun.Target = server.Listener.Addr().String()
gun, _ := NewHTTP2Gun(conf, log, conf.Gun.Target)
conf.Target = server.Listener.Addr().String()
gun, _ := NewHTTP2Gun(conf, log, conf.Target)
var results netsample.TestAggregator
_ = gun.Bind(&results, testDeps())
var r interface{}
Expand All @@ -227,9 +227,9 @@ func TestHTTP2(t *testing.T) {
defer server.Close()
log := zap.NewNop()
conf := DefaultHTTP2GunConfig()
conf.Gun.Target = server.Listener.Addr().String()
conf.Gun.SSL = false
_, err := NewHTTP2Gun(conf, log, conf.Gun.Target)
conf.Target = server.Listener.Addr().String()
conf.SSL = false
_, err := NewHTTP2Gun(conf, log, conf.Target)
require.Error(t, err)
})
}
Expand Down
10 changes: 5 additions & 5 deletions components/guns/http_scenario/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ func (g *gunWrapper) Bind(a core.Aggregator, deps core.GunDeps) error {

func Import(fs afero.Fs) {
register.Gun("http/scenario", func(conf phttp.HTTPGunConfig) func() core.Gun {
targetResolved, _ := PreResolveTargetAddr(&conf.Client, conf.Gun.Target)
answLog := answlog.Init(conf.Gun.Base.AnswLog.Path, conf.Gun.Base.AnswLog.Enabled)
targetResolved, _ := PreResolveTargetAddr(&conf.Client, conf.Target)
answLog := answlog.Init(conf.Base.AnswLog.Path, conf.Base.AnswLog.Enabled)
return func() core.Gun {
gun := NewHTTPGun(conf, answLog, targetResolved)
return WrapGun(gun)
}
}, phttp.DefaultHTTPGunConfig)

register.Gun("http2/scenario", func(conf phttp.HTTP2GunConfig) func() (core.Gun, error) {
targetResolved, _ := PreResolveTargetAddr(&conf.Client, conf.Gun.Target)
answLog := answlog.Init(conf.Gun.Base.AnswLog.Path, conf.Gun.Base.AnswLog.Enabled)
register.Gun("http2/scenario", func(conf phttp.HTTPGunConfig) func() (core.Gun, error) {
targetResolved, _ := PreResolveTargetAddr(&conf.Client, conf.Target)
answLog := answlog.Init(conf.Base.AnswLog.Path, conf.Base.AnswLog.Enabled)
return func() (core.Gun, error) {
gun, err := NewHTTP2Gun(conf, answLog, targetResolved)
return WrapGun(gun), err
Expand Down
14 changes: 7 additions & 7 deletions components/guns/http_scenario/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ import (
)

func NewHTTPGun(conf phttp.HTTPGunConfig, answLog *zap.Logger, targetResolved string) *BaseGun {
transport := phttp.NewTransport(conf.Client.Transport, phttp.NewDialer(conf.Client.Dialer).DialContext, conf.Gun.Target)
transport := phttp.NewTransport(conf.Client.Transport, phttp.NewDialer(conf.Client.Dialer).DialContext, conf.Target)
client := newClient(transport, conf.Client.Redirect)
return NewClientGun(client, conf.Gun, answLog, targetResolved)
return NewClientGun(client, conf, answLog, targetResolved)
}

// NewHTTP2Gun return simple HTTP/2 gun that can shoot sequentially through one connection.
func NewHTTP2Gun(conf phttp.HTTP2GunConfig, answLog *zap.Logger, targetResolved string) (*BaseGun, error) {
if !conf.Gun.SSL {
func NewHTTP2Gun(conf phttp.HTTPGunConfig, answLog *zap.Logger, targetResolved string) (*BaseGun, error) {
if !conf.SSL {
// 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")
}
transport := phttp.NewHTTP2Transport(conf.Client.Transport, phttp.NewDialer(conf.Client.Dialer).DialContext, conf.Gun.Target)
transport := phttp.NewHTTP2Transport(conf.Client.Transport, phttp.NewDialer(conf.Client.Dialer).DialContext, conf.Target)
client := newClient(transport, conf.Client.Redirect)
// Will panic and cancel shooting whet target doesn't support HTTP/2.
client = &panicOnHTTP1Client{client}
return NewClientGun(client, conf.Gun, answLog, targetResolved), nil
return NewClientGun(client, conf, answLog, targetResolved), nil
}

func NewClientGun(client Client, conf phttp.GunConfig, answLog *zap.Logger, targetResolved string) *BaseGun {
func NewClientGun(client Client, conf phttp.HTTPGunConfig, answLog *zap.Logger, targetResolved string) *BaseGun {
scheme := "http"
if conf.SSL {
scheme = "https"
Expand Down
12 changes: 6 additions & 6 deletions components/phttp/import/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ func Import(fs afero.Fs) {
scenarioProvider.Import(fs)

register.Gun("http", func(conf phttp.HTTPGunConfig) func() core.Gun {
targetResolved, _ := PreResolveTargetAddr(&conf.Client, conf.Gun.Target)
answLog := answlog.Init(conf.Gun.Base.AnswLog.Path, conf.Gun.Base.AnswLog.Enabled)
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)) }
}, phttp.DefaultHTTPGunConfig)

register.Gun("http2", func(conf phttp.HTTP2GunConfig) func() (core.Gun, error) {
targetResolved, _ := PreResolveTargetAddr(&conf.Client, conf.Gun.Target)
answLog := answlog.Init(conf.Gun.Base.AnswLog.Path, conf.Gun.Base.AnswLog.Enabled)
register.Gun("http2", func(conf phttp.HTTPGunConfig) func() (core.Gun, error) {
targetResolved, _ := PreResolveTargetAddr(&conf.Client, conf.Target)
answLog := answlog.Init(conf.Base.AnswLog.Path, conf.Base.AnswLog.Enabled)
return func() (core.Gun, error) {
gun, err := phttp.NewHTTP2Gun(conf, answLog, targetResolved)
return phttp.WrapGun(gun), err
Expand All @@ -37,7 +37,7 @@ func Import(fs afero.Fs) {

register.Gun("connect", func(conf phttp.ConnectGunConfig) func() core.Gun {
conf.Target, _ = PreResolveTargetAddr(&conf.Client, conf.Target)
answLog := answlog.Init(conf.BaseGunConfig.AnswLog.Path, conf.BaseGunConfig.AnswLog.Enabled)
answLog := answlog.Init(conf.Base.AnswLog.Path, conf.Base.AnswLog.Enabled)
return func() core.Gun {
return phttp.WrapGun(phttp.NewConnectGun(conf, answLog))
}
Expand Down
4 changes: 1 addition & 3 deletions tests/http_scenario/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ func (s *GunSuite) Test_SuccessScenario() {
ctx := context.Background()
log := zap.NewNop()
g := httpscenario.NewHTTPGun(phttp.HTTPGunConfig{
Gun: phttp.GunConfig{
Target: s.addr,
},
Target: s.addr,
Client: phttp.ClientConfig{},
}, log, s.addr)

Expand Down

0 comments on commit a34bcde

Please sign in to comment.