Skip to content

Commit 3efb9bb

Browse files
committed
Calculate the maximum response time from the initial config
1 parent 0b4b0ce commit 3efb9bb

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

main.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,39 +63,62 @@ func main() {
6363
}
6464
}()
6565

66-
if len(cfg.HTTP.ListenAddr) == 0 && len(cfg.HTTPS.ListenAddr) == 0 {
66+
if len(cfg.Server.HTTP.ListenAddr) == 0 && len(cfg.Server.HTTPS.ListenAddr) == 0 {
6767
panic("BUG: broken config validation - `listen_addr` is not configured")
6868
}
69-
if len(cfg.HTTPS.ListenAddr) != 0 {
70-
go serveTLS(cfg.HTTPS)
69+
70+
maxResponseTime := getMaxResponseTime(cfg)
71+
if len(cfg.Server.HTTPS.ListenAddr) != 0 {
72+
go serveTLS(cfg.Server.HTTPS, maxResponseTime)
7173
}
72-
if len(cfg.HTTP.ListenAddr) != 0 {
73-
go serve(cfg.HTTP)
74+
if len(cfg.Server.HTTP.ListenAddr) != 0 {
75+
go serve(cfg.Server.HTTP, maxResponseTime)
7476
}
7577

7678
select {}
7779
}
7880

79-
func serveTLS(cfg config.HTTPS) {
81+
// getMaxResponseTime returns the maximum possible response time
82+
// for the given cfg.
83+
func getMaxResponseTime(cfg *config.Config) time.Duration {
84+
var d time.Duration
85+
for _, u := range cfg.Users {
86+
ud := u.MaxExecutionTime + u.MaxQueueTime
87+
if ud > d {
88+
d = ud
89+
}
90+
}
91+
for _, c := range cfg.Clusters {
92+
for _, cu := range c.ClusterUsers {
93+
cud := cu.MaxExecutionTime + cu.MaxQueueTime
94+
if cud > d {
95+
d = cud
96+
}
97+
}
98+
}
99+
return d
100+
}
101+
102+
func serveTLS(cfg config.HTTPS, maxResponseTime time.Duration) {
80103
ln, err := net.Listen("tcp4", cfg.ListenAddr)
81104
if err != nil {
82105
log.Fatalf("cannot listen for %q: %s", cfg.ListenAddr, err)
83106
}
84107
tlsCfg := newTLSConfig(cfg)
85108
tln := tls.NewListener(ln, tlsCfg)
86109
log.Infof("Serving https on %q", cfg.ListenAddr)
87-
if err := listenAndServe(tln); err != nil {
110+
if err := listenAndServe(tln, maxResponseTime); err != nil {
88111
log.Fatalf("TLS server error on %q: %s", cfg.ListenAddr, err)
89112
}
90113
}
91114

92-
func serve(cfg config.HTTP) {
115+
func serve(cfg config.HTTP, maxResponseTime time.Duration) {
93116
ln, err := net.Listen("tcp4", cfg.ListenAddr)
94117
if err != nil {
95118
log.Fatalf("cannot listen for %q: %s", cfg.ListenAddr, err)
96119
}
97120
log.Infof("Serving http on %q", cfg.ListenAddr)
98-
if err := listenAndServe(ln); err != nil {
121+
if err := listenAndServe(ln, maxResponseTime); err != nil {
99122
log.Fatalf("HTTP server error on %q: %s", cfg.ListenAddr, err)
100123
}
101124
}
@@ -144,12 +167,19 @@ func newTLSConfig(cfg config.HTTPS) *tls.Config {
144167
return &tlsCfg
145168
}
146169

147-
func listenAndServe(ln net.Listener) error {
170+
func listenAndServe(ln net.Listener, maxResponseTime time.Duration) error {
171+
if maxResponseTime < 0 {
172+
maxResponseTime = 0
173+
}
174+
// Give an additional minute for the maximum response time,
175+
// so the response body may be sent to the requester.
176+
maxResponseTime += time.Minute
177+
148178
s := &http.Server{
149179
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
150180
Handler: http.HandlerFunc(serveHTTP),
151181
ReadTimeout: time.Minute,
152-
WriteTimeout: time.Minute,
182+
WriteTimeout: maxResponseTime,
153183
IdleTimeout: time.Minute * 10,
154184
ErrorLog: log.ErrorLogger,
155185
}
@@ -196,7 +226,7 @@ func serveHTTP(rw http.ResponseWriter, r *http.Request) {
196226
}
197227
}
198228

199-
func reloadConfig() (*config.Server, error) {
229+
func reloadConfig() (*config.Config, error) {
200230
if *configFile == "" {
201231
log.Fatalf("Missing -config flag")
202232
}
@@ -212,7 +242,7 @@ func reloadConfig() (*config.Server, error) {
212242
allowedNetworksMetrics.Store(&cfg.Server.Metrics.AllowedNetworks)
213243
log.SetDebug(cfg.LogDebug)
214244
log.Infof("Loaded config: \n%s", cfg)
215-
return &cfg.Server, nil
245+
return cfg, nil
216246
}
217247

218248
func versionString() string {

main_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net"
88
"net/http"
99
"testing"
10+
"time"
1011
)
1112

1213
var tlsClient = &http.Client{Transport: &http.Transport{
@@ -20,14 +21,14 @@ func startTLS() (net.Listener, chan struct{}) {
2021
}
2122
done := make(chan struct{})
2223

23-
ln, err := net.Listen("tcp4", cfg.HTTPS.ListenAddr)
24+
ln, err := net.Listen("tcp4", cfg.Server.HTTPS.ListenAddr)
2425
if err != nil {
25-
panic(fmt.Sprintf("cannot listen for %q: %s", cfg.HTTPS.ListenAddr, err))
26+
panic(fmt.Sprintf("cannot listen for %q: %s", cfg.Server.HTTPS.ListenAddr, err))
2627
}
27-
tlsCfg := newTLSConfig(cfg.HTTPS)
28+
tlsCfg := newTLSConfig(cfg.Server.HTTPS)
2829
tln := tls.NewListener(ln, tlsCfg)
2930
go func() {
30-
listenAndServe(tln)
31+
listenAndServe(tln, time.Minute)
3132
close(done)
3233
}()
3334
return tln, done
@@ -40,12 +41,12 @@ func startHTTP() (net.Listener, chan struct{}) {
4041
}
4142
done := make(chan struct{})
4243

43-
ln, err := net.Listen("tcp4", cfg.HTTP.ListenAddr)
44+
ln, err := net.Listen("tcp4", cfg.Server.HTTP.ListenAddr)
4445
if err != nil {
45-
panic(fmt.Sprintf("cannot listen for %q: %s", cfg.HTTP.ListenAddr, err))
46+
panic(fmt.Sprintf("cannot listen for %q: %s", cfg.Server.HTTP.ListenAddr, err))
4647
}
4748
go func() {
48-
listenAndServe(ln)
49+
listenAndServe(ln, time.Minute)
4950
close(done)
5051
}()
5152
return ln, done

0 commit comments

Comments
 (0)