merge poc
This commit is contained in:
43
main.go
43
main.go
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"flag"
|
||||
@@ -12,13 +11,12 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/open-policy-agent/gatekeeper-external-data-provider/pkg/handler"
|
||||
"github.com/open-policy-agent/gatekeeper-external-data-provider/pkg/utils"
|
||||
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
timeout = 1 * time.Second
|
||||
// timeout = 15 * time.Second
|
||||
defaultPort = 8090
|
||||
|
||||
certName = "tls.crt"
|
||||
@@ -41,12 +39,12 @@ func init() {
|
||||
|
||||
func main() {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", processTimeout(handler.Handler, timeout))
|
||||
mux.HandleFunc("/", handler.Handler)
|
||||
|
||||
server := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", port),
|
||||
Handler: mux,
|
||||
ReadHeaderTimeout: time.Duration(5) * time.Second,
|
||||
ReadHeaderTimeout: time.Duration(15) * time.Second,
|
||||
}
|
||||
|
||||
config := &tls.Config{
|
||||
@@ -83,23 +81,24 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func processTimeout(h http.HandlerFunc, duration time.Duration) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), duration)
|
||||
defer cancel()
|
||||
// TODO: root cause Content-Length header error
|
||||
// func processTimeout(h http.HandlerFunc, duration time.Duration) http.HandlerFunc {
|
||||
// return func(w http.ResponseWriter, r *http.Request) {
|
||||
// ctx, cancel := context.WithTimeout(r.Context(), duration)
|
||||
// defer cancel()
|
||||
|
||||
r = r.WithContext(ctx)
|
||||
// r = r.WithContext(ctx)
|
||||
|
||||
processDone := make(chan bool)
|
||||
go func() {
|
||||
h(w, r)
|
||||
processDone <- true
|
||||
}()
|
||||
// processDone := make(chan bool)
|
||||
// go func() {
|
||||
// h(w, r)
|
||||
// processDone <- true
|
||||
// }()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
utils.SendResponse(nil, "operation timed out", w)
|
||||
case <-processDone:
|
||||
}
|
||||
}
|
||||
}
|
||||
// select {
|
||||
// case <-ctx.Done():
|
||||
// utils.SendResponse(nil, "operation timed out", w)
|
||||
// case <-processDone:
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user