perf: avoid replacing dist tarball url on each call

This commit is contained in:
 Ilya Atamas 2019-04-23 16:06:45 +03:00
parent 18338eb0d7
commit 7e4e00e3d7
5 changed files with 33 additions and 17 deletions

View File

@ -15,6 +15,7 @@ var rootCmd = &cobra.Command{
}
var rootOptions struct {
Silent bool
ListenAddress string
UpstreamAddress string
CacheLimit string
@ -22,6 +23,7 @@ var rootOptions struct {
}
func init() {
rootCmd.Flags().BoolVar(&rootOptions.Silent, "silent", getEnvBool("SILENT", "0"), "Disable logging")
rootCmd.Flags().StringVar(&rootOptions.ListenAddress, "listen", getEnvString("LISTEN_ADDRESS", "localhost:8080"), "Address to listen")
rootCmd.Flags().StringVar(&rootOptions.UpstreamAddress, "upstream", getEnvString("UPSTREAM_ADDRESS", "https://registry.npmjs.org"), "Upstream registry address")
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) {
proxy.Server(npmproxy.ServerOptions{
ListenAddress: rootOptions.ListenAddress,
Silent: rootOptions.Silent,
}).ListenAndServe()
}

View File

@ -23,3 +23,12 @@ func getEnvInt(env string, def string) int {
return converted
}
func getEnvBool(env string, def string) bool {
value := getEnvString(env, def)
// TODO: handle error
converted, _ := strconv.ParseBool(value)
return converted
}

View File

@ -57,8 +57,9 @@ func (proxy Proxy) GetCachedPath(path string, request *http.Request) ([]byte, er
return nil, err
}
// convert body to string
pkg = string(body)
// TODO: avoid calling MustCompile every time
// find "dist": "https?://.*/ and replace to "dist": "{localurl}/
pkg = regexp.MustCompile(`(?U)"tarball":"https?://.*/`).ReplaceAllString(string(body), `"dist": "http://localhost:8080/`)
// save to redis
err = proxy.Database.Set(key, pkg, options.DatabaseExpiration)
@ -67,10 +68,6 @@ func (proxy Proxy) GetCachedPath(path string, request *http.Request) ([]byte, er
}
}
// TODO: avoid calling MustCompile every time
// find "dist": "https?://.*/ and replace to "dist": "{localurl}/
pkg = regexp.MustCompile(`(?U)"tarball":"https?://.*/`).ReplaceAllString(pkg, `"dist": "http://localhost:8080/`)
return []byte(pkg), nil
}

View File

@ -13,14 +13,20 @@ import (
// ServerOptions provides configuration for Server method
type ServerOptions struct {
ListenAddress string
Silent bool
}
// Server creates http proxy server
func (proxy Proxy) Server(options ServerOptions) *http.Server {
router := gin.New()
logger, _ := zap.NewProduction()
router.Use(ginzap.Ginzap(logger, time.RFC3339, true))
if options.Silent {
router.Use(gin.Recovery())
} else {
logger, _ := zap.NewProduction()
router.Use(ginzap.Ginzap(logger, time.RFC3339, true))
router.Use(ginzap.RecoveryWithZap(logger, true))
}
router.GET("/:scope/:name", proxy.getPackageHandler)
router.GET("/:scope", proxy.getPackageHandler)

View File

@ -77,6 +77,7 @@ ncp --listen "localhost:1234" # listen on port 1234
| `--upstream <address>` | `UPSTREAM_ADDRESS` | `https://registry.npmjs.org` | Upstream registry address |
| `--cache-limit <count>` | `CACHE_LIMIT` | - | Cached packages count limit |
| `--cache-ttl <timeout>` | `CACHE_TTL` | `3600` | Cache expiration timeout in seconds |
| `--silent <address>` | `SILENT` | `0` | Disable logs |
---
@ -146,17 +147,17 @@ NCP can be deployed using Kubernetes, Docker Compose or any other container orch
Macbook Pro 15″ 2017, Intel Core i7-7700HQ. Note `GOMACPROCS=1`.
```bash
# GOMAXPROCS=1 ncp --listen localhost:8080
# SILENT=1 GIN_MODE=release GOMAXPROCS=1 ncp
$ go-wrk -c 100 -d 5 http://localhost:8080/ascii
Running 5s test @ http://localhost:8080/ascii
$ go-wrk -c 100 -d 10 http://localhost:8080/ascii
Running 10s test @ http://localhost:8080/ascii
100 goroutine(s) running concurrently
33321 requests in 5.00537759s, 212.69MB read
Requests/sec: 6657.04
Transfer/sec: 42.49MB
Avg Req Time: 15.02169ms
Fastest Request: 230.514µs
Slowest Request: 571.420487ms
84216 requests in 10.000196326s, 535.30MB read
Requests/sec: 8421.43
Transfer/sec: 53.53MB
Avg Req Time: 11.874461ms
Fastest Request: 2.213324ms
Slowest Request: 745.874068ms
Number of Errors: 0
```