Skip to content

Commit 315715e

Browse files
core: Implement FastAbs to avoid repeated os.Getwd calls (#6687)
* core: Implement FastAbs to avoid repeated os.Getwd calls * Lint * Rename files
1 parent 238f110 commit 315715e

File tree

10 files changed

+90
-16
lines changed

10 files changed

+90
-16
lines changed

caddyconfig/caddyfile/parse.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ func (p *parser) doImport(nesting int) error {
423423
// make path relative to the file of the _token_ being processed rather
424424
// than current working directory (issue #867) and then use glob to get
425425
// list of matching filenames
426-
absFile, err := filepath.Abs(p.Dispenser.File())
426+
absFile, err := caddy.FastAbs(p.Dispenser.File())
427427
if err != nil {
428428
return p.Errf("Failed to get absolute path of file: %s: %v", p.Dispenser.File(), err)
429429
}
@@ -622,7 +622,7 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) {
622622

623623
// Tack the file path onto these tokens so errors show the imported file's name
624624
// (we use full, absolute path to avoid bugs: issue #1892)
625-
filename, err := filepath.Abs(importFile)
625+
filename, err := caddy.FastAbs(importFile)
626626
if err != nil {
627627
return nil, p.Errf("Failed to get absolute path of file: %s: %v", importFile, err)
628628
}

filepath.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2015 Matthew Holt and The Caddy Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build !windows
16+
17+
package caddy
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
)
23+
24+
// FastAbs is an optimized version of filepath.Abs for Unix systems,
25+
// since we don't expect the working directory to ever change once
26+
// Caddy is running. Avoid the os.Getwd() syscall overhead.
27+
func FastAbs(path string) (string, error) {
28+
if filepath.IsAbs(path) {
29+
return filepath.Clean(path), nil
30+
}
31+
if wderr != nil {
32+
return "", wderr
33+
}
34+
return filepath.Join(wd, path), nil
35+
}
36+
37+
var wd, wderr = os.Getwd()

filepath_windows.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2015 Matthew Holt and The Caddy Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package caddy
16+
17+
import (
18+
"path/filepath"
19+
)
20+
21+
// FastAbs can't be optimized on Windows because the
22+
// syscall.FullPath function takes an input.
23+
func FastAbs(path string) (string, error) {
24+
return filepath.Abs(path)
25+
}

filesystem.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2015 Matthew Holt and The Caddy Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package caddy
216

317
import "io/fs"

listen.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
"go.uber.org/zap"
3131
)
3232

33-
func reuseUnixSocket(network, addr string) (any, error) {
33+
func reuseUnixSocket(_, _ string) (any, error) {
3434
return nil, nil
3535
}
3636

modules/caddyhttp/fileserver/staticfiles.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func (fsrv *FileServer) Provision(ctx caddy.Context) error {
204204
// absolute paths before the server starts for very slight performance improvement
205205
for i, h := range fsrv.Hide {
206206
if !strings.Contains(h, "{") && strings.Contains(h, separator) {
207-
if abs, err := filepath.Abs(h); err == nil {
207+
if abs, err := caddy.FastAbs(h); err == nil {
208208
fsrv.Hide[i] = abs
209209
}
210210
}
@@ -636,7 +636,7 @@ func (fsrv *FileServer) transformHidePaths(repl *caddy.Replacer) []string {
636636
for i := range fsrv.Hide {
637637
hide[i] = repl.ReplaceAll(fsrv.Hide[i], "")
638638
if strings.Contains(hide[i], separator) {
639-
abs, err := filepath.Abs(hide[i])
639+
abs, err := caddy.FastAbs(hide[i])
640640
if err == nil {
641641
hide[i] = abs
642642
}
@@ -655,7 +655,7 @@ func fileHidden(filename string, hide []string) bool {
655655
}
656656

657657
// all path comparisons use the complete absolute path if possible
658-
filenameAbs, err := filepath.Abs(filename)
658+
filenameAbs, err := caddy.FastAbs(filename)
659659
if err == nil {
660660
filename = filenameAbs
661661
}

modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func (t Transport) buildEnv(r *http.Request) (envVars, error) {
228228
ip = strings.Replace(ip, "]", "", 1)
229229

230230
// make sure file root is absolute
231-
root, err := filepath.Abs(repl.ReplaceAll(t.Root, "."))
231+
root, err := caddy.FastAbs(repl.ReplaceAll(t.Root, "."))
232232
if err != nil {
233233
return nil, err
234234
}

modules/caddyhttp/reverseproxy/reverseproxy.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ func (h *Handler) proxyLoopIteration(r *http.Request, origReq *http.Request, w h
549549
// ding the health status of the upstream (an error can still
550550
// occur after the roundtrip if, for example, a response handler
551551
// after the roundtrip returns an error)
552-
if succ, ok := proxyErr.(roundtripSucceeded); ok {
552+
if succ, ok := proxyErr.(roundtripSucceededError); ok {
553553
return true, succ.error
554554
}
555555

@@ -953,10 +953,10 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe
953953
res.Body.Close()
954954
}
955955

956-
// wrap any route error in roundtripSucceeded so caller knows that
956+
// wrap any route error in roundtripSucceededError so caller knows that
957957
// the roundtrip was successful and to not retry
958958
if routeErr != nil {
959-
return roundtripSucceeded{routeErr}
959+
return roundtripSucceededError{routeErr}
960960
}
961961

962962
// we're done handling the response, and we don't want to
@@ -1433,9 +1433,9 @@ type TLSTransport interface {
14331433
EnableTLS(base *TLSConfig) error
14341434
}
14351435

1436-
// roundtripSucceeded is an error type that is returned if the
1436+
// roundtripSucceededError is an error type that is returned if the
14371437
// roundtrip succeeded, but an error occurred after-the-fact.
1438-
type roundtripSucceeded struct{ error }
1438+
type roundtripSucceededError struct{ error }
14391439

14401440
// bodyReadCloser is a reader that, upon closing, will return
14411441
// its buffer to the pool and close the underlying body reader.

modules/caddytls/connpolicy.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"fmt"
2525
"io"
2626
"os"
27-
"path/filepath"
2827
"strings"
2928

3029
"github.com/mholt/acmez/v2"
@@ -358,7 +357,7 @@ func (p *ConnectionPolicy) buildStandardTLSConfig(ctx caddy.Context) error {
358357
if err != nil {
359358
return err
360359
}
361-
filename, err = filepath.Abs(filename)
360+
filename, err = caddy.FastAbs(filename)
362361
if err != nil {
363362
return err
364363
}

modules/logging/filewriter.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"io"
2121
"math"
2222
"os"
23-
"path/filepath"
2423
"strconv"
2524

2625
"github.com/dustin/go-humanize"
@@ -133,7 +132,7 @@ func (fw *FileWriter) Provision(ctx caddy.Context) error {
133132
}
134133

135134
func (fw FileWriter) String() string {
136-
fpath, err := filepath.Abs(fw.Filename)
135+
fpath, err := caddy.FastAbs(fw.Filename)
137136
if err == nil {
138137
return fpath
139138
}

0 commit comments

Comments
 (0)