Skip to content

Commit

Permalink
http generator refactoring
Browse files Browse the repository at this point in the history
http generator refactoring
5e26c538a6aa54349735878922023ec6190b8e83
  • Loading branch information
oke11o committed Mar 15, 2024
1 parent 8b70505 commit 7642d2d
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 164 deletions.
21 changes: 15 additions & 6 deletions components/guns/http/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,27 @@ func DefaultBaseGunConfig() BaseGunConfig {
}
}

func NewBaseGun(clientConstructor ClientConstructor, cfg HTTPGunConfig, answLog *zap.Logger) *BaseGun {
client := clientConstructor(cfg.Client, cfg.Target)
return &BaseGun{
Config: cfg.Base,
OnClose: func() error {
client.CloseIdleConnections()
return nil
},
AnswLog: answLog,
Client: client,
}
}

type BaseGun struct {
DebugLog bool // Automaticaly set in Bind if Log accepts debug messages.
Config BaseGunConfig
Connect func(ctx context.Context) error // Optional hook.
OnClose func() error // Optional. Called on Close().
Aggregator netsample.Aggregator // Lazy set via BindResultTo.
AnswLog *zap.Logger

scheme string
hostname string
targetResolved string
client Client
Client Client

core.GunDeps
}
Expand Down Expand Up @@ -169,7 +178,7 @@ func (b *BaseGun) Shoot(ammo Ammo) {
}
}
var res *http.Response
res, err = b.client.Do(req)
res, err = b.Client.Do(req)
if b.Config.HTTPTrace.TraceEnabled && timings != nil {
sample.SetReceiveTime(timings.GetReceiveTime())
}
Expand Down
10 changes: 5 additions & 5 deletions components/guns/http/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func (c *testDecoratedClient) CloseIdleConnections() {
}

func (s *BaseGunSuite) Test_Shoot_BeforeBindPanics() {
s.base.client = &testDecoratedClient{
client: s.base.client,
s.base.Client = &testDecoratedClient{
client: s.base.Client,
before: func(req *http.Request) { panic("should not be called\"") },
after: nil,
}
Expand Down Expand Up @@ -181,7 +181,7 @@ func (s *BaseGunSuite) Test_Shoot() {
beforeEachDoOk := func() {
body = ioutil.NopCloser(strings.NewReader("aaaaaaa"))
s.base.AnswLog = zap.NewNop()
s.base.client = &testDecoratedClient{
s.base.Client = &testDecoratedClient{
before: func(doReq *http.Request) {
s.Require().Equal(req, doReq)
},
Expand Down Expand Up @@ -268,8 +268,8 @@ func (s *BaseGunSuite) Test_Shoot() {
return nil
}

s.base.client = &testDecoratedClient{
client: s.base.client,
s.base.Client = &testDecoratedClient{
client: s.base.Client,
before: func(doReq *http.Request) {
doCalled = true
},
Expand Down
33 changes: 14 additions & 19 deletions components/guns/http/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,21 @@ import (
)

func NewConnectGun(cfg HTTPGunConfig, answLog *zap.Logger) *BaseGun {
scheme := "http"
if cfg.SSL {
scheme = "https"
}
client := newConnectClient(cfg.Client, cfg.Target)
wrappedClient := &httpDecoratedClient{
client: client,
scheme: scheme,
hostname: "",
targetResolved: cfg.Target,
}
return &BaseGun{
Config: cfg.Base,
OnClose: func() error {
client.CloseIdleConnections()
return nil
},
AnswLog: answLog,
client: wrappedClient,
var wrappedConstructor = func(clientConfig ClientConfig, target string) Client {
scheme := "http"
if cfg.SSL {
scheme = "https"
}
client := newConnectClient(cfg.Client, cfg.Target)
return &httpDecoratedClient{
client: client,
hostname: "",
targetResolved: cfg.Target,
scheme: scheme,
}
}

return NewBaseGun(wrappedConstructor, cfg, answLog)
}

func DefaultConnectGunConfig() HTTPGunConfig {
Expand Down
41 changes: 19 additions & 22 deletions components/guns/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ type HTTPGunConfig struct {
SSL bool
}

func NewHTTP1Gun(conf HTTPGunConfig, answLog *zap.Logger, targetResolved string) *BaseGun {
return newHTTPGun(HTTP1ClientConstructor, conf, answLog, targetResolved)
func NewHTTP1Gun(cfg HTTPGunConfig, answLog *zap.Logger, targetResolved string) *BaseGun {
var wrappedConstructor = func(clientConfig ClientConfig, target string) Client {
return WrapClientHostResolving(
HTTP1ClientConstructor(cfg.Client, cfg.Target),
cfg,
targetResolved,
)
}
return NewBaseGun(wrappedConstructor, cfg, answLog)
}

func HTTP1ClientConstructor(clientConfig ClientConfig, target string) Client {
Expand All @@ -25,12 +32,19 @@ func HTTP1ClientConstructor(clientConfig ClientConfig, target string) Client {
var _ ClientConstructor = HTTP1ClientConstructor

// NewHTTP2Gun return simple HTTP/2 gun that can shoot sequentially through one connection.
func NewHTTP2Gun(conf HTTPGunConfig, answLog *zap.Logger, targetResolved string) (*BaseGun, error) {
if !conf.SSL {
func NewHTTP2Gun(cfg HTTPGunConfig, answLog *zap.Logger, targetResolved string) (*BaseGun, error) {
if !cfg.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 newHTTPGun(HTTP2ClientConstructor, conf, answLog, targetResolved), nil
var wrappedConstructor = func(clientConfig ClientConfig, target string) Client {
return WrapClientHostResolving(
HTTP2ClientConstructor(cfg.Client, cfg.Target),
cfg,
targetResolved,
)
}
return NewBaseGun(wrappedConstructor, cfg, answLog), nil
}

func HTTP2ClientConstructor(clientConfig ClientConfig, target string) Client {
Expand All @@ -42,23 +56,6 @@ func HTTP2ClientConstructor(clientConfig ClientConfig, target string) Client {

var _ ClientConstructor = HTTP2ClientConstructor

func newHTTPGun(clientConstructor ClientConstructor, cfg HTTPGunConfig, answLog *zap.Logger, targetResolved string) *BaseGun {
client := clientConstructor(cfg.Client, cfg.Target)
wrappedClient := WrapClientHostResolving(client, cfg, targetResolved)
return &BaseGun{
Config: cfg.Base,
OnClose: func() error {
client.CloseIdleConnections()
return nil
},
AnswLog: answLog,

hostname: getHostWithoutPort(cfg.Target),
targetResolved: targetResolved,
client: wrappedClient,
}
}

func DefaultHTTPGunConfig() HTTPGunConfig {
return HTTPGunConfig{
SSL: false,
Expand Down
Loading

0 comments on commit 7642d2d

Please sign in to comment.