-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (67 loc) · 2.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/kyma-project/kyma/components/event-bus/cmd/event-publish-service/application"
"github.com/kyma-project/kyma/components/event-bus/cmd/event-publish-service/httpserver"
"github.com/kyma-project/kyma/components/event-bus/cmd/event-publish-service/publisher"
"github.com/kyma-project/kyma/components/event-bus/internal/knative/publish/opts"
knative "github.com/kyma-project/kyma/components/event-bus/internal/knative/util"
"github.com/kyma-project/kyma/components/event-bus/internal/trace"
)
const (
shutdownTimeout = time.Minute
)
func main() {
// read application options from the cli args
options := opts.ParseFlags()
// init the knative lib
knativeLib, err := knative.GetKnativeLib()
if err != nil {
log.Fatalf("failed to start knative publish application: %v", err)
}
// init the knative publisher
knativePublisher := publisher.NewKnativePublisher()
// init the tracer
tracer := trace.StartNewTracer(options.TraceOptions)
defer tracer.Stop()
// start the Knative publish application
app := application.StartKnativePublishApplication(options, knativeLib, &knativePublisher, &tracer)
log.Println("started knative publish application")
// print the application startup options
options.Print()
// init the HTTP max concurrent requests
handler, semaphore := limit(app.ServeMux(), &options.MaxRequests)
defer close(semaphore)
// start the HTTP server
server := httpserver.NewHTTPServer(&options.Port, &handler)
go server.Start()
metricsServeMux := http.NewServeMux()
metricsServeMux.Handle("/metrics", promhttp.Handler())
metricsServer := http.Server{
Addr: fmt.Sprintf(":%d", options.MonitoringPort),
Handler: metricsServeMux,
}
log.Printf("HTTP metrics server starting on %v", options.MonitoringPort)
go func() {
if err := metricsServer.ListenAndServe(); err != nil {
log.Fatalf("HTTP metrics server failed with error:\n%v", err)
}
}()
// shutdown the HTTP server gracefully
server.Shutdown(shutdownTimeout)
}
func limit(serveMux *http.ServeMux, maxRequests *int) (http.Handler, chan bool) {
// create a channel to limit the max requests handled concurrently
semaphore := make(chan bool, *maxRequests)
// init the handler function
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
semaphore <- true
defer func() { <-semaphore }()
serveMux.ServeHTTP(w, r)
})
return handler, semaphore
}