diff options
author | Wolfy-J <[email protected]> | 2018-06-10 17:18:23 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-10 17:18:23 +0300 |
commit | 094a4c211022b9446ef988c74c546ad6efb09722 (patch) | |
tree | 603ade627491960108154d6301868c9b881cd101 | |
parent | 232aa8f3c20a060e556ab431467f4f7b3f83bfbf (diff) |
http service
-rw-r--r-- | cmd/rr/.rr.yaml | 63 | ||||
-rw-r--r-- | cmd/rr/cmd/serve.go | 9 | ||||
-rw-r--r-- | config.go | 4 | ||||
-rw-r--r-- | http/config.go | 10 | ||||
-rw-r--r-- | http/service.go | 2 | ||||
-rw-r--r-- | http/uploads.go | 2 | ||||
-rw-r--r-- | http/uploads_config.go | 12 | ||||
-rw-r--r-- | service/container.go | 17 | ||||
-rw-r--r-- | service/container_test.go | 4 | ||||
-rw-r--r-- | static/service.go | 2 | ||||
-rw-r--r-- | static_pool.go | 4 | ||||
-rw-r--r-- | static_pool_test.go | 4 |
12 files changed, 66 insertions, 67 deletions
diff --git a/cmd/rr/.rr.yaml b/cmd/rr/.rr.yaml index 5ccf73e9..b7da7fac 100644 --- a/cmd/rr/.rr.yaml +++ b/cmd/rr/.rr.yaml @@ -9,59 +9,50 @@ rpc: # http service configuration. http: # set to false to disable http server. - enabled: true + enable: true # http host to listen. - host: 0.0.0.0 + address: 0.0.0.0:8080 - # http port. - port: 8080 - - # max POST request size, including file uploads. - maxRequest: 1G - - # static file serving. - static: - # when true rr http will be serving static files. - serve: true - - # root directory for static file (http would not serve .php and .htacess files). - root: "/Users/wolfy-j/Projects/phpapp/webroot" + # max POST request size, including file uploads. (default: 1GB) + maxRequest: 1073741824 # file upload configuration. uploads: - # directory to store uploaded files before passing to PHP, keep empty to use default system - # temp directory. - tmpDir: - # list of file extensions which are forbidden for uploading. forbid: [".php", ".exe", ".bat"] # http worker pool configuration. - pool: + workers: # php worker command. command: "php /Users/wolfy-j/Projects/phpapp/webroot/index.php rr pipes --no-ansi" - user: "wolfy-j" - - group: "wolfy-j" - # connection method (pipes, tcp://:9000, unix://socket.unix). relay: "pipes" - # number of active workers. - number: 1 + # worker pool configuration. + pool: + # number of workers to be serving. + numWorkers: 1 + + # maximum jobs per worker, 0 - unlimited. + maxJobs: 0 + + # worker allocation timeouts. + timeouts: + # for how long worker is allowed to be bootstrapped. + allocateTimeout: 6000000 - # maximum jobs per worker, 0 - unlimited. - maxJobs: 0 + # amount of time given to worker to gracefully destruct itself. + destroyTimeout: 6000000 - # worker allocation timeouts. - timeouts: - # for how long socket based relay should await worker connection. - connect: 10 +# static file serving. +static: + # serve http static files + enable: true - # for how long worker is allowed to be bootstrapped. - allocate: 60 + # root directory for static file (http would not serve .php and .htacess files). + dir: "/Users/wolfy-j/Projects/phpapp/webroot" - # amount of time given to worker to gracefully destruct itself. - destroy: 60
\ No newline at end of file + # list of extensions for forbid for serving. + forbid: [".php", ".htaccess"]
\ No newline at end of file diff --git a/cmd/rr/cmd/serve.go b/cmd/rr/cmd/serve.go index ffa283d3..b04ea4b7 100644 --- a/cmd/rr/cmd/serve.go +++ b/cmd/rr/cmd/serve.go @@ -33,14 +33,17 @@ func init() { CLI.AddCommand(&cobra.Command{ Use: "serve", Short: "Serve RoadRunner service(s)", - Run: serveHandler, + RunE: serveHandler, }) signal.Notify(stopSignal, syscall.SIGTERM) } -func serveHandler(cmd *cobra.Command, args []string) { - Container.Serve() +func serveHandler(cmd *cobra.Command, args []string) error { + if err := Container.Serve(); err != nil { + return err + } + <-stopSignal Container.Stop() } @@ -11,10 +11,10 @@ type Config struct { // might be doubled by Swapper while hot-swap. NumWorkers uint64 - // MaxExecutions defines how many executions is allowed for the worker until + // MaxJobs defines how many executions is allowed for the worker until // it's destruction. set 1 to create new process for each new task, 0 to let // worker handle as many tasks as it can. - MaxExecutions uint64 + MaxJobs uint64 // AllocateTimeout defines for how long pool will be waiting for a worker to // be freed to handle the task. diff --git a/http/config.go b/http/config.go index efcaae30..abb14d9a 100644 --- a/http/config.go +++ b/http/config.go @@ -2,7 +2,6 @@ package http import ( "github.com/spiral/roadrunner" - "fmt" ) // Configures RoadRunner HTTP server. @@ -10,8 +9,8 @@ type Config struct { // Enable enables http service. Enable bool - // Host and port to handle as http server. - Host, Port string + // Address and port to handle as http server. + Address string // MaxRequest specified max size for payload body in bytes, set 0 to unlimited. MaxRequest int64 @@ -27,8 +26,3 @@ type Config struct { func (cfg *Config) Valid() error { return nil } - -// httpAddr returns prepared http listen address. -func (cfg *Config) httpAddr() string { - return fmt.Sprintf("%s:%v", cfg.Host, cfg.Port) -} diff --git a/http/service.go b/http/service.go index c31c4a47..bf25667d 100644 --- a/http/service.go +++ b/http/service.go @@ -64,7 +64,7 @@ func (s *Service) Serve() error { s.rr = rr s.srv = &Server{cfg: s.cfg, rr: s.rr} - s.http = &http.Server{Addr: s.cfg.httpAddr()} + s.http = &http.Server{Addr: s.cfg.Address} s.rr.Listen(s.listener) s.srv.Listen(s.listener) diff --git a/http/uploads.go b/http/uploads.go index 62167a6c..1f3060c0 100644 --- a/http/uploads.go +++ b/http/uploads.go @@ -112,7 +112,7 @@ func (f *FileUpload) Open(cfg *UploadsConfig) error { } defer file.Close() - tmp, err := ioutil.TempFile(cfg.Dir, "upload") + tmp, err := ioutil.TempFile(cfg.TmpDir(), "upload") if err != nil { // most likely cause of this issue is missing tmp dir f.Error = UploadErrorNoTmpDir diff --git a/http/uploads_config.go b/http/uploads_config.go index ac80723f..715de69a 100644 --- a/http/uploads_config.go +++ b/http/uploads_config.go @@ -3,6 +3,7 @@ package http import ( "strings" "path" + "os" ) // UploadsConfig describes file location and controls access to them. @@ -15,8 +16,17 @@ type UploadsConfig struct { Forbid []string } +// TmpDir returns temporary directory. +func (cfg *UploadsConfig) TmpDir() string { + if cfg.Dir != "" { + return cfg.Dir + } + + return os.TempDir() +} + // Forbid must return true if file extension is not allowed for the upload. -func (cfg UploadsConfig) Forbids(filename string) bool { +func (cfg *UploadsConfig) Forbids(filename string) bool { ext := strings.ToLower(path.Ext(filename)) for _, v := range cfg.Forbid { diff --git a/service/container.go b/service/container.go index 3395cd86..0e89f224 100644 --- a/service/container.go +++ b/service/container.go @@ -5,6 +5,7 @@ import ( "github.com/pkg/errors" "github.com/sirupsen/logrus" "sync" + "github.com/fatih/color" ) // Config provides ability to slice configuration sections and unmarshal configuration data into @@ -64,7 +65,7 @@ func (c *container) Register(name string, service Service) { status: StatusRegistered, }) - c.log.Debugf("%s.service: registered", name) + c.log.Debugf("%s: registered", color.GreenString(name)) } // Check hasStatus svc has been registered. @@ -102,18 +103,18 @@ func (c *container) Configure(cfg Config) error { for _, e := range c.services { if e.getStatus() >= StatusConfigured { - return fmt.Errorf("service %s has already been configured", e.name) + return fmt.Errorf("service %s has already been configured", color.GreenString(e.name)) } segment := cfg.Get(e.name) if segment == nil { - c.log.Debugf("%s.service: no config has been provided", e.name) + c.log.Debugf("%s: no config has been provided", color.GreenString(e.name)) continue } ok, err := e.svc.Configure(segment, c) if err != nil { - return errors.Wrap(err, fmt.Sprintf("%s.service", e.name)) + return errors.Wrap(err, fmt.Sprintf("%s", color.GreenString(e.name))) } else if ok { e.setStatus(StatusConfigured) } @@ -138,14 +139,14 @@ func (c *container) Serve() error { continue } - c.log.Debugf("%s.service: started", e.name) + c.log.Debugf("%s: started", color.GreenString(e.name)) go func(e *entry) { e.setStatus(StatusServing) defer e.setStatus(StatusStopped) if err := e.svc.Serve(); err != nil { - c.log.Errorf("%s.service: %s", e.name, err) - done <- errors.Wrap(err, fmt.Sprintf("%s.service", e.name)) + c.log.Errorf("%s: %s", color.GreenString(e.name), err) + done <- errors.Wrap(err, fmt.Sprintf("%s", color.GreenString(e.name))) } }(e) } @@ -173,7 +174,7 @@ func (c *container) Stop() { if e.hasStatus(StatusServing) { e.svc.Stop() e.setStatus(StatusStopped) - c.log.Debugf("%s.service: stopped", e.name) + c.log.Debugf("%s: stopped", color.GreenString(e.name)) } } } diff --git a/service/container_test.go b/service/container_test.go index bba0056f..c4e3c614 100644 --- a/service/container_test.go +++ b/service/container_test.go @@ -290,7 +290,7 @@ func TestContainer_ServeError(t *testing.T) { err := c.Serve() assert.Error(t, err) assert.Contains(t, err.Error(), "serve error") - assert.Contains(t, err.Error(), "test.service") + assert.Contains(t, err.Error(), "test") s, st := c.Get("test") assert.IsType(t, &testService{}, s) @@ -322,7 +322,7 @@ func TestContainer_ServeErrorMultiple(t *testing.T) { err := c.Serve() assert.Error(t, err) assert.Contains(t, err.Error(), "serve error") - assert.Contains(t, err.Error(), "test.service") + assert.Contains(t, err.Error(), "test") s, st := c.Get("test") assert.IsType(t, &testService{}, s) diff --git a/static/service.go b/static/service.go index eab9fe49..6be7f20f 100644 --- a/static/service.go +++ b/static/service.go @@ -11,7 +11,7 @@ import ( ) // Name contains default service name. -const Name = "static-server" +const Name = "static" // Service serves static files. Potentially convert into middleware? type Service struct { diff --git a/static_pool.go b/static_pool.go index a4391005..a2d30679 100644 --- a/static_pool.go +++ b/static_pool.go @@ -161,8 +161,8 @@ func (p *StaticPool) allocateWorker() (w *Worker, err error) { // release releases or replaces the worker. func (p *StaticPool) release(w *Worker) { - if p.cfg.MaxExecutions != 0 && w.State().NumExecs() >= p.cfg.MaxExecutions { - go p.replaceWorker(w, p.cfg.MaxExecutions) + if p.cfg.MaxJobs != 0 && w.State().NumExecs() >= p.cfg.MaxJobs { + go p.replaceWorker(w, p.cfg.MaxJobs) return } diff --git a/static_pool_test.go b/static_pool_test.go index d9fa75cd..cf3af4f6 100644 --- a/static_pool_test.go +++ b/static_pool_test.go @@ -237,7 +237,7 @@ func Test_StaticPool_Replace_Worker(t *testing.T) { NewPipeFactory(), Config{ NumWorkers: 1, - MaxExecutions: 1, + MaxJobs: 1, AllocateTimeout: time.Second, DestroyTimeout: time.Second, }, @@ -364,7 +364,7 @@ func Benchmark_Pool_Echo_Replaced(b *testing.B) { NewPipeFactory(), Config{ NumWorkers: 1, - MaxExecutions: 1, + MaxJobs: 1, AllocateTimeout: time.Second, DestroyTimeout: time.Second, }, |