Skip to content

Commit aaedd5e

Browse files
authored
Merge pull request #226 from roadrunner-server/chore/modernize
chore: modernization
2 parents 19cefe5 + 6bdb86d commit aaedd5e

25 files changed

Lines changed: 446 additions & 228 deletions

acme/acme.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ func IssueCertificates(cacheDir, email, challengeType string, domains []string,
7171

7272
cfg.Issuers = append(cfg.Issuers, myAcme)
7373

74-
for i := range domains {
75-
err := cfg.ObtainCertAsync(context.Background(), domains[i])
74+
for _, domain := range domains {
75+
err := cfg.ObtainCertAsync(context.Background(), domain)
7676
if err != nil {
7777
return nil, err
7878
}

config/uploads.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ func (cfg *Uploads) InitDefaults() error {
3030
cfg.Forbidden = make(map[string]struct{})
3131
cfg.Allowed = make(map[string]struct{})
3232

33-
for i := range cfg.Forbid {
34-
cfg.Forbidden[cfg.Forbid[i]] = struct{}{}
33+
for _, ext := range cfg.Forbid {
34+
cfg.Forbidden[ext] = struct{}{}
3535
}
3636

37-
for i := range cfg.Allow {
38-
cfg.Allowed[cfg.Allow[i]] = struct{}{}
37+
for _, ext := range cfg.Allow {
38+
cfg.Allowed[ext] = struct{}{}
3939
}
4040

4141
for k := range cfg.Forbidden {

go.work.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,6 +2087,8 @@ github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw
20872087
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
20882088
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
20892089
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
2090+
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
2091+
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
20902092
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
20912093
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
20922094
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c h1:964Od4U6p2jUkFxvCydnIczKteheJEzHRToSGK3Bnlw=
@@ -2566,6 +2568,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
25662568
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
25672569
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
25682570
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=
2571+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
25692572
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
25702573
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
25712574
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
@@ -2682,6 +2685,8 @@ go.temporal.io/api v1.28.0/go.mod h1:sAtVCXkwNaCtHVMP6B/FlK8PcEnaDjJ+KHCwS/ufscI
26822685
go.temporal.io/api v1.30.0 h1:6U/6lgpXf4UCywYxL3YfssqeI7IQnt9yNzwlfozUi2o=
26832686
go.temporal.io/api v1.30.0/go.mod h1:PIZ+UyGTMh3HJFauMysfanNBAh3f0jRf1xydrhTEcSQ=
26842687
go.temporal.io/api v1.57.0/go.mod h1:iaxoP/9OXMJcQkETTECfwYq4cw/bj4nwov8b3ZLVnXM=
2688+
go.temporal.io/api v1.62.2 h1:jFhIzlqNyJsJZTiCRQmTIMv6OTQ5BZ57z8gbgLGMaoo=
2689+
go.temporal.io/api v1.62.2/go.mod h1:iaxoP/9OXMJcQkETTECfwYq4cw/bj4nwov8b3ZLVnXM=
26852690
go.temporal.io/sdk v1.26.0-rc.2 h1:0NX4wR2qwD6xCv+JNhZdViamsITMYWzZkUqBJEIVHBw=
26862691
go.temporal.io/sdk v1.26.0-rc.2/go.mod h1:HDr8fIWJ/HF8dJwTPgOayI8PYB5WoVIxUMjzE78M2ng=
26872692
go.temporal.io/sdk/contrib/opentelemetry v0.4.0 h1:Ddx+39cESh4CNFI6cy8TI1OBJC8MUQUDzt6TpIJJjPQ=
@@ -2871,6 +2876,7 @@ golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI=
28712876
golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU=
28722877
golang.org/x/oauth2 v0.31.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
28732878
golang.org/x/oauth2 v0.32.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
2879+
golang.org/x/oauth2 v0.34.0 h1:hqK/t4AKgbqWkdkcAeI8XLmbK+4m4G5YeQRrmiotGlw=
28742880
golang.org/x/oauth2 v0.34.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
28752881
golang.org/x/oauth2 v0.35.0 h1:Mv2mzuHuZuY2+bkyWXIHMfhNdJAdwW3FuWeCPYN5GVQ=
28762882
golang.org/x/oauth2 v0.35.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
@@ -3231,6 +3237,7 @@ google.golang.org/genproto/googleapis/api v0.0.0-20250929231259-57b25ae835d4/go.
32313237
google.golang.org/genproto/googleapis/api v0.0.0-20251103181224-f26f9409b101/go.mod h1:E17fc4PDhkr22dE3RgnH2hEubUaky6ZwW4VhANxyspg=
32323238
google.golang.org/genproto/googleapis/api v0.0.0-20251124214823-79d6a2a48846/go.mod h1:Fk4kyraUvqD7i5H6S43sj2W98fbZa75lpZz/eUyhfO0=
32333239
google.golang.org/genproto/googleapis/api v0.0.0-20251202230838-ff82c1b0f217/go.mod h1:+rXWjjaukWZun3mLfjmVnQi18E1AsFbDN9QdJ5YXLto=
3240+
google.golang.org/genproto/googleapis/api v0.0.0-20260217215200-42d3e9bedb6d h1:EocjzKLywydp5uZ5tJ79iP6Q0UjDnyiHkGRWxuPBP8s=
32343241
google.golang.org/genproto/googleapis/api v0.0.0-20260217215200-42d3e9bedb6d/go.mod h1:48U2I+QQUYhsFrg2SY6r+nJzeOtjey7j//WBESw+qyQ=
32353242
google.golang.org/genproto/googleapis/bytestream v0.0.0-20230530153820-e85fd2cbaebc h1:g3hIDl0jRNd9PPTs2uBzYuaD5mQuwOkZY0vSc0LR32o=
32363243
google.golang.org/genproto/googleapis/bytestream v0.0.0-20240125205218-1f4bbc51befe h1:weYsP+dNijSQVoLAb5bpUos3ciBpNU/NEVlHFKrk8pg=
@@ -3257,6 +3264,7 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20251103181224-f26f9409b101/go.
32573264
google.golang.org/genproto/googleapis/rpc v0.0.0-20251124214823-79d6a2a48846/go.mod h1:7i2o+ce6H/6BluujYR+kqX3GKH+dChPTQU19wjRPiGk=
32583265
google.golang.org/genproto/googleapis/rpc v0.0.0-20251202230838-ff82c1b0f217/go.mod h1:7i2o+ce6H/6BluujYR+kqX3GKH+dChPTQU19wjRPiGk=
32593266
google.golang.org/genproto/googleapis/rpc v0.0.0-20260209200024-4cfbd4190f57/go.mod h1:j9x/tPzZkyxcgEFkiKEEGxfvyumM01BEtsW8xzOahRQ=
3267+
google.golang.org/genproto/googleapis/rpc v0.0.0-20260217215200-42d3e9bedb6d h1:t/LOSXPJ9R0B6fnZNyALBRfZBH0Uy0gT+uR+SJ6syqQ=
32603268
google.golang.org/genproto/googleapis/rpc v0.0.0-20260217215200-42d3e9bedb6d/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
32613269
google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
32623270
google.golang.org/grpc v1.16.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio=
@@ -3285,6 +3293,8 @@ google.golang.org/grpc v1.71.0/go.mod h1:H0GRtasmQOh9LkFoCPDu3ZrwUtD1YGE+b2vYBYd
32853293
google.golang.org/grpc v1.72.1/go.mod h1:wH5Aktxcg25y1I3w7H69nHfXdOG3UiadoBtjh3izSDM=
32863294
google.golang.org/grpc v1.75.1/go.mod h1:JtPAzKiq4v1xcAB2hydNlWI2RnF85XXcV0mhKXr2ecQ=
32873295
google.golang.org/grpc v1.78.0/go.mod h1:I47qjTo4OKbMkjA/aOOwxDIiPSBofUtQUI5EfpWvW7U=
3296+
google.golang.org/grpc v1.79.1 h1:zGhSi45ODB9/p3VAawt9a+O/MULLl9dpizzNNpq7flY=
3297+
google.golang.org/grpc v1.79.1/go.mod h1:KmT0Kjez+0dde/v2j9vzwoAScgEPx/Bw1CYChhHLrHQ=
32883298
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0 h1:M1YKkFIboKNieVO5DLUEVzQfGwJD30Nv2jfUgzb5UcE=
32893299
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
32903300
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=

handler/handler_test.go

Lines changed: 125 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestServeHTTP_InvalidMultipart_Returns400(t *testing.T) {
5959
h := newTestHandler(t, defaultCfg(), nil)
6060

6161
// Boundary declared in header but body has no valid multipart parts → EOF.
62-
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
62+
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/", strings.NewReader(""))
6363
req.Header.Set("Content-Type", "multipart/form-data; boundary=1111")
6464

6565
rr := httptest.NewRecorder()
@@ -73,7 +73,7 @@ func TestServeHTTP_InvalidMultipart_Returns400(t *testing.T) {
7373
func TestServeHTTP_StreamBody_MaxBytesExceeded_Returns413(t *testing.T) {
7474
h := newTestHandler(t, defaultCfg(), nil)
7575

76-
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("this body is too long"))
76+
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/", strings.NewReader("this body is too long"))
7777
req.Header.Set("Content-Type", "application/json")
7878

7979
rr := httptest.NewRecorder()
@@ -92,7 +92,7 @@ func TestServeHTTP_TruncatedMultipart_Returns400(t *testing.T) {
9292

9393
// Multipart body with an open part but no closing boundary → ErrUnexpectedEOF.
9494
body := "--1111\r\nContent-Disposition: form-data; name=\"f\"\r\n\r\nval"
95-
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body))
95+
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/", strings.NewReader(body))
9696
req.Header.Set("Content-Type", "multipart/form-data; boundary=1111")
9797

9898
rr := httptest.NewRecorder()
@@ -150,7 +150,7 @@ func TestHandleError_DebugMode_WritesEscapedError(t *testing.T) {
150150
}
151151

152152
func TestURI_PlainHTTP(t *testing.T) {
153-
r := httptest.NewRequest(http.MethodGet, "/path?q=1", nil)
153+
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/path?q=1", nil)
154154
r.Host = "example.com"
155155

156156
got := URI(r)
@@ -161,7 +161,7 @@ func TestURI_PlainHTTP(t *testing.T) {
161161
}
162162

163163
func TestURI_TLSRequest_HTTPSScheme(t *testing.T) {
164-
r := httptest.NewRequest(http.MethodGet, "/path?q=1", nil)
164+
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/path?q=1", nil)
165165
r.Host = "example.com"
166166
r.TLS = &tls.ConnectionState{}
167167

@@ -173,7 +173,7 @@ func TestURI_TLSRequest_HTTPSScheme(t *testing.T) {
173173
}
174174

175175
func TestURI_StripsCRLFInjection(t *testing.T) {
176-
r := httptest.NewRequest(http.MethodGet, "/path", nil)
176+
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/path", nil)
177177
r.Host = "example.com"
178178
// Inject CRLF into the raw query — a classic HTTP response-splitting vector.
179179
r.URL.RawQuery = "param=value\r\nX-Injected: true"
@@ -206,7 +206,7 @@ func TestServeHTTP_PoolExecError_Returns500(t *testing.T) {
206206
mp := &mockPool{execErr: fmt.Errorf("worker died")}
207207
h := newTestHandler(t, defaultCfg(), mp)
208208

209-
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"key":"val"}`))
209+
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/", strings.NewReader(`{"key":"val"}`))
210210
req.Header.Set("Content-Type", "application/json")
211211

212212
rr := httptest.NewRecorder()
@@ -217,11 +217,128 @@ func TestServeHTTP_PoolExecError_Returns500(t *testing.T) {
217217
}
218218
}
219219

220+
// ── Group B′: FetchIP edge cases ─────────────────────────────────────────────
221+
222+
func TestFetchIP_EdgeCases(t *testing.T) {
223+
tests := []struct {
224+
name string
225+
input string
226+
want string
227+
}{
228+
{"empty string", "", ""},
229+
{"ipv4 no port", "10.0.0.1", "10.0.0.1"},
230+
{"ipv6 bracketed with port", "[::1]:8080", "::1"},
231+
{"ipv6 full address bare", "2001:db8::1", "2001:db8::1"},
232+
{"garbage with colons", "not:a:valid:thing", ""},
233+
{"port only", ":8080", ""},
234+
{"ipv4 with empty port", "127.0.0.1:", "127.0.0.1"},
235+
{"ipv6 full with port", "[2001:db8::1]:443", "2001:db8::1"},
236+
}
237+
238+
log := zap.NewNop()
239+
for _, tt := range tests {
240+
t.Run(tt.name, func(t *testing.T) {
241+
got := FetchIP(tt.input, log)
242+
if got != tt.want {
243+
t.Errorf("FetchIP(%q) = %q, want %q", tt.input, got, tt.want)
244+
}
245+
})
246+
}
247+
}
248+
249+
// ── Group B″: URI edge cases ─────────────────────────────────────────────────
250+
251+
func TestURI_EdgeCases(t *testing.T) {
252+
tests := []struct {
253+
name string
254+
setup func() *http.Request
255+
want string
256+
}{
257+
{
258+
name: "empty host",
259+
setup: func() *http.Request {
260+
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/", nil)
261+
r.Host = ""
262+
return r
263+
},
264+
want: "http:///",
265+
},
266+
{
267+
name: "host with port",
268+
setup: func() *http.Request {
269+
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/p", nil)
270+
r.Host = "example.com:8080"
271+
return r
272+
},
273+
want: "http://example.com:8080/p",
274+
},
275+
{
276+
name: "url already has host set",
277+
setup: func() *http.Request {
278+
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/x", nil)
279+
r.URL.Host = "other.com"
280+
return r
281+
},
282+
want: "//other.com/x",
283+
},
284+
{
285+
name: "root path only",
286+
setup: func() *http.Request {
287+
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/", nil)
288+
r.Host = "example.com"
289+
return r
290+
},
291+
want: "http://example.com/",
292+
},
293+
{
294+
name: "query but no path",
295+
setup: func() *http.Request {
296+
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/", nil)
297+
r.Host = "example.com"
298+
r.URL.Path = ""
299+
r.URL.RawQuery = "a=1"
300+
return r
301+
},
302+
want: "http://example.com?a=1",
303+
},
304+
{
305+
name: "encoded CRLF in path preserved",
306+
setup: func() *http.Request {
307+
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/foo%0D%0Abar", nil)
308+
r.Host = "example.com"
309+
return r
310+
},
311+
want: "http://example.com/foo%0D%0Abar",
312+
},
313+
{
314+
name: "tab in query not stripped",
315+
setup: func() *http.Request {
316+
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/", nil)
317+
r.Host = "example.com"
318+
r.URL.RawQuery = "x=1\tX-Bad: true"
319+
return r
320+
},
321+
want: "http://example.com/?x=1\tX-Bad: true",
322+
},
323+
}
324+
325+
for _, tt := range tests {
326+
t.Run(tt.name, func(t *testing.T) {
327+
got := URI(tt.setup())
328+
if got != tt.want {
329+
t.Errorf("URI() = %q, want %q", got, tt.want)
330+
}
331+
})
332+
}
333+
}
334+
335+
// ── Group C: mockPool tests ───────────────────────────────────────────────────
336+
220337
func TestServeHTTP_NoFreeWorkers_SetsHeader(t *testing.T) {
221338
mp := &mockPool{execErr: errors.E(errors.NoFreeWorkers)}
222339
h := newTestHandler(t, defaultCfg(), mp)
223340

224-
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"key":"val"}`))
341+
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/", strings.NewReader(`{"key":"val"}`))
225342
req.Header.Set("Content-Type", "application/json")
226343

227344
rr := httptest.NewRecorder()

handler/response.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func (h *Handler) handlePROTOresponse(pld *payload.Payload, w http.ResponseWrite
5353
push := rsp.GetHeaders()[HTTP2Push].GetValues()
5454

5555
if pusher, ok := w.(http.Pusher); ok {
56-
for i := range push {
57-
err = pusher.Push(string(rsp.GetHeaders()[HTTP2Push].GetValues()[i]), nil)
56+
for _, pushVal := range push {
57+
err = pusher.Push(string(pushVal), nil)
5858
if err != nil {
5959
return err
6060
}

handler/uploads.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (u *Uploads) Open(log *zap.Logger, dir string, forbid, allow map[string]str
6060
func (u *Uploads) Clear(log *zap.Logger) {
6161
for _, f := range u.list {
6262
if f.TempFilename != "" && exists(f.TempFilename) {
63-
err := os.Remove(f.TempFilename) //nolint:gosec
63+
err := os.Remove(f.TempFilename)
6464
if err != nil && log != nil {
6565
log.Error("error removing the file", zap.Error(err))
6666
}
@@ -164,7 +164,7 @@ func (f *FileUpload) Open(dir string, forbid, allow map[string]struct{}) error {
164164

165165
// exists if file exists.
166166
func exists(path string) bool {
167-
if _, err := os.Stat(path); os.IsNotExist(err) { //nolint:gosec
167+
if _, err := os.Stat(path); os.IsNotExist(err) {
168168
return false
169169
}
170170
return true

init.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ func nilOr(cfg *config.Config) *acme.Config {
5757

5858
func (p *Plugin) applyBundledMiddleware() {
5959
// apply max_req_size and logger middleware
60-
for i := range p.servers {
61-
switch srv := p.servers[i].Server().(type) {
60+
for _, s := range p.servers {
61+
switch srv := s.Server().(type) {
6262
case *http.Server:
6363
srv.Handler = bundledMw.MaxRequestSize(srv.Handler, p.cfg.MaxRequestSize*MB)
6464
srv.Handler = bundledMw.NewLogMiddleware(srv.Handler, p.cfg.AccessLogs, p.log)
6565
case *http3.Server:
6666
srv.Handler = bundledMw.MaxRequestSize(srv.Handler, p.cfg.MaxRequestSize*MB)
6767
srv.Handler = bundledMw.NewLogMiddleware(srv.Handler, p.cfg.AccessLogs, p.log)
6868
default:
69-
p.log.DPanic("unknown server type", zap.Any("server", p.servers[i].Server()))
69+
p.log.DPanic("unknown server type", zap.Any("server", s.Server()))
7070
}
7171
}
7272
}

metrics.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ func (s *StatsExporter) Collect(ch chan<- prometheus.Metric) {
6868
var invalid float64
6969

7070
// collect the memory
71-
for i := range workerStates {
72-
cum += float64(workerStates[i].MemoryUsage)
71+
for _, ws := range workerStates {
72+
cum += float64(ws.MemoryUsage)
7373

74-
ch <- prometheus.MustNewConstMetric(s.StateDesc, prometheus.GaugeValue, 0, workerStates[i].StatusStr, strconv.Itoa(int(workerStates[i].Pid)))
75-
ch <- prometheus.MustNewConstMetric(s.WorkerMemoryDesc, prometheus.GaugeValue, float64(workerStates[i].MemoryUsage), strconv.Itoa(int(workerStates[i].Pid)))
74+
ch <- prometheus.MustNewConstMetric(s.StateDesc, prometheus.GaugeValue, 0, ws.StatusStr, strconv.Itoa(int(ws.Pid)))
75+
ch <- prometheus.MustNewConstMetric(s.WorkerMemoryDesc, prometheus.GaugeValue, float64(ws.MemoryUsage), strconv.Itoa(int(ws.Pid)))
7676

7777
// sync with sdk/worker/state.go
78-
switch workerStates[i].Status {
78+
switch ws.Status {
7979
case fsm.StateReady:
8080
ready++
8181
case fsm.StateWorking:

middleware/redirect.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package middleware
22

33
import (
4-
"fmt"
4+
"net"
55
"net/http"
66
"net/url"
7+
"strconv"
78
"strings"
89
)
910

@@ -26,11 +27,17 @@ func Redirect(_ http.Handler, port int) http.Handler {
2627

2728
// TLSAddr replaces listen or host port with port configured by SSLConfig config.
2829
func TLSAddr(host string, forcePort bool, sslPort int) string {
29-
// remove current forcePort first
30-
host = strings.Split(host, ":")[0]
30+
if u, err := url.Parse("//" + host); err == nil {
31+
host = u.Hostname()
32+
}
3133

3234
if forcePort || sslPort != 443 {
33-
host = fmt.Sprintf("%s:%v", host, sslPort)
35+
return net.JoinHostPort(host, strconv.Itoa(sslPort))
36+
}
37+
38+
// url.URL.Host requires bracketed IPv6 literals even without a port.
39+
if strings.Contains(host, ":") {
40+
return "[" + host + "]"
3441
}
3542

3643
return host

0 commit comments

Comments
 (0)