github.com/yandex/pandora@v0.5.32/components/guns/http/wrapper.go (about)

     1  package phttp
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/yandex/pandora/core"
     7  	"github.com/yandex/pandora/core/aggregator/netsample"
     8  	"github.com/yandex/pandora/core/warmup"
     9  )
    10  
    11  //go:generate mockery --name=Ammo --case=underscore --outpkg=ammomock
    12  
    13  // Ammo ammo interface for http based guns.
    14  // http ammo providers should produce ammo that implements Ammo.
    15  // http guns should use convert ammo to Ammo, not to specific implementation.
    16  // Returned request have
    17  type Ammo interface {
    18  	// TODO(skipor): instead of sample use it wrapper with httptrace and more usable interface.
    19  	Request() (*http.Request, *netsample.Sample)
    20  	// Id unique ammo id. Usually equals to ammo num got from provider.
    21  	ID() uint64
    22  	IsInvalid() bool
    23  }
    24  
    25  type Gun interface {
    26  	Shoot(ammo Ammo)
    27  	Bind(sample netsample.Aggregator, deps core.GunDeps) error
    28  	WarmUp(opts *warmup.Options) (any, error)
    29  }
    30  
    31  func WrapGun(g Gun) core.Gun {
    32  	if g == nil {
    33  		return nil
    34  	}
    35  	return &gunWrapper{g}
    36  }
    37  
    38  type gunWrapper struct{ Gun }
    39  
    40  func (g *gunWrapper) Shoot(ammo core.Ammo) {
    41  	g.Gun.Shoot(ammo.(Ammo))
    42  }
    43  
    44  func (g *gunWrapper) Bind(a core.Aggregator, deps core.GunDeps) error {
    45  	return g.Gun.Bind(netsample.UnwrapAggregator(a), deps)
    46  }
    47  
    48  func (g *gunWrapper) WarmUp(opts *warmup.Options) (any, error) {
    49  	return g.Gun.WarmUp(opts)
    50  }