@@ -63,39 +63,62 @@ func main() {
63
63
}
64
64
}()
65
65
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 {
67
67
panic ("BUG: broken config validation - `listen_addr` is not configured" )
68
68
}
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 )
71
73
}
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 )
74
76
}
75
77
76
78
select {}
77
79
}
78
80
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 ) {
80
103
ln , err := net .Listen ("tcp4" , cfg .ListenAddr )
81
104
if err != nil {
82
105
log .Fatalf ("cannot listen for %q: %s" , cfg .ListenAddr , err )
83
106
}
84
107
tlsCfg := newTLSConfig (cfg )
85
108
tln := tls .NewListener (ln , tlsCfg )
86
109
log .Infof ("Serving https on %q" , cfg .ListenAddr )
87
- if err := listenAndServe (tln ); err != nil {
110
+ if err := listenAndServe (tln , maxResponseTime ); err != nil {
88
111
log .Fatalf ("TLS server error on %q: %s" , cfg .ListenAddr , err )
89
112
}
90
113
}
91
114
92
- func serve (cfg config.HTTP ) {
115
+ func serve (cfg config.HTTP , maxResponseTime time. Duration ) {
93
116
ln , err := net .Listen ("tcp4" , cfg .ListenAddr )
94
117
if err != nil {
95
118
log .Fatalf ("cannot listen for %q: %s" , cfg .ListenAddr , err )
96
119
}
97
120
log .Infof ("Serving http on %q" , cfg .ListenAddr )
98
- if err := listenAndServe (ln ); err != nil {
121
+ if err := listenAndServe (ln , maxResponseTime ); err != nil {
99
122
log .Fatalf ("HTTP server error on %q: %s" , cfg .ListenAddr , err )
100
123
}
101
124
}
@@ -144,12 +167,19 @@ func newTLSConfig(cfg config.HTTPS) *tls.Config {
144
167
return & tlsCfg
145
168
}
146
169
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
+
148
178
s := & http.Server {
149
179
TLSNextProto : make (map [string ]func (* http.Server , * tls.Conn , http.Handler )),
150
180
Handler : http .HandlerFunc (serveHTTP ),
151
181
ReadTimeout : time .Minute ,
152
- WriteTimeout : time . Minute ,
182
+ WriteTimeout : maxResponseTime ,
153
183
IdleTimeout : time .Minute * 10 ,
154
184
ErrorLog : log .ErrorLogger ,
155
185
}
@@ -196,7 +226,7 @@ func serveHTTP(rw http.ResponseWriter, r *http.Request) {
196
226
}
197
227
}
198
228
199
- func reloadConfig () (* config.Server , error ) {
229
+ func reloadConfig () (* config.Config , error ) {
200
230
if * configFile == "" {
201
231
log .Fatalf ("Missing -config flag" )
202
232
}
@@ -212,7 +242,7 @@ func reloadConfig() (*config.Server, error) {
212
242
allowedNetworksMetrics .Store (& cfg .Server .Metrics .AllowedNetworks )
213
243
log .SetDebug (cfg .LogDebug )
214
244
log .Infof ("Loaded config: \n %s" , cfg )
215
- return & cfg . Server , nil
245
+ return cfg , nil
216
246
}
217
247
218
248
func versionString () string {
0 commit comments