1 |
/* Ignore handling. |
2 |
* |
3 |
* IRC Services is copyright (c) 1996-2009 Andrew Church. |
4 |
* E-mail: <achurch@achurch.org> |
5 |
* Parts written by Andrew Kempe and others. |
6 |
* This program is free but copyrighted software; see the file GPL.txt for |
7 |
* details. |
8 |
*/ |
9 |
|
10 |
#include "services.h" |
11 |
|
12 |
/*************************************************************************/ |
13 |
|
14 |
/* ignore_init: Initialize ignore-related fields of a new User structure. */ |
15 |
|
16 |
void |
17 |
ignore_init(User *u) |
18 |
{ |
19 |
u->ignore = 0; |
20 |
u->lastcmd = time_msec(); |
21 |
u->lastcmd_s = time(NULL); |
22 |
} |
23 |
|
24 |
/*************************************************************************/ |
25 |
|
26 |
/* ignore_update: Update the user's ignore level. The "ignore level" is a |
27 |
* measure of how much time Services has spent processing |
28 |
* this user's commands, and is calculated as a decaying |
29 |
* average of the fraction of time spent on the user's |
30 |
* commands over the total time Services has been running-- |
31 |
* more specifically, the average over time of a function |
32 |
* whose value is 1 when Services is executing a command |
33 |
* from the user and 0 at all other times. The rate of |
34 |
* decay of the average, and hence the rate of response to |
35 |
* new commands, is dependent on the IgnoreDecay |
36 |
* configuration setting: the average decays by half every |
37 |
* IgnoreDecay milliseconds (note that the value is given as |
38 |
* seconds in the configuration file). |
39 |
* The `msec' parameter to this function is the length |
40 |
* of time taken by the most recent command (which caused |
41 |
* this update). `msec' may be specified as zero to update |
42 |
* the user's ignore level at any time. |
43 |
*/ |
44 |
|
45 |
void |
46 |
ignore_update(User *u, uint32 msec) |
47 |
{ |
48 |
time_t now; |
49 |
uint32 now_msec; |
50 |
|
51 |
if (!IgnoreDecay) |
52 |
return; /* Ignore code disabled, just return */ |
53 |
|
54 |
now = time(NULL); |
55 |
now_msec = time_msec(); |
56 |
|
57 |
if (now - u->lastcmd_s > 1000000) |
58 |
{ |
59 |
/* Millisecond counter may have overflowed, just reset to 0 */ |
60 |
u->ignore = 0; |
61 |
} |
62 |
else |
63 |
{ |
64 |
double zerolen = (now_msec - msec) - u->lastcmd; |
65 |
|
66 |
if (zerolen > 0) |
67 |
u->ignore *= pow(2, -(zerolen / IgnoreDecay)); |
68 |
} |
69 |
|
70 |
if (msec) |
71 |
{ |
72 |
double factor; |
73 |
|
74 |
while (msec > IgnoreDecay) |
75 |
{ |
76 |
u->ignore = u->ignore * 0.5 + 0.5; |
77 |
msec -= IgnoreDecay; |
78 |
} |
79 |
|
80 |
factor = pow(2, -((double) msec / IgnoreDecay)); |
81 |
u->ignore = u->ignore * factor + (1 - factor); |
82 |
} |
83 |
|
84 |
u->lastcmd = now_msec; |
85 |
u->lastcmd_s = now; |
86 |
} |
87 |
|
88 |
/*************************************************************************/ |
89 |
|
90 |
/* |
91 |
* Local variables: |
92 |
* c-file-style: "stroustrup" |
93 |
* c-file-offsets: ((case-label . *) (statement-case-intro . *)) |
94 |
* indent-tabs-mode: nil |
95 |
* End: |
96 |
* |
97 |
* vim: expandtab shiftwidth=4: |
98 |
*/ |