RTS API Documentation  1.10.11
Macros | Functions
LIMIT code
+ Collaboration diagram for LIMIT code:

Macros

#define SWITCH_LIMIT_INCR(name)   static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource, const int max, const int interval)
 
#define SWITCH_LIMIT_RELEASE(name)   static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource)
 
#define SWITCH_LIMIT_USAGE(name)   static int name (const char *realm, const char *resource, uint32_t *rcount)
 
#define SWITCH_LIMIT_RESET(name)   static switch_status_t name (void)
 
#define SWITCH_LIMIT_INTERVAL_RESET(name)   static switch_status_t name (const char *realm, const char *resource)
 
#define SWITCH_LIMIT_STATUS(name)   static char * name (void)
 
#define LIMIT_IGNORE_TRANSFER_VARIABLE   "limit_ignore_transfer"
 
#define LIMIT_BACKEND_VARIABLE   "limit_backend"
 
#define LIMIT_EVENT_USAGE   "limit::usage"
 
#define LIMIT_DEF_XFER_EXTEN   "limit_exceeded"
 

Functions

SWITCH_BEGIN_EXTERN_C void switch_limit_init (switch_memory_pool_t *pool)
 Initilize the LIMIT Core System. More...
 
switch_status_t switch_limit_incr (const char *backend, switch_core_session_t *session, const char *realm, const char *resource, const int max, const int interval)
 Increment resource. More...
 
switch_status_t switch_limit_release (const char *backend, switch_core_session_t *session, const char *realm, const char *resource)
 Release resource. More...
 
int switch_limit_usage (const char *backend, const char *realm, const char *resource, uint32_t *rcount)
 get usage count for resource More...
 
switch_status_t switch_limit_interval_reset (const char *backend, const char *realm, const char *resource)
 reset interval usage counter for a given resource More...
 
switch_status_t switch_limit_reset (const char *backend)
 reset all usage counters More...
 
void switch_limit_fire_event (const char *backend, const char *realm, const char *resource, uint32_t usage, uint32_t rate, uint32_t max, uint32_t ratemax)
 fire event for limit usage More...
 
char * switch_limit_status (const char *backend)
 retrieve arbitrary status information More...
 

Detailed Description

Macro Definition Documentation

◆ LIMIT_BACKEND_VARIABLE

#define LIMIT_BACKEND_VARIABLE   "limit_backend"

Definition at line 121 of file switch_limit.h.

Referenced by limit_state_handler(), and switch_limit_incr().

◆ LIMIT_DEF_XFER_EXTEN

#define LIMIT_DEF_XFER_EXTEN   "limit_exceeded"

Definition at line 123 of file switch_limit.h.

◆ LIMIT_EVENT_USAGE

#define LIMIT_EVENT_USAGE   "limit::usage"

Definition at line 122 of file switch_limit.h.

Referenced by switch_limit_fire_event(), and switch_limit_init().

◆ LIMIT_IGNORE_TRANSFER_VARIABLE

#define LIMIT_IGNORE_TRANSFER_VARIABLE   "limit_ignore_transfer"

Definition at line 120 of file switch_limit.h.

Referenced by limit_state_handler().

◆ SWITCH_LIMIT_INCR

#define SWITCH_LIMIT_INCR (   name)    static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource, const int max, const int interval)

callback to init a backend

Definition at line 113 of file switch_limit.h.

◆ SWITCH_LIMIT_INTERVAL_RESET

#define SWITCH_LIMIT_INTERVAL_RESET (   name)    static switch_status_t name (const char *realm, const char *resource)

Definition at line 117 of file switch_limit.h.

◆ SWITCH_LIMIT_RELEASE

#define SWITCH_LIMIT_RELEASE (   name)    static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource)

Definition at line 114 of file switch_limit.h.

◆ SWITCH_LIMIT_RESET

#define SWITCH_LIMIT_RESET (   name)    static switch_status_t name (void)

Definition at line 116 of file switch_limit.h.

◆ SWITCH_LIMIT_STATUS

#define SWITCH_LIMIT_STATUS (   name)    static char * name (void)

Definition at line 118 of file switch_limit.h.

◆ SWITCH_LIMIT_USAGE

#define SWITCH_LIMIT_USAGE (   name)    static int name (const char *realm, const char *resource, uint32_t *rcount)

Definition at line 115 of file switch_limit.h.

Function Documentation

◆ switch_limit_fire_event()

void switch_limit_fire_event ( const char *  backend,
const char *  realm,
const char *  resource,
uint32_t  usage,
uint32_t  rate,
uint32_t  max,
uint32_t  ratemax 
)

fire event for limit usage

Parameters
backendto use
realm
resource
usage
rate
max
ratemax

Definition at line 60 of file switch_limit.c.

References LIMIT_EVENT_USAGE, switch_event_add_header(), switch_event_add_header_string(), switch_event_create_subclass, SWITCH_EVENT_CUSTOM, switch_event_fire, SWITCH_STACK_BOTTOM, and SWITCH_STATUS_SUCCESS.

61 {
62  switch_event_t *event;
63 
65  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "backend", backend);
68  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "usage", "%d", usage);
69  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "rate", "%d", rate);
70  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "max", "%d", max);
71  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ratemax", "%d", ratemax);
72  switch_event_fire(&event);
73  }
74 }
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:413
#define LIMIT_EVENT_USAGE
Definition: switch_limit.h:122
switch_status_t switch_event_add_header(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *fmt,...) PRINTF_FUNCTION(4
Add a header to an event.
Representation of an event.
Definition: switch_event.h:80
#define switch_event_create_subclass(_e, _eid, _sn)
Definition: switch_event.h:153
static const char usage[]
Definition: switch.c:419
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
char * key
Definition: switch_msrp.c:64

◆ switch_limit_incr()

switch_status_t switch_limit_incr ( const char *  backend,
switch_core_session_t session,
const char *  realm,
const char *  resource,
const int  max,
const int  interval 
)

Increment resource.

Parameters
backendto use
realm
resource
max- 0 means no limit, just count
interval- 0 means no interval
Returns
true/false - true ok, false over limit

Definition at line 109 of file switch_limit.c.

References get_backend(), switch_limit_interface::incr, LIMIT_BACKEND_VARIABLE, limit_state_handler(), release_backend(), switch_channel_get_variable, SWITCH_CHANNEL_SESSION_LOG, switch_channel_set_variable, switch_channel_set_variable_printf(), switch_core_session_get_channel(), switch_goto_status, SWITCH_LOG_DEBUG, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_GENERR, and SWITCH_STATUS_SUCCESS.

109  {
110  switch_limit_interface_t *limit = NULL;
111  switch_channel_t *channel = NULL;
112  int status = SWITCH_STATUS_SUCCESS;
113 
114  assert(session);
115 
116  channel = switch_core_session_get_channel(session);
117 
118  /* locate impl, call appropriate func */
119  if (!(limit = get_backend(backend))) {
120  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
122  }
123 
124  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "incr called: %s_%s max:%d, interval:%d\n",
125  realm, resource, max, interval);
126 
127  if ((status = limit->incr(session, realm, resource, max, interval)) == SWITCH_STATUS_SUCCESS) {
128  /* race condition? what if another leg is doing the same thing? */
129  const char *existing = switch_channel_get_variable(channel, LIMIT_BACKEND_VARIABLE);
130  if (existing) {
131  if (!strstr(existing, backend)) {
132  switch_channel_set_variable_printf(channel, LIMIT_BACKEND_VARIABLE, "%s,%s", existing, backend);
133  }
134  } else {
136  switch_core_event_hook_add_state_change(session, limit_state_handler);
137  }
138  }
139 
140  release_backend(limit);
141 
142 end:
143  return status;
144 }
switch_status_t switch_channel_set_variable_printf(switch_channel_t *channel, const char *varname, const char *fmt,...)
#define SWITCH_CHANNEL_SESSION_LOG(x)
Abstract interface to a limit module.
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
#define switch_channel_get_variable(_c, _v)
static void release_backend(switch_limit_interface_t *limit)
Definition: switch_limit.c:49
#define switch_goto_status(_status, _label)
Definition: switch_utils.h:287
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static switch_limit_interface_t * get_backend(const char *backend)
Definition: switch_limit.c:35
#define LIMIT_BACKEND_VARIABLE
Definition: switch_limit.h:121
#define switch_channel_set_variable(_channel, _var, _val)
switch_status_t(* incr)(switch_core_session_t *session, const char *realm, const char *resource, const int max, const int interval)
static switch_status_t limit_state_handler(switch_core_session_t *session)
Definition: switch_limit.c:76

◆ switch_limit_init()

SWITCH_BEGIN_EXTERN_C void switch_limit_init ( switch_memory_pool_t pool)

Initilize the LIMIT Core System.

Parameters
poolthe memory pool to use for long term allocations
Note
Generally called by the core_init

Definition at line 53 of file switch_limit.c.

References LIMIT_EVENT_USAGE, SWITCH_CHANNEL_LOG, switch_event_reserve_subclass, SWITCH_LOG_ERROR, switch_log_printf(), and SWITCH_STATUS_SUCCESS.

53  {
55  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't register event subclass \"%s\"", LIMIT_EVENT_USAGE);
56  }
57 }
#define SWITCH_CHANNEL_LOG
#define LIMIT_EVENT_USAGE
Definition: switch_limit.h:122
#define switch_event_reserve_subclass(subclass_name)
Reserve a subclass assuming the owner string is the current filename.
Definition: switch_event.h:375
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.

◆ switch_limit_interval_reset()

switch_status_t switch_limit_interval_reset ( const char *  backend,
const char *  realm,
const char *  resource 
)

reset interval usage counter for a given resource

Parameters
backend
realm
resource

Definition at line 197 of file switch_limit.c.

References get_backend(), switch_limit_interface::interval_reset, release_backend(), SWITCH_CHANNEL_LOG, switch_goto_status, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_GENERR, and SWITCH_STATUS_SUCCESS.

197  {
198  switch_limit_interface_t *limit = NULL;
199  int status = SWITCH_STATUS_SUCCESS;
200 
201  /* locate impl, call appropriate func */
202  if (!(limit = get_backend(backend))) {
203  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
205  }
206 
207  if (!limit->interval_reset) {
208  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s does not implement interval_reset!\n", backend);
210  }
211 
212  status = limit->interval_reset(realm, resource);
213 
214 end:
215  release_backend(limit);
216  return status;
217 }
Abstract interface to a limit module.
#define SWITCH_CHANNEL_LOG
switch_status_t(* interval_reset)(const char *realm, const char *resource)
static void release_backend(switch_limit_interface_t *limit)
Definition: switch_limit.c:49
#define switch_goto_status(_status, _label)
Definition: switch_utils.h:287
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static switch_limit_interface_t * get_backend(const char *backend)
Definition: switch_limit.c:35

◆ switch_limit_release()

switch_status_t switch_limit_release ( const char *  backend,
switch_core_session_t session,
const char *  realm,
const char *  resource 
)

Release resource.

Parameters
backendto use
realm
resource
Returns
true/false - true ok, false over limit

Definition at line 146 of file switch_limit.c.

References get_backend(), switch_limit_interface::release, release_backend(), SWITCH_CHANNEL_SESSION_LOG, switch_goto_status, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_GENERR, and SWITCH_STATUS_SUCCESS.

Referenced by limit_state_handler().

146  {
147  switch_limit_interface_t *limit = NULL;
148  int status = SWITCH_STATUS_SUCCESS;
149 
150  /* locate impl, call appropriate func */
151  if (!(limit = get_backend(backend))) {
152  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
154  }
155 
156  status = limit->release(session, realm, resource);
157 
158 end:
159  release_backend(limit);
160  return status;
161 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
Abstract interface to a limit module.
switch_status_t(* release)(switch_core_session_t *session, const char *realm, const char *resource)
static void release_backend(switch_limit_interface_t *limit)
Definition: switch_limit.c:49
#define switch_goto_status(_status, _label)
Definition: switch_utils.h:287
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static switch_limit_interface_t * get_backend(const char *backend)
Definition: switch_limit.c:35

◆ switch_limit_reset()

switch_status_t switch_limit_reset ( const char *  backend)

reset all usage counters

Parameters
backendto use

Definition at line 180 of file switch_limit.c.

References get_backend(), release_backend(), switch_limit_interface::reset, SWITCH_CHANNEL_LOG, switch_goto_status, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_GENERR, and SWITCH_STATUS_SUCCESS.

180  {
181  switch_limit_interface_t *limit = NULL;
182  int status = SWITCH_STATUS_SUCCESS;
183 
184  /* locate impl, call appropriate func */
185  if (!(limit = get_backend(backend))) {
186  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
188  }
189 
190  status = limit->reset();
191 
192 end:
193  release_backend(limit);
194  return status;
195 }
Abstract interface to a limit module.
#define SWITCH_CHANNEL_LOG
switch_status_t(* reset)(void)
static void release_backend(switch_limit_interface_t *limit)
Definition: switch_limit.c:49
#define switch_goto_status(_status, _label)
Definition: switch_utils.h:287
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static switch_limit_interface_t * get_backend(const char *backend)
Definition: switch_limit.c:35

◆ switch_limit_status()

char* switch_limit_status ( const char *  backend)

retrieve arbitrary status information

Parameters
backendto use
Note
caller must free returned value

Definition at line 219 of file switch_limit.c.

References get_backend(), release_backend(), switch_limit_interface::status, SWITCH_CHANNEL_LOG, switch_goto_status, SWITCH_LOG_ERROR, and switch_log_printf().

219  {
220  switch_limit_interface_t *limit = NULL;
221  char *status = NULL;
222 
223  /* locate impl, call appropriate func */
224  if (!(limit = get_backend(backend))) {
225  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
226  switch_goto_status(strdup("-ERR"), end);
227  }
228 
229  status = limit->status();
230 
231 end:
232  release_backend(limit);
233  return status;
234 }
Abstract interface to a limit module.
#define SWITCH_CHANNEL_LOG
static void release_backend(switch_limit_interface_t *limit)
Definition: switch_limit.c:49
#define switch_goto_status(_status, _label)
Definition: switch_utils.h:287
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static switch_limit_interface_t * get_backend(const char *backend)
Definition: switch_limit.c:35

◆ switch_limit_usage()

int switch_limit_usage ( const char *  backend,
const char *  realm,
const char *  resource,
uint32_t *  rcount 
)

get usage count for resource

Parameters
backendto use
realm
resource
rcount- output paramter, rate counter

Definition at line 163 of file switch_limit.c.

References get_backend(), release_backend(), SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), usage, and switch_limit_interface::usage.

163  {
164  switch_limit_interface_t *limit = NULL;
165  int usage = 0;
166 
167  /* locate impl, call appropriate func */
168  if (!(limit = get_backend(backend))) {
169  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
170  goto end;
171  }
172 
173  usage = limit->usage(realm, resource, rcount);
174 
175 end:
176  release_backend(limit);
177  return usage;
178 }
Abstract interface to a limit module.
#define SWITCH_CHANNEL_LOG
static const char usage[]
Definition: switch.c:419
static void release_backend(switch_limit_interface_t *limit)
Definition: switch_limit.c:49
int(* usage)(const char *realm, const char *resource, uint32_t *rcount)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static switch_limit_interface_t * get_backend(const char *backend)
Definition: switch_limit.c:35