summaryrefslogtreecommitdiff
path: root/plugins/websockets/config.go
blob: 93d9ac3b3e361c59f2581f3354e6b82c8cb807a4 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package websockets

import (
	"time"

	"github.com/spiral/roadrunner/v2/pkg/pool"
)

/*
# GLOBAL
redis:
  addrs:
    - 'localhost:6379'

websockets:
  # pubsubs should implement PubSub interface to be collected via endure.Collects

  pubsubs:["redis", "amqp", "memory"]
  # OR local
  redis:
    addrs:
      - 'localhost:6379'

  # path used as websockets path
  path: "/ws"
*/

type RedisConfig struct {
	Addrs            []string      `mapstructure:"addrs"`
	DB               int           `mapstructure:"db"`
	Username         string        `mapstructure:"username"`
	Password         string        `mapstructure:"password"`
	MasterName       string        `mapstructure:"master_name"`
	SentinelPassword string        `mapstructure:"sentinel_password"`
	RouteByLatency   bool          `mapstructure:"route_by_latency"`
	RouteRandomly    bool          `mapstructure:"route_randomly"`
	MaxRetries       int           `mapstructure:"max_retries"`
	DialTimeout      time.Duration `mapstructure:"dial_timeout"`
	MinRetryBackoff  time.Duration `mapstructure:"min_retry_backoff"`
	MaxRetryBackoff  time.Duration `mapstructure:"max_retry_backoff"`
	PoolSize         int           `mapstructure:"pool_size"`
	MinIdleConns     int           `mapstructure:"min_idle_conns"`
	MaxConnAge       time.Duration `mapstructure:"max_conn_age"`
	ReadTimeout      time.Duration `mapstructure:"read_timeout"`
	WriteTimeout     time.Duration `mapstructure:"write_timeout"`
	PoolTimeout      time.Duration `mapstructure:"pool_timeout"`
	IdleTimeout      time.Duration `mapstructure:"idle_timeout"`
	IdleCheckFreq    time.Duration `mapstructure:"idle_check_freq"`
	ReadOnly         bool          `mapstructure:"read_only"`
}

// Config represents configuration for the ws plugin
type Config struct {
	// http path for the websocket
	Path string `mapstructure:"path"`
	// ["redis", "amqp", "memory"]
	PubSubs    []string `mapstructure:"pubsubs"`
	Middleware []string `mapstructure:"middleware"`

	Redis *RedisConfig `mapstructure:"redis"`

	Pool *pool.Config `mapstructure:"pool"`
}

// InitDefault initialize default values for the ws config
func (c *Config) InitDefault() {
	if c.Path == "" {
		c.Path = "/ws"
	}
	if len(c.PubSubs) == 0 {
		// memory used by default
		c.PubSubs = append(c.PubSubs, "memory")
	}

	if c.Pool == nil {
		c.Pool = &pool.Config{}
		if c.Pool.NumWorkers == 0 {
			// 2 workers by default
			c.Pool.NumWorkers = 2
		}

		if c.Pool.AllocateTimeout == 0 {
			c.Pool.AllocateTimeout = time.Minute
		}

		if c.Pool.DestroyTimeout == 0 {
			c.Pool.DestroyTimeout = time.Minute
		}
		if c.Pool.Supervisor == nil {
			return
		}
		c.Pool.Supervisor.InitDefaults()
	}

	if c.Redis != nil {
		if c.Redis.Addrs == nil {
			// append default
			c.Redis.Addrs = append(c.Redis.Addrs, "localhost:6379")
		}
	}
}