Skip to content

Commit

Permalink
answlog fix
Browse files Browse the repository at this point in the history
answlog fix
5e87661de95d4947595f0a6bf8c056cab5133e85
  • Loading branch information
oke11o committed Apr 27, 2024
1 parent 2eb92fd commit 57b3522
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 159 deletions.
46 changes: 14 additions & 32 deletions components/guns/http/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ const (
EmptyTag = "__EMPTY__"
)

type BaseGunConfig struct {
AutoTag AutoTagConfig `config:"auto-tag"`
AnswLog AnswLogConfig `config:"answlog"`
HTTPTrace HTTPTraceConfig `config:"httptrace"`
SharedClient struct {
ClientNumber int `config:"client-number,omitempty"`
Enabled bool `config:"enabled"`
} `config:"shared-client,omitempty"`
}

// AutoTagConfig configure automatic tags generation based on ammo URI. First AutoTag URI path elements becomes tag.
// Example: /my/very/deep/page?id=23&param=33 -> /my/very when uri-elements: 2.
type AutoTagConfig struct {
Expand All @@ -54,29 +44,10 @@ type HTTPTraceConfig struct {
TraceEnabled bool `config:"trace"`
}

func DefaultBaseGunConfig() BaseGunConfig {
return BaseGunConfig{
AutoTag: AutoTagConfig{
Enabled: false,
URIElements: 2,
NoTagOnly: true,
},
AnswLog: AnswLogConfig{
Enabled: false,
Path: "answ.log",
Filter: "error",
},
HTTPTrace: HTTPTraceConfig{
DumpEnabled: false,
TraceEnabled: false,
},
}
}

func NewBaseGun(clientConstructor ClientConstructor, cfg HTTPGunConfig, answLog *zap.Logger) *BaseGun {
func NewBaseGun(clientConstructor ClientConstructor, cfg GunConfig, answLog *zap.Logger) *BaseGun {
client := clientConstructor(cfg.Client, cfg.Target)
return &BaseGun{
Config: cfg.Base,
Config: cfg,
OnClose: func() error {
client.CloseIdleConnections()
return nil
Expand All @@ -91,7 +62,7 @@ func NewBaseGun(clientConstructor ClientConstructor, cfg HTTPGunConfig, answLog

type BaseGun struct {
DebugLog bool // Automaticaly set in Bind if Log accepts debug messages.
Config BaseGunConfig
Config GunConfig
Connect func(ctx context.Context) error // Optional hook.
OnClose func() error // Optional. Called on Close().
Aggregator netsample.Aggregator // Lazy set via BindResultTo.
Expand Down Expand Up @@ -183,6 +154,17 @@ func (b *BaseGun) Shoot(ammo Ammo) {
b.Log.Warn("Invalid ammo", zap.Uint64("request", ammo.ID()))
return
}

if b.Config.SSL {
req.URL.Scheme = "https"
} else {
req.URL.Scheme = "http"
}
if req.Host == "" {
req.Host = getHostWithoutPort(b.Config.Target)
}
req.URL.Host = b.Config.TargetResolved

if b.DebugLog {
b.Log.Debug("Prepared ammo to shoot", zap.Stringer("url", req.URL))
}
Expand Down
5 changes: 3 additions & 2 deletions components/guns/http/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *BaseGunSuite) SetupSuite() {
}

func (s *BaseGunSuite) SetupTest() {
s.base = BaseGun{Config: DefaultBaseGunConfig()}
s.base = BaseGun{Config: DefaultHTTPGunConfig()}
}

func (s *BaseGunSuite) Test_BindResultTo_Panics() {
Expand Down Expand Up @@ -335,8 +335,9 @@ func Test_Autotag(t *testing.T) {
}

func Test_ConfigDecode(t *testing.T) {
var conf BaseGunConfig
var conf GunConfig
coretest.DecodeAndValidateT(t, `
target: localhost:80
auto-tag:
enabled: true
uri-elements: 3
Expand Down
37 changes: 0 additions & 37 deletions components/guns/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,43 +173,6 @@ func (c *panicOnHTTP1Client) Do(req *http.Request) (*http.Response, error) {
return res, nil
}

func WrapClientHostResolving(client Client, cfg HTTPGunConfig, targetResolved string) Client {
hostname := getHostWithoutPort(cfg.Target)
scheme := "http"
if cfg.SSL {
scheme = "https"
}
return &httpDecoratedClient{
client: client,
scheme: scheme,
hostname: hostname,
targetResolved: targetResolved,
}
}

type httpDecoratedClient struct {
client Client
scheme string
hostname string
targetResolved string
}

func (c *httpDecoratedClient) Do(req *http.Request) (*http.Response, error) {
if req.Host == "" {
req.Host = c.hostname
}

if c.targetResolved != "" {
req.URL.Host = c.targetResolved
}
req.URL.Scheme = c.scheme
return c.client.Do(req)
}

func (c *httpDecoratedClient) CloseIdleConnections() {
c.client.CloseIdleConnections()
}

func checkHTTP2(state *tls.ConnectionState) error {
if state == nil {
return errors.New("http2: non TLS connection")
Expand Down
37 changes: 20 additions & 17 deletions components/guns/http/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,32 @@ import (
"go.uber.org/zap"
)

func NewConnectGun(cfg HTTPGunConfig, answLog *zap.Logger) *BaseGun {
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,
}
func NewConnectGun(cfg GunConfig, answLog *zap.Logger) *BaseGun {
if cfg.TargetResolved == "" {
cfg.TargetResolved = cfg.Target
}

return NewBaseGun(wrappedConstructor, cfg, answLog)
return NewBaseGun(newConnectClient, cfg, answLog)
}

func DefaultConnectGunConfig() HTTPGunConfig {
return HTTPGunConfig{
func DefaultConnectGunConfig() GunConfig {
return GunConfig{
SSL: false,
Client: DefaultClientConfig(),
Base: DefaultBaseGunConfig(),
AutoTag: AutoTagConfig{
Enabled: false,
URIElements: 2,
NoTagOnly: true,
},
AnswLog: AnswLogConfig{
Enabled: false,
Path: "answ.log",
Filter: "error",
},
HTTPTrace: HTTPTraceConfig{
DumpEnabled: false,
TraceEnabled: false,
},
}
}

Expand Down
80 changes: 50 additions & 30 deletions components/guns/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ import (
"go.uber.org/zap"
)

type HTTPGunConfig struct {
Base BaseGunConfig `config:",squash"`
Client ClientConfig `config:",squash"`
Target string `validate:"endpoint,required"`
SSL bool
type GunConfig struct {
Client ClientConfig `config:",squash"`
Target string `validate:"endpoint,required"`
TargetResolved string `config:"-"`
SSL bool

AutoTag AutoTagConfig `config:"auto-tag"`
AnswLog AnswLogConfig `config:"answlog"`
HTTPTrace HTTPTraceConfig `config:"httptrace"`
SharedClient struct {
ClientNumber int `config:"client-number,omitempty"`
Enabled bool `config:"enabled"`
} `config:"shared-client,omitempty"`
}

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 NewHTTP1Gun(cfg GunConfig, answLog *zap.Logger) *BaseGun {
return NewBaseGun(HTTP1ClientConstructor, cfg, answLog)
}

func HTTP1ClientConstructor(clientConfig ClientConfig, target string) Client {
Expand All @@ -32,19 +33,12 @@ 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(cfg HTTPGunConfig, answLog *zap.Logger, targetResolved string) (*BaseGun, error) {
func NewHTTP2Gun(cfg GunConfig, answLog *zap.Logger) (*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.")
}
var wrappedConstructor = func(clientConfig ClientConfig, target string) Client {
return WrapClientHostResolving(
HTTP2ClientConstructor(cfg.Client, cfg.Target),
cfg,
targetResolved,
)
}
return NewBaseGun(wrappedConstructor, cfg, answLog), nil
return NewBaseGun(HTTP2ClientConstructor, cfg, answLog), nil
}

func HTTP2ClientConstructor(clientConfig ClientConfig, target string) Client {
Expand All @@ -56,18 +50,44 @@ func HTTP2ClientConstructor(clientConfig ClientConfig, target string) Client {

var _ ClientConstructor = HTTP2ClientConstructor

func DefaultHTTPGunConfig() HTTPGunConfig {
return HTTPGunConfig{
func DefaultHTTPGunConfig() GunConfig {
return GunConfig{
SSL: false,
Base: DefaultBaseGunConfig(),
Client: DefaultClientConfig(),
AutoTag: AutoTagConfig{
Enabled: false,
URIElements: 2,
NoTagOnly: true,
},
AnswLog: AnswLogConfig{
Enabled: false,
Path: "answ.log",
Filter: "error",
},
HTTPTrace: HTTPTraceConfig{
DumpEnabled: false,
TraceEnabled: false,
},
}
}

func DefaultHTTP2GunConfig() HTTPGunConfig {
return HTTPGunConfig{
func DefaultHTTP2GunConfig() GunConfig {
return GunConfig{
Client: DefaultClientConfig(),
Base: DefaultBaseGunConfig(),
SSL: true,
AutoTag: AutoTagConfig{
Enabled: false,
URIElements: 2,
NoTagOnly: true,
},
AnswLog: AnswLogConfig{
Enabled: false,
Path: "answ.log",
Filter: "error",
},
HTTPTrace: HTTPTraceConfig{
DumpEnabled: false,
TraceEnabled: false,
},
SSL: true,
}
}
21 changes: 14 additions & 7 deletions components/guns/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func TestBaseGun_integration(t *testing.T) {
conf.Target = host + ":80"
targetResolved := strings.TrimPrefix(server.URL, "http://")
results := &netsample.TestAggregator{}
httpGun := NewHTTP1Gun(conf, log, targetResolved)
conf.TargetResolved = targetResolved
httpGun := NewHTTP1Gun(conf, log)
_ = httpGun.Bind(results, testDeps())

am := newAmmoReq(t, expectedReq)
Expand Down Expand Up @@ -90,7 +91,8 @@ func TestHTTP(t *testing.T) {
conf := DefaultHTTPGunConfig()
conf.Target = server.Listener.Addr().String()
conf.SSL = tt.https
gun := NewHTTP1Gun(conf, log, conf.Target)
conf.TargetResolved = conf.Target
gun := NewHTTP1Gun(conf, log)
var aggr netsample.TestAggregator
_ = gun.Bind(&aggr, testDeps())
gun.Shoot(newAmmoURL(t, "/"))
Expand Down Expand Up @@ -131,7 +133,8 @@ func TestHTTP_Redirect(t *testing.T) {
conf := DefaultHTTPGunConfig()
conf.Target = server.Listener.Addr().String()
conf.Client.Redirect = tt.redirect
gun := NewHTTP1Gun(conf, log, conf.Target)
conf.TargetResolved = conf.Target
gun := NewHTTP1Gun(conf, log)
var aggr netsample.TestAggregator
_ = gun.Bind(&aggr, testDeps())
gun.Shoot(newAmmoURL(t, "/redirect"))
Expand Down Expand Up @@ -169,7 +172,8 @@ func TestHTTP_notSupportHTTP2(t *testing.T) {
conf := DefaultHTTPGunConfig()
conf.Target = server.Listener.Addr().String()
conf.SSL = true
gun := NewHTTP1Gun(conf, log, conf.Target)
conf.TargetResolved = conf.Target
gun := NewHTTP1Gun(conf, log)
var results netsample.TestAggregator
_ = gun.Bind(&results, testDeps())
gun.Shoot(newAmmoURL(t, "/"))
Expand All @@ -191,7 +195,8 @@ func TestHTTP2(t *testing.T) {
log := zap.NewNop()
conf := DefaultHTTP2GunConfig()
conf.Target = server.Listener.Addr().String()
gun, _ := NewHTTP2Gun(conf, log, conf.Target)
conf.TargetResolved = conf.Target
gun, _ := NewHTTP2Gun(conf, log)
var results netsample.TestAggregator
_ = gun.Bind(&results, testDeps())
gun.Shoot(newAmmoURL(t, "/"))
Expand All @@ -206,7 +211,8 @@ func TestHTTP2(t *testing.T) {
log := zap.NewNop()
conf := DefaultHTTP2GunConfig()
conf.Target = server.Listener.Addr().String()
gun, _ := NewHTTP2Gun(conf, log, conf.Target)
conf.TargetResolved = conf.Target
gun, _ := NewHTTP2Gun(conf, log)
var results netsample.TestAggregator
_ = gun.Bind(&results, testDeps())
var r interface{}
Expand All @@ -229,7 +235,8 @@ func TestHTTP2(t *testing.T) {
conf := DefaultHTTP2GunConfig()
conf.Target = server.Listener.Addr().String()
conf.SSL = false
_, err := NewHTTP2Gun(conf, log, conf.Target)
conf.TargetResolved = conf.Target
_, err := NewHTTP2Gun(conf, log)
require.Error(t, err)
})
}
Expand Down
Loading

0 comments on commit 57b3522

Please sign in to comment.