Skip to content

Commit

Permalink
pool size
Browse files Browse the repository at this point in the history
a22625a9522b6fd6948d1091081f3c2a8d403321
  • Loading branch information
oke11o committed Mar 18, 2024
1 parent a6530df commit 116bcc8
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@
"tests/acceptance/http_test.go":"load/projects/pandora/tests/acceptance/http_test.go",
"tests/acceptance/testdata/connect/connect-check-limit.yaml":"load/projects/pandora/tests/acceptance/testdata/connect/connect-check-limit.yaml",
"tests/acceptance/testdata/connect/connect-check-passes.yaml":"load/projects/pandora/tests/acceptance/testdata/connect/connect-check-passes.yaml",
"tests/acceptance/testdata/connect/connect-pool-size.yaml":"load/projects/pandora/tests/acceptance/testdata/connect/connect-pool-size.yaml",
"tests/acceptance/testdata/connect/connect-ssl.yaml":"load/projects/pandora/tests/acceptance/testdata/connect/connect-ssl.yaml",
"tests/acceptance/testdata/connect/connect.yaml":"load/projects/pandora/tests/acceptance/testdata/connect/connect.yaml",
"tests/acceptance/testdata/connect/payload.uri":"load/projects/pandora/tests/acceptance/testdata/connect/payload.uri",
Expand All @@ -377,6 +378,7 @@
"tests/acceptance/testdata/http/http-check-limit.yaml":"load/projects/pandora/tests/acceptance/testdata/http/http-check-limit.yaml",
"tests/acceptance/testdata/http/http-check-passes.yaml":"load/projects/pandora/tests/acceptance/testdata/http/http-check-passes.yaml",
"tests/acceptance/testdata/http/http.yaml":"load/projects/pandora/tests/acceptance/testdata/http/http.yaml",
"tests/acceptance/testdata/http/http2-pool-size.yaml":"load/projects/pandora/tests/acceptance/testdata/http/http2-pool-size.yaml",
"tests/acceptance/testdata/http/http2.yaml":"load/projects/pandora/tests/acceptance/testdata/http/http2.yaml",
"tests/acceptance/testdata/http/https.yaml":"load/projects/pandora/tests/acceptance/testdata/http/https.yaml",
"tests/acceptance/testdata/http/payload.uri":"load/projects/pandora/tests/acceptance/testdata/http/payload.uri",
Expand Down
54 changes: 45 additions & 9 deletions components/guns/http/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/pkg/errors"
"github.com/yandex/pandora/core"
"github.com/yandex/pandora/core/aggregator/netsample"
"github.com/yandex/pandora/core/clientpool"
"github.com/yandex/pandora/core/warmup"
"github.com/yandex/pandora/lib/netutil"
"go.uber.org/zap"
Expand All @@ -28,6 +29,7 @@ type BaseGunConfig struct {
AutoTag AutoTagConfig `config:"auto-tag"`
AnswLog AnswLogConfig `config:"answlog"`
HTTPTrace HTTPTraceConfig `config:"httptrace"`
PoolSize int `config:"pool-size"`
}

// AutoTagConfig configure automatic tags generation based on ammo URI. First AutoTag URI path elements becomes tag.
Expand Down Expand Up @@ -78,26 +80,56 @@ func NewBaseGun(clientConstructor ClientConstructor, cfg HTTPGunConfig, answLog
},
AnswLog: answLog,
Client: client,
ClientConstructor: func() Client {
return clientConstructor(cfg.Client, cfg.Target)
},
}
}

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
Client Client
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
Client Client
ClientConstructor func() Client

core.GunDeps
}

var _ Gun = (*BaseGun)(nil)
var _ io.Closer = (*BaseGun)(nil)

func (b *BaseGun) WarmUp(_ *warmup.Options) (any, error) {
return nil, nil
type SharedDeps struct {
clientPool *clientpool.Pool[Client]
}

func (b *BaseGun) WarmUp(opts *warmup.Options) (any, error) {
return b.createSharedDeps(opts)
}

func (b *BaseGun) createSharedDeps(opts *warmup.Options) (*SharedDeps, error) {
clientPool, err := b.prepareClientPool()
if err != nil {
return nil, err
}
return &SharedDeps{
clientPool: clientPool,
}, nil
}

func (b *BaseGun) prepareClientPool() (*clientpool.Pool[Client], error) {
if b.Config.PoolSize <= 0 {
return nil, nil
}
clientPool, _ := clientpool.New[Client](b.Config.PoolSize)
for i := 0; i < b.Config.PoolSize; i++ {
client := b.ClientConstructor()
clientPool.Add(client)
}
return clientPool, nil
}

func (b *BaseGun) Bind(aggregator netsample.Aggregator, deps core.GunDeps) error {
Expand All @@ -106,6 +138,10 @@ func (b *BaseGun) Bind(aggregator netsample.Aggregator, deps core.GunDeps) error
// Enable debug level logging during shooting. Creating log entries isn't free.
b.DebugLog = true
}
extraDeps, ok := deps.Shared.(*SharedDeps)
if ok && extraDeps.clientPool != nil {
b.Client = extraDeps.clientPool.Next()
}

if b.Aggregator != nil {
log.Panic("already binded")
Expand Down
6 changes: 6 additions & 0 deletions tests/acceptance/connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ func (s *ConnectGunSuite) Test_Connect() {
isTLS: true,
wantCnt: 4,
},
{
name: "connect-pool-size",
filecfg: "testdata/connect/connect-pool-size.yaml",
isTLS: false,
wantCnt: 4,
},
}
for _, tt := range tests {
s.Run(tt.name, func() {
Expand Down
10 changes: 10 additions & 0 deletions tests/acceptance/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ func (s *PandoraSuite) Test_Http_Check_Passes() {
isTLS: false,
wantCnt: 15,
},
{
name: "http2-pool-size",
filecfg: "testdata/http/http2-pool-size.yaml",
isTLS: true,
preStartSrv: func(srv *httptest.Server) {
_ = http2.ConfigureServer(srv.Config, nil)
srv.TLS = srv.Config.TLSConfig
},
wantCnt: 8,
},
}
for _, tt := range tests {
s.Run(tt.name, func() {
Expand Down
23 changes: 23 additions & 0 deletions tests/acceptance/testdata/connect/connect-pool-size.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pools:
- id: ""
ammo:
file: testdata/http/payload.uri
type: uri
result:
type: discard
gun:
target: {{.target}}
type: connect
pool-size: 1
answlog:
enabled: false
rps-per-instance: false
rps:
- duration: 1s
ops: 4
type: const
startup:
- times: 2
type: once
log:
level: debug
26 changes: 26 additions & 0 deletions tests/acceptance/testdata/http/http2-pool-size.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pools:
- id: ""
ammo:
type: uri
headers:
- '[Content-Type: application/json]'
uris:
- /
result:
type: discard
gun:
target: {{.target}}
type: http2
answlog:
enabled: false
pool-size: 1
rps-per-instance: false
rps:
- duration: 1s
ops: 8
type: const
startup:
- times: 2
type: once
log:
level: debug

0 comments on commit 116bcc8

Please sign in to comment.