Skip to content

Commit 8e41366

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

3 files changed

Lines changed: 64 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: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type pluginConfiguration struct {
4646
path string
4747
authority string
4848
timeout uint32
49+
poke bool
4950
}
5051

5152
// newPluginConfiguration creates a pluginConfiguration with default values
@@ -91,6 +92,9 @@ type BlockingConfiguration struct {
9192
Timeout string `json:"timeout"`
9293
}
9394

95+
type PokeConfiguration struct {
96+
}
97+
9498
type Config struct {
9599
// SablierURL in the format of hostname:port. The scheme is excluded
96100
SablierURL string `json:"sablier_url"`
@@ -105,6 +109,7 @@ type Config struct {
105109
SessionDuration string `json:"session_duration"`
106110
Dynamic *DynamicConfiguration `json:"dynamic"`
107111
Blocking *BlockingConfiguration `json:"blocking"`
112+
Poke *PokeConfiguration `json:"poke"`
108113
}
109114

110115
func (c Config) GetPath() string {
@@ -133,6 +138,8 @@ func (c Config) GetPath() string {
133138
return c.getDynamicQuery(path)
134139
} else if c.Blocking != nil {
135140
return c.getBlockingQuery(path)
141+
} else if c.Poke != nil {
142+
return c.getPokeQuery(path)
136143
}
137144
return "no strategy configured"
138145
}
@@ -183,6 +190,12 @@ func (c Config) getBlockingQuery(path url.URL) string {
183190
return path.String()
184191
}
185192

193+
func (c Config) getPokeQuery(path url.URL) string {
194+
path.Path = "/api/strategies/poke"
195+
196+
return path.String()
197+
}
198+
186199
func parsePluginConfiguration(data []byte) (pluginConfiguration, error) {
187200
pluginConf := newPluginConfiguration()
188201
if len(data) == 0 {
@@ -196,11 +209,20 @@ func parsePluginConfiguration(data []byte) (pluginConfiguration, error) {
196209
return pluginConf, err
197210
}
198211

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

@@ -232,6 +254,7 @@ func parsePluginConfiguration(data []byte) (pluginConfiguration, error) {
232254
}
233255

234256
pluginConf.path = c.GetPath()
257+
pluginConf.poke = c.Poke != nil
235258

236259
return pluginConf, nil
237260
}
@@ -250,6 +273,7 @@ func (ctx *pluginContext) NewHttpContext(contextID uint32) types.HttpContext {
250273
headers: headers,
251274
cluster: ctx.configuration.cluster,
252275
timeout: ctx.configuration.timeout,
276+
poke: ctx.configuration.poke,
253277
}
254278
}
255279

@@ -261,6 +285,7 @@ type httpOnDemand struct {
261285
headers [][2]string
262286
cluster string
263287
timeout uint32
288+
poke bool
264289
}
265290

266291
// Override types.DefaultHttpContext.
@@ -276,6 +301,9 @@ func (ctx *httpOnDemand) OnHttpRequestHeaders(numHeaders int, endOfStream bool)
276301

277302
proxywasm.LogInfof("http call dispatched to %s", ctx.cluster)
278303

304+
if ctx.poke {
305+
return types.ActionContinue
306+
}
279307
return types.ActionPause
280308
}
281309

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)