diff options
author | Wolfy-J <[email protected]> | 2019-05-05 12:06:04 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2019-05-05 12:06:04 +0300 |
commit | ee12d7b834b501beed56945a54fdabe8bbdd4570 (patch) | |
tree | b4eb26e5fba83124f7eab3979bd4e13e7d4a190e /service/limit | |
parent | db838d7465cc6b147c5d7f672d71c6523df54209 (diff) |
init service
Diffstat (limited to 'service/limit')
-rw-r--r-- | service/limit/controller.go | 20 | ||||
-rw-r--r-- | service/limit/service.go | 1 | ||||
-rw-r--r-- | service/limit/service_test.go | 99 |
3 files changed, 109 insertions, 11 deletions
diff --git a/service/limit/controller.go b/service/limit/controller.go index 3e6aed4b..706197fa 100644 --- a/service/limit/controller.go +++ b/service/limit/controller.go @@ -32,11 +32,11 @@ type controllerConfig struct { // TTL defines maximum time worker is allowed to live. TTL int64 - // MaxIdleTTL defines maximum duration worker can spend in idle mode. - MaxIdleTTL int64 + // IdleTTL defines maximum duration worker can spend in idle mode. + IdleTTL int64 - // MaxExecTTL defines maximum lifetime per job. - MaxExecTTL int64 + // ExecTTL defines maximum lifetime per job. + ExecTTL int64 } type controller struct { @@ -56,13 +56,13 @@ func (c *controller) control(p roadrunner.Pool) { now := time.Now() - if c.cfg.MaxExecTTL != 0 { + if c.cfg.ExecTTL != 0 { for _, w := range c.sw.find( roadrunner.StateWorking, - now.Add(-time.Second*time.Duration(c.cfg.MaxExecTTL)), + now.Add(-time.Second*time.Duration(c.cfg.ExecTTL)), ) { eID := w.State().NumExecs() - err := fmt.Errorf("max exec time reached (%vs)", c.cfg.MaxExecTTL) + err := fmt.Errorf("max exec time reached (%vs)", c.cfg.ExecTTL) // make sure worker still on initial request if p.Remove(w, err) && w.State().NumExecs() == eID { @@ -73,12 +73,12 @@ func (c *controller) control(p roadrunner.Pool) { } // locale workers which are in idle mode for too long - if c.cfg.MaxIdleTTL != 0 { + if c.cfg.IdleTTL != 0 { for _, w := range c.sw.find( roadrunner.StateReady, - now.Add(-time.Second*time.Duration(c.cfg.MaxIdleTTL)), + now.Add(-time.Second*time.Duration(c.cfg.IdleTTL)), ) { - err := fmt.Errorf("max idle time reached (%vs)", c.cfg.MaxIdleTTL) + err := fmt.Errorf("max idle time reached (%vs)", c.cfg.IdleTTL) if p.Remove(w, err) { c.report(EventMaxIdleTTL, w, err) } diff --git a/service/limit/service.go b/service/limit/service.go index 99e2b1ee..6cbc22be 100644 --- a/service/limit/service.go +++ b/service/limit/service.go @@ -16,7 +16,6 @@ type controllable interface { // Service to control the state of rr service inside other services. type Service struct { - cfg *Config lsns []func(event int, ctx interface{}) } diff --git a/service/limit/service_test.go b/service/limit/service_test.go new file mode 100644 index 00000000..87c8046b --- /dev/null +++ b/service/limit/service_test.go @@ -0,0 +1,99 @@ +package limit + +import ( + "encoding/json" + "fmt" + "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus/hooks/test" + "github.com/spiral/roadrunner/service" + rrhttp "github.com/spiral/roadrunner/service/http" + "github.com/stretchr/testify/assert" + "io/ioutil" + "net/http" + "testing" + "time" +) + +type testCfg struct { + httpCfg string + limitCfg string + target string +} + +func (cfg *testCfg) Get(name string) service.Config { + if name == rrhttp.ID { + if cfg.httpCfg == "" { + return nil + } + + return &testCfg{target: cfg.httpCfg} + } + + if name == ID { + return &testCfg{target: cfg.limitCfg} + } + + return nil +} + +func (cfg *testCfg) Unmarshal(out interface{}) error { + err := json.Unmarshal([]byte(cfg.target), out) + + if cl, ok := out.(*Config); ok { + // to speed up tests + cl.Interval = time.Millisecond + } + + return err +} + +func Test_Service_PidEcho(t *testing.T) { + logger, _ := test.NewNullLogger() + logger.SetLevel(logrus.DebugLevel) + + c := service.NewContainer(logger) + c.Register(rrhttp.ID, &rrhttp.Service{}) + c.Register(ID, &Service{}) + + assert.NoError(t, c.Init(&testCfg{ + httpCfg: `{ + "address": ":6029", + "workers":{ + "command": "php ../../tests/http/client.php pid pipes", + "pool": {"numWorkers": 1} + } + }`, + limitCfg: `{ + "services": { + "http": { + "ttl": 1 + } + } + }`, + })) + + s, _ := c.Get(rrhttp.ID) + assert.NotNil(t, s) + + go func() { c.Serve() }() + time.Sleep(time.Millisecond * 100) + defer c.Stop() + + req, err := http.NewRequest("GET", "http://localhost:6029", nil) + assert.NoError(t, err) + + r, err := http.DefaultClient.Do(req) + assert.NoError(t, err) + defer r.Body.Close() + + b, err := ioutil.ReadAll(r.Body) + assert.NoError(t, err) + + assert.NoError(t, err) + assert.Equal(t, getPID(s), string(b)) +} + +func getPID(s interface{}) string { + w := s.(*rrhttp.Service).Server().Workers()[0] + return fmt.Sprintf("%v", *w.Pid) +} |