Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sleep/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module handler/function

go 1.18
67 changes: 47 additions & 20 deletions sleep/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,85 @@ import (
"fmt"
"log"
"math/rand"
"net/http"
"os"
"time"
)

var r *rand.Rand
var (
random *rand.Rand
defaultDuration time.Duration = time.Second * 2
)

func init() {
random = rand.New(rand.NewSource(time.Now().Unix()))

r = rand.New(rand.NewSource(time.Now().Unix()))
if val, ok := os.LookupEnv("sleep_duration"); ok && len(val) > 0 {
var err error
defaultDuration, err = time.ParseDuration(val)
if err != nil {
log.Fatalf("Error parsing sleep_duration environment variable: %v", err)
}
}
}

// Handle a serverless request
// 1. When no headers are given, sleep for the environment variable: sleep_duration.
// 2. When an X-Sleep header is given, sleep for that amount of time.
// 3. When the X-Min-Sleep and X-Max-Sleep headers are given, sleep for a random amount
// of time between those two figures
func Handle(req []byte) string {

if v := os.Getenv("Http_Path"); v == "/_/ready" {
return "ok"
}

if minV, ok := os.LookupEnv("Http_X_Min_Sleep"); ok && len(minV) > 0 {
if maxV, ok := os.LookupEnv("Http_X_Max_Sleep"); ok && len(maxV) > 0 {
minSleep, _ := time.ParseDuration(minV)
maxSleep, _ := time.ParseDuration(maxV)
func Handle(w http.ResponseWriter, r *http.Request) {
if minV := r.Header.Get("X-Min-Sleep"); len(minV) > 0 {
if maxV := r.Header.Get("X-Max-Sleep"); len(maxV) > 0 {
minSleep, err := time.ParseDuration(minV)
if err != nil {
log.Printf("Error parsing X-Min-Sleep header: %v", err)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Error parsing X-Min-Sleep header: %v", err)
return
}
maxSleep, err := time.ParseDuration(maxV)
if err != nil {
log.Printf("Error parsing X-Max-Sleep header: %v", err)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Error parsing X-Max-Sleep header: %v", err)
return
}

minMs := minSleep.Milliseconds()
maxMs := maxSleep.Milliseconds()

randMs := r.Int63n(maxMs-minMs) + minMs
randMs := random.Int63n(maxMs-minMs) + minMs

sleepDuration, _ := time.ParseDuration(fmt.Sprintf("%dms", randMs))

log.Printf("Start sleep for: %fs\n", sleepDuration.Seconds())
time.Sleep(sleepDuration)
log.Printf("Sleep done for: %fs\n", sleepDuration.Seconds())
return fmt.Sprintf("Slept for: %fs", sleepDuration.Seconds())

w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Slept for: %fs", sleepDuration.Seconds())
return
}
}

sleepDuration := time.Second * 2
sleepDuration := defaultDuration

if val, ok := os.LookupEnv("Http_X_Sleep"); ok && len(val) > 0 {
sleepDuration, _ = time.ParseDuration(val)
} else if val, ok := os.LookupEnv("sleep_duration"); ok && len(val) > 0 {
sleepDuration, _ = time.ParseDuration(val)
var err error
if val := r.Header.Get("X-Sleep"); len(val) > 0 {
sleepDuration, err = time.ParseDuration(val)
if err != nil {
log.Printf("Error parsing X-Sleep header: %v", err)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Error parsing X-Sleep header: %v", err)
return
}
}

log.Printf("Start sleep for: %fs\n", sleepDuration.Seconds())
time.Sleep(sleepDuration)
log.Printf("Sleep done for: %fs\n", sleepDuration.Seconds())

return fmt.Sprintf("Slept for: %fs", sleepDuration.Seconds())
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Slept for: %fs", sleepDuration.Seconds())
}
6 changes: 2 additions & 4 deletions stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,11 @@ functions:
com.openfaas.ui.ext: "mp4"

sleep:
lang: go
lang: golang-middleware
handler: ./sleep
image: ${SERVER:-ghcr.io}/${OWNER:-openfaas}/sleep:${TAG:-latest}
environment:
sleep_duration: 2s
combine_output: false

haveibeenpwned:
lang: go
Expand All @@ -100,13 +99,12 @@ functions:
lang: golang-middleware
handler: ./markdown
image: ${SERVER:-ghcr.io}/${OWNER:-openfaas}/markdown-fn:${TAG:-latest}

nvidia-smi:
lang: dockerfile
handler: ./nvidia-smi
image: ${SERVER:-ghcr.io}/${OWNER:-openfaas}/nvidia-smi:${TAG:-latest}


configuration:
templates:
- name: golang-middleware
Expand Down