summaryrefslogtreecommitdiff
path: root/service/limit/config.go
blob: 7a56280d15ab448d0f59fbf8e015c6f0b421397d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package limit

import (
	"time"

	"github.com/spiral/roadrunner"
	"github.com/spiral/roadrunner/service"
)

// Config of Limit service.
type Config struct {
	// Interval defines the update duration for underlying controllers, default 1s.
	Interval time.Duration

	// Services declares list of services to be watched.
	Services map[string]*controllerConfig
}

// Hydrate must populate Config values using given Config source. Must return error if Config is not valid.
func (c *Config) Hydrate(cfg service.Config) error {
	if err := cfg.Unmarshal(c); err != nil {
		return err
	}

	// Always use second based definition for time durations
	if c.Interval < time.Microsecond {
		c.Interval = time.Second * time.Duration(c.Interval.Nanoseconds())
	}

	return nil
}

// InitDefaults sets missing values to their default values.
func (c *Config) InitDefaults() error {
	c.Interval = time.Second

	return nil
}

// Controllers returns list of defined Services
func (c *Config) Controllers(l listener) (controllers map[string]roadrunner.Controller) {
	controllers = make(map[string]roadrunner.Controller)

	for name, cfg := range c.Services {
		controllers[name] = &controller{lsn: l, tick: c.Interval, cfg: cfg}
	}

	return controllers
}