Skip to content

Commit 63646e5

Browse files
viveksynghalexellis
authored andcommitted
Add async endpoint support
This commit adds async endpoint support to cron connector which can be enabled using `asynchronous_invocation` environment variable. Fixes: #11 Signed-off-by: Vivek Singh <[email protected]>
1 parent 549c1dc commit 63646e5

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

main.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ func main() {
2525
}
2626

2727
sha, ver := version.GetReleaseInfo()
28-
log.Printf("Version: %s\tCommit: %s", sha, ver)
28+
log.Printf("Version: %s\tCommit: %s\n", sha, ver)
29+
log.Printf("Gateway URL: %s", config.GatewayURL)
30+
log.Printf("Async Invocation: %v", config.AsyncFunctionInvocation)
2931

30-
invoker := types.NewInvoker(config.GatewayURL, types.MakeClient(config.UpstreamTimeout), config.PrintResponse)
32+
invoker := types.NewInvoker(gatewayRoute(config), types.MakeClient(config.UpstreamTimeout), config.PrintResponse)
3133
cronScheduler := cfunction.NewScheduler()
3234
topic := "cron-function"
3335
interval := time.Second * 10
@@ -40,17 +42,30 @@ func main() {
4042
}
4143
}
4244

45+
func gatewayRoute(config *types.ControllerConfig) string {
46+
if config.AsyncFunctionInvocation {
47+
return fmt.Sprintf("%s/%s", config.GatewayURL, "async-function")
48+
}
49+
return fmt.Sprintf("%s/%s", config.GatewayURL, "function")
50+
}
51+
4352
func getControllerConfig() (*types.ControllerConfig, error) {
4453
gURL, ok := os.LookupEnv("gateway_url")
4554

4655
if !ok {
4756
return nil, fmt.Errorf("Gateway URL not set")
4857
}
4958

59+
asynchronousInvocation := false
60+
if val, exists := os.LookupEnv("asynchronous_invocation"); exists {
61+
asynchronousInvocation = (val == "1" || val == "true")
62+
}
63+
5064
return &types.ControllerConfig{
51-
RebuildInterval: time.Millisecond * 1000,
52-
GatewayURL: gURL,
53-
PrintResponse: true,
65+
RebuildInterval: time.Millisecond * 1000,
66+
GatewayURL: gURL,
67+
PrintResponse: true,
68+
AsyncFunctionInvocation: asynchronousInvocation,
5469
}, nil
5570
}
5671

main_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package main
55
import (
66
"testing"
77

8+
"github.com/openfaas/connector-sdk/types"
89
cfunction "github.com/openfaas/cron-connector/types"
910
ptypes "github.com/openfaas/faas-provider/types"
1011
)
@@ -68,3 +69,35 @@ func TestNamespaceFuncs(t *testing.T) {
6869
t.Error("function should be left as it is")
6970
}
7071
}
72+
73+
func TestGatewayRoute_Async(t *testing.T) {
74+
testscases := []struct {
75+
GatewayURL string
76+
ExpectedGatewayURL string
77+
AsyncFunctionInvocation bool
78+
}{
79+
{
80+
GatewayURL: "http://localhost:8080",
81+
AsyncFunctionInvocation: true,
82+
ExpectedGatewayURL: "http://localhost:8080/async-function",
83+
},
84+
{
85+
GatewayURL: "http://localhost:8080",
86+
AsyncFunctionInvocation: false,
87+
ExpectedGatewayURL: "http://localhost:8080/function",
88+
},
89+
}
90+
91+
for _, test := range testscases {
92+
config := &types.ControllerConfig{
93+
GatewayURL: test.GatewayURL,
94+
AsyncFunctionInvocation: test.AsyncFunctionInvocation,
95+
}
96+
97+
val := gatewayRoute(config)
98+
if val != test.ExpectedGatewayURL {
99+
t.Errorf("expected: %s, got: %s", test.ExpectedGatewayURL, val)
100+
}
101+
}
102+
103+
}

types/cron_function.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func ToCronFunction(f ptypes.FunctionStatus, namespace string, topic string) (Cr
6666

6767
// InvokeFunction Invokes the cron function
6868
func (c CronFunction) InvokeFunction(i *types.Invoker) (*[]byte, error) {
69-
gwURL := fmt.Sprintf("%s/function/%s.%s", i.GatewayURL, c.Name, c.Namespace)
69+
gwURL := fmt.Sprintf("%s/%s.%s", i.GatewayURL, c.Name, c.Namespace)
7070
reader := bytes.NewReader(make([]byte, 0))
7171
httpReq, _ := http.NewRequest(http.MethodPost, gwURL, reader)
7272

0 commit comments

Comments
 (0)