Skip to content

Commit

Permalink
Merge pull request #144 from yandex/arcadia
Browse files Browse the repository at this point in the history
Http URI style ammo
  • Loading branch information
ligreen authored Nov 23, 2021
2 parents e6cddc7 + 3250fc3 commit e7713bc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/yandex/pandora/lib/zaputil"
)

const Version = "0.3.4"
const Version = "0.3.5"
const defaultConfigFile = "load"
const stdinConfigSelector = "-"

Expand Down
3 changes: 3 additions & 0 deletions components/phttp/ammo/simple/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func NewProvider(fs afero.Fs, fileName string, start func(ctx context.Context, f
start: start,
Sink: make(chan *Ammo, 128),
Pool: sync.Pool{New: func() interface{} { return &Ammo{} }},
Close: func() {},
}
}

Expand All @@ -33,6 +34,7 @@ type Provider struct {
Sink chan *Ammo
Pool sync.Pool
idCounter atomic.Int64
Close func()
core.ProviderDeps
}

Expand All @@ -49,6 +51,7 @@ func (p *Provider) Release(a core.Ammo) {
}

func (p *Provider) Run(ctx context.Context, deps core.ProviderDeps) error {
defer p.Close()
p.ProviderDeps = deps
defer close(p.Sink)
file, err := p.fs.Open(p.fileName)
Expand Down
31 changes: 30 additions & 1 deletion components/phttp/ammo/simple/uri/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package uri
import (
"bufio"
"context"
"fmt"

"github.com/pkg/errors"
"github.com/spf13/afero"
Expand All @@ -17,22 +18,50 @@ import (
)

type Config struct {
File string `validate:"required"`
File string
// Limit limits total num of ammo. Unlimited if zero.
Limit int `validate:"min=0"`
// Redefine HTTP headers
Headers []string
// Passes limits ammo file passes. Unlimited if zero.
Passes int `validate:"min=0"`
Uris []string
}

// TODO: pass logger and metricsRegistry
func NewProvider(fs afero.Fs, conf Config) *Provider {
if len(conf.Uris) > 0 {
if conf.File != "" {
panic(`One should specify either 'file' or 'uris', but not both of them.`)
}
file, err := afero.TempFile(fs, "", "generated_ammo_")
if err != nil {
panic(fmt.Sprintf(`failed to create tmp ammo file: %v`, err))
}
for _, uri := range conf.Uris {
_, err := file.WriteString(fmt.Sprintf("%s\n", uri))
if err != nil {
panic(fmt.Sprintf(`failed to write ammo in tmp file: %v`, err))
}
}
conf.File = file.Name()
}
if conf.File == "" {
panic(`One should specify either 'file' or 'uris'.`)
}
var p Provider
p = Provider{
Provider: simple.NewProvider(fs, conf.File, p.start),
Config: conf,
}
p.Close = func() {
if len(conf.Uris) > 0 {
err := fs.Remove(conf.File)
if err != nil {
zap.L().Error("failed to delete temp file", zap.String("file name", conf.File))
}
}
}
return &p
}

Expand Down

0 comments on commit e7713bc

Please sign in to comment.