|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "fmt"
|
5 | 6 | "io/ioutil"
|
6 | 7 | "log"
|
7 | 8 | "net/http"
|
8 | 9 | "os"
|
| 10 | + "os/signal" |
9 | 11 | "strconv"
|
| 12 | + "sync/atomic" |
| 13 | + "syscall" |
10 | 14 | "time"
|
11 | 15 |
|
12 | 16 | "handler/function"
|
13 |
| - // "github.com/alexellis/golang-http-template/template/golang-http/function" |
| 17 | + |
14 | 18 | handler "github.com/openfaas-incubator/go-function-sdk"
|
15 | 19 | )
|
16 | 20 |
|
| 21 | +var ( |
| 22 | + acceptingConnections int32 |
| 23 | +) |
| 24 | + |
| 25 | +const defaultTimeout = 10 * time.Second |
| 26 | + |
| 27 | +func main() { |
| 28 | + readTimeout := parseIntOrDurationValue(os.Getenv("read_timeout"), defaultTimeout) |
| 29 | + writeTimeout := parseIntOrDurationValue(os.Getenv("write_timeout"), defaultTimeout) |
| 30 | + |
| 31 | + s := &http.Server{ |
| 32 | + Addr: fmt.Sprintf(":%d", 8082), |
| 33 | + ReadTimeout: readTimeout, |
| 34 | + WriteTimeout: writeTimeout, |
| 35 | + MaxHeaderBytes: 1 << 20, // Max header of 1MB |
| 36 | + } |
| 37 | + |
| 38 | + http.HandleFunc("/", makeRequestHandler()) |
| 39 | + listenUntilShutdown(s, writeTimeout) |
| 40 | +} |
| 41 | + |
| 42 | +func listenUntilShutdown(s *http.Server, shutdownTimeout time.Duration) { |
| 43 | + idleConnsClosed := make(chan struct{}) |
| 44 | + go func() { |
| 45 | + sig := make(chan os.Signal, 1) |
| 46 | + signal.Notify(sig, syscall.SIGTERM) |
| 47 | + |
| 48 | + <-sig |
| 49 | + |
| 50 | + log.Printf("[entrypoint] SIGTERM received.. shutting down server in %s\n", shutdownTimeout.String()) |
| 51 | + |
| 52 | + <-time.Tick(shutdownTimeout) |
| 53 | + |
| 54 | + if err := s.Shutdown(context.Background()); err != nil { |
| 55 | + log.Printf("[entrypoint] Error in Shutdown: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + log.Printf("[entrypoint] No new connections allowed. Exiting in: %s\n", shutdownTimeout.String()) |
| 59 | + |
| 60 | + <-time.Tick(shutdownTimeout) |
| 61 | + |
| 62 | + close(idleConnsClosed) |
| 63 | + }() |
| 64 | + |
| 65 | + // Run the HTTP server in a separate go-routine. |
| 66 | + go func() { |
| 67 | + if err := s.ListenAndServe(); err != http.ErrServerClosed { |
| 68 | + log.Printf("[entrypoint] Error ListenAndServe: %v", err) |
| 69 | + close(idleConnsClosed) |
| 70 | + } |
| 71 | + }() |
| 72 | + |
| 73 | + atomic.StoreInt32(&acceptingConnections, 1) |
| 74 | + |
| 75 | + <-idleConnsClosed |
| 76 | +} |
| 77 | + |
17 | 78 | func makeRequestHandler() func(http.ResponseWriter, *http.Request) {
|
18 | 79 | return func(w http.ResponseWriter, r *http.Request) {
|
19 | 80 | var input []byte
|
@@ -74,18 +135,3 @@ func parseIntOrDurationValue(val string, fallback time.Duration) time.Duration {
|
74 | 135 | }
|
75 | 136 | return duration
|
76 | 137 | }
|
77 |
| - |
78 |
| -func main() { |
79 |
| - readTimeout := parseIntOrDurationValue(os.Getenv("read_timeout"), 10*time.Second) |
80 |
| - writeTimeout := parseIntOrDurationValue(os.Getenv("write_timeout"), 10*time.Second) |
81 |
| - |
82 |
| - s := &http.Server{ |
83 |
| - Addr: fmt.Sprintf(":%d", 8082), |
84 |
| - ReadTimeout: readTimeout, |
85 |
| - WriteTimeout: writeTimeout, |
86 |
| - MaxHeaderBytes: 1 << 20, // Max header of 1MB |
87 |
| - } |
88 |
| - |
89 |
| - http.HandleFunc("/", makeRequestHandler()) |
90 |
| - log.Fatal(s.ListenAndServe()) |
91 |
| -} |
|
0 commit comments