Skip to content

Commit 7e4e00e

Browse files
committed
perf: avoid replacing dist tarball url on each call
1 parent 18338eb commit 7e4e00e

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

cli/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ var rootCmd = &cobra.Command{
1515
}
1616

1717
var rootOptions struct {
18+
Silent bool
1819
ListenAddress string
1920
UpstreamAddress string
2021
CacheLimit string
2122
CacheTTL int
2223
}
2324

2425
func init() {
26+
rootCmd.Flags().BoolVar(&rootOptions.Silent, "silent", getEnvBool("SILENT", "0"), "Disable logging")
2527
rootCmd.Flags().StringVar(&rootOptions.ListenAddress, "listen", getEnvString("LISTEN_ADDRESS", "localhost:8080"), "Address to listen")
2628
rootCmd.Flags().StringVar(&rootOptions.UpstreamAddress, "upstream", getEnvString("UPSTREAM_ADDRESS", "https://registry.npmjs.org"), "Upstream registry address")
2729
rootCmd.Flags().StringVar(&rootOptions.CacheLimit, "cache-limit", getEnvString("CACHE_LIMIT", "0"), "Cached packages count limit")
@@ -39,5 +41,6 @@ func run(cmd *cobra.Command, args []string) {
3941

4042
proxy.Server(npmproxy.ServerOptions{
4143
ListenAddress: rootOptions.ListenAddress,
44+
Silent: rootOptions.Silent,
4245
}).ListenAndServe()
4346
}

cli/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,12 @@ func getEnvInt(env string, def string) int {
2323

2424
return converted
2525
}
26+
27+
func getEnvBool(env string, def string) bool {
28+
value := getEnvString(env, def)
29+
30+
// TODO: handle error
31+
converted, _ := strconv.ParseBool(value)
32+
33+
return converted
34+
}

proxy/cache.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ func (proxy Proxy) GetCachedPath(path string, request *http.Request) ([]byte, er
5757
return nil, err
5858
}
5959

60-
// convert body to string
61-
pkg = string(body)
60+
// TODO: avoid calling MustCompile every time
61+
// find "dist": "https?://.*/ and replace to "dist": "{localurl}/
62+
pkg = regexp.MustCompile(`(?U)"tarball":"https?://.*/`).ReplaceAllString(string(body), `"dist": "http://localhost:8080/`)
6263

6364
// save to redis
6465
err = proxy.Database.Set(key, pkg, options.DatabaseExpiration)
@@ -67,10 +68,6 @@ func (proxy Proxy) GetCachedPath(path string, request *http.Request) ([]byte, er
6768
}
6869
}
6970

70-
// TODO: avoid calling MustCompile every time
71-
// find "dist": "https?://.*/ and replace to "dist": "{localurl}/
72-
pkg = regexp.MustCompile(`(?U)"tarball":"https?://.*/`).ReplaceAllString(pkg, `"dist": "http://localhost:8080/`)
73-
7471
return []byte(pkg), nil
7572
}
7673

proxy/server.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,20 @@ import (
1313
// ServerOptions provides configuration for Server method
1414
type ServerOptions struct {
1515
ListenAddress string
16+
Silent bool
1617
}
1718

1819
// Server creates http proxy server
1920
func (proxy Proxy) Server(options ServerOptions) *http.Server {
2021
router := gin.New()
2122

22-
logger, _ := zap.NewProduction()
23-
router.Use(ginzap.Ginzap(logger, time.RFC3339, true))
23+
if options.Silent {
24+
router.Use(gin.Recovery())
25+
} else {
26+
logger, _ := zap.NewProduction()
27+
router.Use(ginzap.Ginzap(logger, time.RFC3339, true))
28+
router.Use(ginzap.RecoveryWithZap(logger, true))
29+
}
2430

2531
router.GET("/:scope/:name", proxy.getPackageHandler)
2632
router.GET("/:scope", proxy.getPackageHandler)

readme.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ ncp --listen "localhost:1234" # listen on port 1234
7777
| `--upstream <address>` | `UPSTREAM_ADDRESS` | `https://registry.npmjs.org` | Upstream registry address |
7878
| `--cache-limit <count>` | `CACHE_LIMIT` | - | Cached packages count limit |
7979
| `--cache-ttl <timeout>` | `CACHE_TTL` | `3600` | Cache expiration timeout in seconds |
80+
| `--silent <address>` | `SILENT` | `0` | Disable logs |
8081

8182
---
8283

@@ -146,17 +147,17 @@ NCP can be deployed using Kubernetes, Docker Compose or any other container orch
146147
Macbook Pro 15″ 2017, Intel Core i7-7700HQ. Note `GOMACPROCS=1`.
147148

148149
```bash
149-
# GOMAXPROCS=1 ncp --listen localhost:8080
150+
# SILENT=1 GIN_MODE=release GOMAXPROCS=1 ncp
150151

151-
$ go-wrk -c 100 -d 5 http://localhost:8080/ascii
152-
Running 5s test @ http://localhost:8080/ascii
152+
$ go-wrk -c 100 -d 10 http://localhost:8080/ascii
153+
Running 10s test @ http://localhost:8080/ascii
153154
100 goroutine(s) running concurrently
154-
33321 requests in 5.00537759s, 212.69MB read
155-
Requests/sec: 6657.04
156-
Transfer/sec: 42.49MB
157-
Avg Req Time: 15.02169ms
158-
Fastest Request: 230.514µs
159-
Slowest Request: 571.420487ms
155+
84216 requests in 10.000196326s, 535.30MB read
156+
Requests/sec: 8421.43
157+
Transfer/sec: 53.53MB
158+
Avg Req Time: 11.874461ms
159+
Fastest Request: 2.213324ms
160+
Slowest Request: 745.874068ms
160161
Number of Errors: 0
161162
```
162163

0 commit comments

Comments
 (0)