Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit b5fcd06

Browse files
committed
Results channel update, deadline handling, #10
Signed-off-by: tzununbekov <[email protected]>
1 parent 836c3ef commit b5fcd06

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![CircleCI](https://circleci.com/gh/triggermesh/aws-custom-runtime.svg?style=svg)](https://circleci.com/gh/triggermesh/aws-custom-runtime)
2+
13
## Running AWS Lambda Custom Runtime in Knative
24

35
In November 2018, AWS announced support for [Lambda custom runtime](https://aws.amazon.com/about-aws/whats-new/2018/11/aws-lambda-now-supports-custom-runtimes-and-layers/) using a straightforward [AWS lambda runtime API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html).

main.go

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,26 @@ import (
2929

3030
const (
3131
requestSizeLimit = 67108864
32+
functionTTL = 5e+9 // Funtions deadline, 5 seconds
3233
)
3334

3435
type message struct {
35-
id string
36-
data []byte
36+
id string
37+
deadline int64
38+
data []byte
3739
}
3840

3941
var (
4042
tasks chan message
41-
results chan message
43+
results map[string]chan message
4244

4345
awsEndpoint = "/2018-06-01/runtime"
4446
environment = map[string]string{
4547
"PATH": "/usr/local/bin:/usr/bin/:/bin:/opt/bin",
4648
"LD_LIBRARY_PATH": "/lib64:/usr/lib64:$LAMBDA_RUNTIME_DIR:$LAMBDA_RUNTIME_DIR/lib:$LAMBDA_TASK_ROOT:$LAMBDA_TASK_ROOT/lib:/opt/lib",
4749
"AWS_LAMBDA_RUNTIME_API": "127.0.0.1",
4850

49-
// Some dummy values required by Rust client
51+
// Some dummy values
5052
"AWS_LAMBDA_FUNCTION_NAME": "foo",
5153
"AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "128",
5254
"AWS_LAMBDA_FUNCTION_VERSION": "0.0.1",
@@ -75,17 +77,31 @@ func newTask(w http.ResponseWriter, r *http.Request) {
7577
}
7678
defer r.Body.Close()
7779

78-
id := strconv.Itoa(int(time.Now().UnixNano()))
79-
fmt.Printf("<- %s %s\n", id, body)
80-
tasks <- message{
81-
id: id,
82-
data: body,
80+
now := time.Now().UnixNano()
81+
task := message{
82+
id: fmt.Sprintf("%d", now),
83+
deadline: now + functionTTL,
84+
data: body,
85+
}
86+
fmt.Printf("<- %s %s\n", task.id, task.data)
87+
88+
results[task.id] = make(chan message)
89+
defer close(results[task.id])
90+
91+
tasks <- task
92+
93+
select {
94+
case <-time.After(time.Duration(functionTTL)):
95+
fmt.Printf("-> ! %s Deadline is reached\n", task.id)
96+
w.WriteHeader(http.StatusRequestTimeout)
97+
w.Write([]byte("Function deadline is reached"))
98+
case result := <-results[task.id]:
99+
fmt.Printf("Response in queue %s\n", result.id)
100+
fmt.Printf("-> %s %s\n", result.id, result.data)
101+
w.WriteHeader(http.StatusOK)
102+
w.Write(result.data)
83103
}
84104

85-
response := <-results
86-
fmt.Printf("-> %s %s\n", response.id, response.data)
87-
w.WriteHeader(http.StatusOK)
88-
w.Write(response.data)
89105
return
90106
}
91107

@@ -94,7 +110,7 @@ func getTask(w http.ResponseWriter, r *http.Request) {
94110

95111
// Dummy headers required by Rust client. Replace with something meaningful
96112
w.Header().Set("Lambda-Runtime-Aws-Request-Id", task.id)
97-
w.Header().Set("Lambda-Runtime-Deadline-Ms", "5543843233064023422")
113+
w.Header().Set("Lambda-Runtime-Deadline-Ms", strconv.Itoa(int(task.deadline)))
98114
w.Header().Set("Lambda-Runtime-Invoked-Function-Arn", "arn:aws:lambda:us-east-1:123456789012:function:custom-runtime")
99115
w.Header().Set("Lambda-Runtime-Trace-Id", "0")
100116

@@ -123,7 +139,7 @@ func postResult(w http.ResponseWriter, r *http.Request) {
123139
}
124140
defer r.Body.Close()
125141

126-
results <- message{
142+
results[id] <- message{
127143
id: id,
128144
data: data,
129145
}
@@ -139,8 +155,7 @@ func taskError(w http.ResponseWriter, r *http.Request) {
139155
return
140156
}
141157

142-
fmt.Printf("! %s %s\n", id, data)
143-
results <- message{
158+
results[id] <- message{
144159
id: id,
145160
data: data,
146161
}
@@ -166,11 +181,11 @@ func (h *maxBytesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
166181
r.Body = http.MaxBytesReader(w, r.Body, h.n)
167182
h.h.ServeHTTP(w, r)
168183
}
184+
169185
func main() {
170-
tasks = make(chan message)
171-
results = make(chan message)
186+
tasks = make(chan message, 100)
187+
results = make(map[string]chan message)
172188
defer close(tasks)
173-
defer close(results)
174189

175190
fmt.Println("Setup env")
176191
if err := setupEnv(); err != nil {

0 commit comments

Comments
 (0)