-
Notifications
You must be signed in to change notification settings - Fork 63
/
custom_main.go
88 lines (71 loc) · 2.09 KB
/
custom_main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2017 Yandex LLC. All rights reserved.
// Use of this source code is governed by a MPL 2.0
// license that can be found in the LICENSE file.
// Author: Vladimir Skipor <skipor@yandex-team.ru>
package main
import (
"math/rand"
"time"
"github.com/spf13/afero"
"github.com/yandex/pandora/cli"
phttp "github.com/yandex/pandora/components/phttp/import"
"github.com/yandex/pandora/core"
coreimport "github.com/yandex/pandora/core/import"
"github.com/yandex/pandora/core/register"
"go.uber.org/zap"
)
type Ammo struct {
URL string
QueryParam string
}
type Sample struct {
URL string
ShootTimeSeconds float64
}
type GunConfig struct {
Target string `validate:"required"` // Configuration will fail, without target defined
}
type Gun struct {
// Configured on construction.
conf GunConfig
// Configured on Bind, before shooting.
aggr core.Aggregator // May be your custom Aggregator.
core.GunDeps
}
func NewGun(conf GunConfig) *Gun {
return &Gun{conf: conf}
}
func (g *Gun) Bind(aggr core.Aggregator, deps core.GunDeps) error {
g.aggr = aggr
g.GunDeps = deps
return nil
}
func (g *Gun) Shoot(ammo core.Ammo) {
customAmmo := ammo.(*Ammo) // Shoot will panic on unexpected ammo type. Panic cancels shooting.
g.shoot(customAmmo)
}
func (g *Gun) shoot(ammo *Ammo) {
start := time.Now()
defer func() {
g.aggr.Report(Sample{ammo.URL, time.Since(start).Seconds()})
}()
// Put your shoot logic here.
g.Log.Info("Custom shoot", zap.String("target", g.conf.Target), zap.Any("ammo", ammo))
time.Sleep(time.Duration(rand.Float64() * float64(time.Second)))
}
func main() {
// Standard imports.
fs := afero.NewOsFs()
coreimport.Import(fs)
// May not be imported, if you don't need http guns and etc.
phttp.Import(fs)
// Custom imports. Integrate your custom types into configuration system.
coreimport.RegisterCustomJSONProvider("my-custom-provider-name", func() core.Ammo { return &Ammo{} })
register.Gun("my-custom-gun-name", NewGun, func() GunConfig {
return GunConfig{
Target: "default target",
}
})
register.Gun("my-custom/no-default", NewGun)
cli.Run()
}