Skip to content

Commit aa268d8

Browse files
committed
feat(config): add poke strategy
1 parent e2aba40 commit aa268d8

3 files changed

Lines changed: 57 additions & 4 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ You can load a JSON configuration such as:
7575
}
7676
```
7777

78+
Or use the poke strategy to start instances without waiting:
79+
80+
```json
81+
{
82+
"sablier_url": "sablier:10000",
83+
"group": "my-group",
84+
"session_duration": "1m",
85+
"poke": {}
86+
}
87+
```
88+
7889
## Examples
7990

8091
### Apache APISIX

main.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ type BlockingConfiguration struct {
9191
Timeout string `json:"timeout"`
9292
}
9393

94+
type PokeConfiguration struct {
95+
}
96+
9497
type Config struct {
9598
// SablierURL in the format of hostname:port. The scheme is excluded
9699
SablierURL string `json:"sablier_url"`
@@ -105,6 +108,7 @@ type Config struct {
105108
SessionDuration string `json:"session_duration"`
106109
Dynamic *DynamicConfiguration `json:"dynamic"`
107110
Blocking *BlockingConfiguration `json:"blocking"`
111+
Poke *PokeConfiguration `json:"poke"`
108112
}
109113

110114
func (c Config) GetPath() string {
@@ -133,6 +137,8 @@ func (c Config) GetPath() string {
133137
return c.getDynamicQuery(path)
134138
} else if c.Blocking != nil {
135139
return c.getBlockingQuery(path)
140+
} else if c.Poke != nil {
141+
return c.getPokeQuery(path)
136142
}
137143
return "no strategy configured"
138144
}
@@ -183,6 +189,12 @@ func (c Config) getBlockingQuery(path url.URL) string {
183189
return path.String()
184190
}
185191

192+
func (c Config) getPokeQuery(path url.URL) string {
193+
path.Path = "/api/strategies/poke"
194+
195+
return path.String()
196+
}
197+
186198
func parsePluginConfiguration(data []byte) (pluginConfiguration, error) {
187199
pluginConf := newPluginConfiguration()
188200
if len(data) == 0 {
@@ -196,11 +208,20 @@ func parsePluginConfiguration(data []byte) (pluginConfiguration, error) {
196208
return pluginConf, err
197209
}
198210

199-
if c.Blocking == nil && c.Dynamic == nil {
200-
return pluginConf, fmt.Errorf("you must specify one strategy (dynamic or blocking)")
211+
strategyCount := 0
212+
if c.Blocking != nil {
213+
strategyCount++
201214
}
202-
203-
if c.Blocking != nil && c.Dynamic != nil {
215+
if c.Dynamic != nil {
216+
strategyCount++
217+
}
218+
if c.Poke != nil {
219+
strategyCount++
220+
}
221+
if strategyCount == 0 {
222+
return pluginConf, fmt.Errorf("you must specify one strategy (dynamic, blocking or poke)")
223+
}
224+
if strategyCount > 1 {
204225
return pluginConf, fmt.Errorf("you must specify only one strategy")
205226
}
206227

main_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@ import (
99
"github.com/stretchr/testify/require"
1010
)
1111

12+
func TestUnmarshalPoke(t *testing.T) {
13+
data := `{
14+
"sablier_url": "sablier",
15+
"group": "demo",
16+
"session_duration": "30s",
17+
"poke": {}
18+
}`
19+
20+
config, err := parsePluginConfiguration([]byte(data))
21+
if err != nil {
22+
t.Error(err)
23+
}
24+
25+
expected := "/api/strategies/poke?group=demo&session_duration=30s"
26+
if config.path != expected {
27+
t.Errorf("path = %v, want %v", config.path, expected)
28+
}
29+
30+
t.Log("path:", config.path)
31+
}
32+
1233
func TestUnmarshal(t *testing.T) {
1334
data := `{
1435
"sablier_url": "sablier",

0 commit comments

Comments
 (0)