Let Stasis load itself with default values
[asterisk/asterisk.git] / main / stasis_config.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * David M. Lee, II <dlee@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Stasis Message Bus configuration API.
22  *
23  * \author David M. Lee, II <dlee@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/config_options.h"
35 #include "asterisk/stasis.h"
36 #include "asterisk/threadpool.h"
37
38 #include <limits.h>
39
40 /*** DOCUMENTATION
41         <configInfo name="stasis" language="en_US">
42                 <synopsis>Stasis message bus configuration.</synopsis>
43                 <configFile name="stasis.conf">
44                         <configObject name="threadpool">
45                                 <synopsis>Threadpool configuration.</synopsis>
46                                 <configOption name="initial_size" default="0">
47                                         <synopsis>Initial number of threads in the message bus threadpool.</synopsis>
48                                 </configOption>
49                                 <configOption name="idle_timeout_sec" default="20">
50                                         <synopsis>Number of seconds for an idle thread to be disposed of.</synopsis>
51                                 </configOption>
52                                 <configOption name="max_size" default="200">
53                                         <synopsis>Maximum number of threads in the threadpool.</synopsis>
54                                 </configOption>
55                         </configObject>
56                 </configFile>
57         </configInfo>
58  ***/
59
60 /*! \brief Locking container for safe configuration access. */
61 static AO2_GLOBAL_OBJ_STATIC(confs);
62
63 struct stasis_threadpool_conf {
64         int initial_size;
65         int idle_timeout_sec;
66         int max_size;
67 };
68
69 struct stasis_conf {
70         struct stasis_threadpool_conf *threadpool;
71 };
72
73 /*! \brief Mapping of the stasis http conf struct's globals to the
74  *         threadpool context in the config file. */
75 static struct aco_type threadpool_option = {
76         .type = ACO_GLOBAL,
77         .name = "threadpool",
78         .item_offset = offsetof(struct stasis_conf, threadpool),
79         .category = "^threadpool$",
80         .category_match = ACO_WHITELIST,
81 };
82
83 static struct aco_type *threadpool_options[] = ACO_TYPES(&threadpool_option);
84
85 #define CONF_FILENAME "stasis.conf"
86
87 /*! \brief The conf file that's processed for the module. */
88 static struct aco_file conf_file = {
89         /*! The config file name. */
90         .filename = CONF_FILENAME,
91         /*! The mapping object types to be processed. */
92         .types = ACO_TYPES(&threadpool_option),
93 };
94
95 static void conf_dtor(void *obj)
96 {
97         struct stasis_conf *conf = obj;
98
99         ao2_cleanup(conf->threadpool);
100         conf->threadpool = NULL;
101 }
102
103 static void *conf_alloc(void)
104 {
105         RAII_VAR(struct stasis_conf *, conf, NULL, ao2_cleanup);
106
107         conf = ao2_alloc_options(sizeof(*conf), conf_dtor,
108                 AO2_ALLOC_OPT_LOCK_NOLOCK);
109         if (!conf) {
110                 return NULL;
111         }
112
113         conf->threadpool = ao2_alloc_options(sizeof(*conf->threadpool), NULL,
114                 AO2_ALLOC_OPT_LOCK_NOLOCK);
115         if (!conf->threadpool) {
116                 return NULL;
117         }
118
119         aco_set_defaults(&threadpool_option, "threadpool", conf->threadpool);
120
121         ao2_ref(conf, +1);
122         return conf;
123 }
124
125 CONFIG_INFO_CORE("stasis", cfg_info, confs, conf_alloc,
126         .files = ACO_FILES(&conf_file));
127
128 void stasis_config_get_threadpool_options(
129         struct ast_threadpool_options *threadpool_options)
130 {
131         RAII_VAR(struct stasis_conf *, conf, NULL, ao2_cleanup);
132
133         conf = ao2_global_obj_ref(confs);
134
135         ast_assert(conf && conf->threadpool);
136
137         {
138                 struct ast_threadpool_options newopts = {
139                         .version = AST_THREADPOOL_OPTIONS_VERSION,
140                         .initial_size = conf->threadpool->initial_size,
141                         .auto_increment = 1,
142                         .idle_timeout = conf->threadpool->idle_timeout_sec,
143                         .max_size = conf->threadpool->max_size,
144                 };
145
146                 *threadpool_options = newopts;
147         }
148 }
149
150 /*! \brief Load (or reload) configuration. */
151 static int process_config(int reload)
152 {
153         RAII_VAR(struct stasis_conf *, conf, conf_alloc(), ao2_cleanup);
154
155                 switch (aco_process_config(&cfg_info, reload)) {
156                 case ACO_PROCESS_ERROR:
157                         if (conf && !reload && !aco_set_defaults(&threadpool_option, "threadpool", conf->threadpool)) {
158                                 ast_log(AST_LOG_NOTICE, "Failed to process Stasis configuration; using defaults\n");
159                                 ao2_global_obj_replace(confs, conf);
160                                 return 0;
161                         }
162                         return -1;
163                 case ACO_PROCESS_OK:
164                 case ACO_PROCESS_UNCHANGED:
165                         break;
166                 }
167
168         return 0;
169 }
170
171 static void config_exit(void)
172 {
173         aco_info_destroy(&cfg_info);
174 }
175
176 int stasis_config_init(void)
177 {
178         if (aco_info_init(&cfg_info)) {
179                 aco_info_destroy(&cfg_info);
180                 return -1;
181         }
182
183         ast_register_atexit(config_exit);
184
185         /* threadpool section */
186         aco_option_register(&cfg_info, "initial_size", ACO_EXACT,
187                 threadpool_options, "0", OPT_INT_T, PARSE_IN_RANGE,
188                 FLDSET(struct stasis_threadpool_conf, initial_size), 0,
189                 INT_MAX);
190         aco_option_register(&cfg_info, "idle_timeout_sec", ACO_EXACT,
191                 threadpool_options, "20", OPT_INT_T, PARSE_IN_RANGE,
192                 FLDSET(struct stasis_threadpool_conf, idle_timeout_sec), 0,
193                 INT_MAX);
194         aco_option_register(&cfg_info, "max_size", ACO_EXACT,
195                 threadpool_options, "200", OPT_INT_T, PARSE_IN_RANGE,
196                 FLDSET(struct stasis_threadpool_conf, max_size), 0, INT_MAX);
197
198         return process_config(0);
199 }