File tree Expand file tree Collapse file tree 4 files changed +88
-8
lines changed Expand file tree Collapse file tree 4 files changed +88
-8
lines changed Original file line number Diff line number Diff line change 55 "io/ioutil"
66 "log"
77 "net/http"
8+ "os"
9+ "strconv"
810 "time"
911
1012 "handler/function"
@@ -57,11 +59,29 @@ func makeRequestHandler() func(http.ResponseWriter, *http.Request) {
5759 }
5860}
5961
62+ func parseIntOrDurationValue (val string , fallback time.Duration ) time.Duration {
63+ if len (val ) > 0 {
64+ parsedVal , parseErr := strconv .Atoi (val )
65+ if parseErr == nil && parsedVal >= 0 {
66+ return time .Duration (parsedVal ) * time .Second
67+ }
68+ }
69+
70+ duration , durationErr := time .ParseDuration (val )
71+ if durationErr != nil {
72+ return fallback
73+ }
74+ return duration
75+ }
76+
6077func main () {
78+ readTimeout := parseIntOrDurationValue (os .Getenv ("read_timeout" ), 10 * time .Second )
79+ writeTimeout := parseIntOrDurationValue (os .Getenv ("write_timeout" ), 10 * time .Second )
80+
6181 s := & http.Server {
6282 Addr : fmt .Sprintf (":%d" , 8082 ),
63- ReadTimeout : 3 * time . Second ,
64- WriteTimeout : 3 * time . Second ,
83+ ReadTimeout : readTimeout ,
84+ WriteTimeout : writeTimeout ,
6585 MaxHeaderBytes : 1 << 20 , // Max header of 1MB
6686 }
6787
Original file line number Diff line number Diff line change 55 "io/ioutil"
66 "log"
77 "net/http"
8+ "os"
9+ "strconv"
810 "time"
911
1012 "handler/function"
@@ -57,11 +59,29 @@ func makeRequestHandler() func(http.ResponseWriter, *http.Request) {
5759 }
5860}
5961
62+ func parseIntOrDurationValue (val string , fallback time.Duration ) time.Duration {
63+ if len (val ) > 0 {
64+ parsedVal , parseErr := strconv .Atoi (val )
65+ if parseErr == nil && parsedVal >= 0 {
66+ return time .Duration (parsedVal ) * time .Second
67+ }
68+ }
69+
70+ duration , durationErr := time .ParseDuration (val )
71+ if durationErr != nil {
72+ return fallback
73+ }
74+ return duration
75+ }
76+
6077func main () {
78+ readTimeout := parseIntOrDurationValue (os .Getenv ("read_timeout" ), 10 * time .Second )
79+ writeTimeout := parseIntOrDurationValue (os .Getenv ("write_timeout" ), 10 * time .Second )
80+
6181 s := & http.Server {
6282 Addr : fmt .Sprintf (":%d" , 8082 ),
63- ReadTimeout : 3 * time . Second ,
64- WriteTimeout : 3 * time . Second ,
83+ ReadTimeout : readTimeout ,
84+ WriteTimeout : writeTimeout ,
6585 MaxHeaderBytes : 1 << 20 , // Max header of 1MB
6686 }
6787
Original file line number Diff line number Diff line change @@ -4,17 +4,37 @@ import (
44 "fmt"
55 "log"
66 "net/http"
7+ "os"
8+ "strconv"
79 "time"
810
911 "handler/function"
1012 //"github.com/openfaas-incubator/golang-http-template/template/golang-middleware/function"
1113)
1214
15+ func parseIntOrDurationValue (val string , fallback time.Duration ) time.Duration {
16+ if len (val ) > 0 {
17+ parsedVal , parseErr := strconv .Atoi (val )
18+ if parseErr == nil && parsedVal >= 0 {
19+ return time .Duration (parsedVal ) * time .Second
20+ }
21+ }
22+
23+ duration , durationErr := time .ParseDuration (val )
24+ if durationErr != nil {
25+ return fallback
26+ }
27+ return duration
28+ }
29+
1330func main () {
31+ readTimeout := parseIntOrDurationValue (os .Getenv ("read_timeout" ), 10 * time .Second )
32+ writeTimeout := parseIntOrDurationValue (os .Getenv ("write_timeout" ), 10 * time .Second )
33+
1434 s := & http.Server {
1535 Addr : fmt .Sprintf (":%d" , 8082 ),
16- ReadTimeout : 3 * time . Second ,
17- WriteTimeout : 3 * time . Second ,
36+ ReadTimeout : readTimeout ,
37+ WriteTimeout : writeTimeout ,
1838 MaxHeaderBytes : 1 << 20 , // Max header of 1MB
1939 }
2040
Original file line number Diff line number Diff line change @@ -4,17 +4,37 @@ import (
44 "fmt"
55 "log"
66 "net/http"
7+ "os"
8+ "strconv"
79 "time"
810
911 "handler/function"
1012 //"github.com/openfaas-incubator/golang-http-template/template/golang-middleware/function"
1113)
1214
15+ func parseIntOrDurationValue (val string , fallback time.Duration ) time.Duration {
16+ if len (val ) > 0 {
17+ parsedVal , parseErr := strconv .Atoi (val )
18+ if parseErr == nil && parsedVal >= 0 {
19+ return time .Duration (parsedVal ) * time .Second
20+ }
21+ }
22+
23+ duration , durationErr := time .ParseDuration (val )
24+ if durationErr != nil {
25+ return fallback
26+ }
27+ return duration
28+ }
29+
1330func main () {
31+ readTimeout := parseIntOrDurationValue (os .Getenv ("read_timeout" ), 10 * time .Second )
32+ writeTimeout := parseIntOrDurationValue (os .Getenv ("write_timeout" ), 10 * time .Second )
33+
1434 s := & http.Server {
1535 Addr : fmt .Sprintf (":%d" , 8082 ),
16- ReadTimeout : 3 * time . Second ,
17- WriteTimeout : 3 * time . Second ,
36+ ReadTimeout : readTimeout ,
37+ WriteTimeout : writeTimeout ,
1838 MaxHeaderBytes : 1 << 20 , // Max header of 1MB
1939 }
2040
You can’t perform that action at this time.
0 commit comments