perf: avoid replacing dist tarball url on each call
This commit is contained in:
parent
18338eb0d7
commit
7e4e00e3d7
@ -15,6 +15,7 @@ var rootCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var rootOptions struct {
|
var rootOptions struct {
|
||||||
|
Silent bool
|
||||||
ListenAddress string
|
ListenAddress string
|
||||||
UpstreamAddress string
|
UpstreamAddress string
|
||||||
CacheLimit string
|
CacheLimit string
|
||||||
@ -22,6 +23,7 @@ var rootOptions struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
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.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.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")
|
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{
|
proxy.Server(npmproxy.ServerOptions{
|
||||||
ListenAddress: rootOptions.ListenAddress,
|
ListenAddress: rootOptions.ListenAddress,
|
||||||
|
Silent: rootOptions.Silent,
|
||||||
}).ListenAndServe()
|
}).ListenAndServe()
|
||||||
}
|
}
|
||||||
|
@ -23,3 +23,12 @@ func getEnvInt(env string, def string) int {
|
|||||||
|
|
||||||
return converted
|
return converted
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getEnvBool(env string, def string) bool {
|
||||||
|
value := getEnvString(env, def)
|
||||||
|
|
||||||
|
// TODO: handle error
|
||||||
|
converted, _ := strconv.ParseBool(value)
|
||||||
|
|
||||||
|
return converted
|
||||||
|
}
|
||||||
|
@ -57,8 +57,9 @@ func (proxy Proxy) GetCachedPath(path string, request *http.Request) ([]byte, er
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert body to string
|
// TODO: avoid calling MustCompile every time
|
||||||
pkg = string(body)
|
// find "dist": "https?://.*/ and replace to "dist": "{localurl}/
|
||||||
|
pkg = regexp.MustCompile(`(?U)"tarball":"https?://.*/`).ReplaceAllString(string(body), `"dist": "http://localhost:8080/`)
|
||||||
|
|
||||||
// save to redis
|
// save to redis
|
||||||
err = proxy.Database.Set(key, pkg, options.DatabaseExpiration)
|
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
|
return []byte(pkg), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,14 +13,20 @@ import (
|
|||||||
// ServerOptions provides configuration for Server method
|
// ServerOptions provides configuration for Server method
|
||||||
type ServerOptions struct {
|
type ServerOptions struct {
|
||||||
ListenAddress string
|
ListenAddress string
|
||||||
|
Silent bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Server creates http proxy server
|
// Server creates http proxy server
|
||||||
func (proxy Proxy) Server(options ServerOptions) *http.Server {
|
func (proxy Proxy) Server(options ServerOptions) *http.Server {
|
||||||
router := gin.New()
|
router := gin.New()
|
||||||
|
|
||||||
logger, _ := zap.NewProduction()
|
if options.Silent {
|
||||||
router.Use(ginzap.Ginzap(logger, time.RFC3339, true))
|
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/:name", proxy.getPackageHandler)
|
||||||
router.GET("/:scope", proxy.getPackageHandler)
|
router.GET("/:scope", proxy.getPackageHandler)
|
||||||
|
19
readme.md
19
readme.md
@ -77,6 +77,7 @@ ncp --listen "localhost:1234" # listen on port 1234
|
|||||||
| `--upstream <address>` | `UPSTREAM_ADDRESS` | `https://registry.npmjs.org` | Upstream registry address |
|
| `--upstream <address>` | `UPSTREAM_ADDRESS` | `https://registry.npmjs.org` | Upstream registry address |
|
||||||
| `--cache-limit <count>` | `CACHE_LIMIT` | - | Cached packages count limit |
|
| `--cache-limit <count>` | `CACHE_LIMIT` | - | Cached packages count limit |
|
||||||
| `--cache-ttl <timeout>` | `CACHE_TTL` | `3600` | Cache expiration timeout in seconds |
|
| `--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`.
|
Macbook Pro 15″ 2017, Intel Core i7-7700HQ. Note `GOMACPROCS=1`.
|
||||||
|
|
||||||
```bash
|
```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
|
$ go-wrk -c 100 -d 10 http://localhost:8080/ascii
|
||||||
Running 5s test @ http://localhost:8080/ascii
|
Running 10s test @ http://localhost:8080/ascii
|
||||||
100 goroutine(s) running concurrently
|
100 goroutine(s) running concurrently
|
||||||
33321 requests in 5.00537759s, 212.69MB read
|
84216 requests in 10.000196326s, 535.30MB read
|
||||||
Requests/sec: 6657.04
|
Requests/sec: 8421.43
|
||||||
Transfer/sec: 42.49MB
|
Transfer/sec: 53.53MB
|
||||||
Avg Req Time: 15.02169ms
|
Avg Req Time: 11.874461ms
|
||||||
Fastest Request: 230.514µs
|
Fastest Request: 2.213324ms
|
||||||
Slowest Request: 571.420487ms
|
Slowest Request: 745.874068ms
|
||||||
Number of Errors: 0
|
Number of Errors: 0
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user