Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
* h2 workers. As worker threads are a scarce resource,
* we need to take measures that we do not get DoSed.
*
* This is what we call an 'idle block'. Limit the amount
* of busy workers we allow for this connection until it
* well behaves.
*/
now = apr_time_now();
m->last_idle_block = now;
if (m->limit_active > 2
&& now - m->last_limit_change >= m->limit_change_interval) {
if (m->limit_active > 16) {
m->limit_active = 16;
}
else if (m->limit_active > 8) {
m->limit_active = 8;
}
else if (m->limit_active > 4) {
m->limit_active = 4;
}
else if (m->limit_active > 2) {
m->limit_active = 2;
}
m->last_limit_change = now;
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, m->c,
"h2_mplx(%ld): decrease worker limit to %d",
m->id, m->limit_active);
}
if (m->tasks_active > m->limit_active) {
status = unschedule_slow_tasks(m);
}
}
else if (!h2_iq_empty(m->q)) {
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, m->c,
"h2_mplx(%ld): idle, but %d streams to process",
m->id, (int)h2_iq_count(m->q));
status = APR_EAGAIN;
}
else {
/* idle, have streams, but no tasks active. what are we waiting for?
* WINDOW_UPDATEs from client? */
h2_stream *stream = NULL;
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, m->c,
"h2_mplx(%ld): idle, no tasks ongoing, %d streams",
m->id, (int)h2_ihash_count(m->streams));
h2_ihash_shift(m->streams, (void**)&stream, 1);
if (stream) {
h2_ihash_add(m->streams, stream);
if (stream->output && !stream->out_checked) {
/* FIXME: this looks like a race between the session thinking
* it is idle and the EOF on a stream not being sent.
* Signal to caller to leave IDLE state.
*/
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, m->c,
H2_STRM_MSG(stream, "output closed=%d, mplx idle"
", out has %ld bytes buffered"),
h2_beam_is_closed(stream->output),
(long)h2_beam_get_buffered(stream->output));
h2_ihash_add(m->streams, stream);
check_data_for(m, stream, 0);
stream->out_checked = 1;
status = APR_EAGAIN;
}
}
}
}
register_if_needed(m);
H2_MPLX_LEAVE(m);
return status;
}
/*******************************************************************************
* HTTP/2 request engines
******************************************************************************/
typedef struct {
h2_mplx * m;
h2_req_engine *ngn;
int streams_updated;
} ngn_update_ctx;
static int ngn_update_window(void *ctx, void *val)
{
ngn_update_ctx *uctx = ctx;
h2_stream *stream = val;
if (stream->task && stream->task->assigned == uctx->ngn
&& output_consumed_signal(uctx->m, stream->task)) {
++uctx->streams_updated;
}
return 1;
}
static apr_status_t ngn_out_update_windows(h2_mplx *m, h2_req_engine *ngn)
{
ngn_update_ctx ctx;
ctx.m = m;
ctx.ngn = ngn;
ctx.streams_updated = 0;
h2_ihash_iter(m->streams, ngn_update_window, &ctx);
return ctx.streams_updated? APR_SUCCESS : APR_EAGAIN;
}
apr_status_t h2_mplx_req_engine_push(const char *ngn_type,
request_rec *r,
http2_req_engine_init *einit)
{
apr_status_t status;
h2_mplx *m;
h2_task *task;
h2_stream *stream;
task = h2_ctx_rget_task(r);
if (!task) {
return APR_ECONNABORTED;
}
m = task->mplx;
H2_MPLX_ENTER(m);
stream = h2_ihash_get(m->streams, task->stream_id);
if (stream) {
status = h2_ngn_shed_push_request(m->ngn_shed, ngn_type, r, einit);
}
else {
status = APR_ECONNABORTED;
}
H2_MPLX_LEAVE(m);
return status;
}
apr_status_t h2_mplx_req_engine_pull(h2_req_engine *ngn,
apr_read_type_e block,
int capacity,
request_rec **pr)
{
h2_ngn_shed *shed = h2_ngn_shed_get_shed(ngn);
h2_mplx *m = h2_ngn_shed_get_ctx(shed);
apr_status_t status;
int want_shutdown;
H2_MPLX_ENTER(m);
want_shutdown = (block == APR_BLOCK_READ);
/* Take this opportunity to update output consummation
* for this engine */
ngn_out_update_windows(m, ngn);
if (want_shutdown && !h2_iq_empty(m->q)) {
/* For a blocking read, check first if requests are to be
* had and, if not, wait a short while before doing the
* blocking, and if unsuccessful, terminating read.
*/
status = h2_ngn_shed_pull_request(shed, ngn, capacity, 1, pr);
if (APR_STATUS_IS_EAGAIN(status)) {
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, m->c,
"h2_mplx(%ld): start block engine pull", m->id);
apr_thread_cond_timedwait(m->task_thawed, m->lock,
apr_time_from_msec(20));
status = h2_ngn_shed_pull_request(shed, ngn, capacity, 1, pr);
}
}
else {
status = h2_ngn_shed_pull_request(shed, ngn, capacity,
want_shutdown, pr);
}
H2_MPLX_LEAVE(m);
return status;
}
void h2_mplx_req_engine_done(h2_req_engine *ngn, conn_rec *r_conn,
apr_status_t status)
{
h2_task *task = h2_ctx_cget_task(r_conn);
if (task) {
h2_mplx *m = task->mplx;
h2_stream *stream;
H2_MPLX_ENTER_ALWAYS(m);
stream = h2_ihash_get(m->streams, task->stream_id);
ngn_out_update_windows(m, ngn);
h2_ngn_shed_done_task(m->ngn_shed, ngn, task);
if (status != APR_SUCCESS && stream
&& h2_task_can_redo(task)
&& !h2_ihash_get(m->sredo, stream->id)) {
h2_ihash_add(m->sredo, stream);
}
if (task->engine) {
/* cannot report that as done until engine returns */
}
else {
task_done(m, task, ngn);
}
H2_MPLX_LEAVE(m);
}
}
/*******************************************************************************
* mplx master events dispatching
******************************************************************************/
int h2_mplx_has_master_events(h2_mplx *m)
{
return apr_atomic_read32(&m->event_pending) > 0;
}
apr_status_t h2_mplx_dispatch_master_events(h2_mplx *m,
stream_ev_callback *on_resume,
void *on_ctx)
{
h2_stream *stream;
int n, id;
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, m->c,
"h2_mplx(%ld): dispatch events", m->id);
apr_atomic_set32(&m->event_pending, 0);
/* update input windows for streams */
h2_ihash_iter(m->streams, report_consumption_iter, m);
purge_streams(m, 1);
n = h2_ififo_count(m->readyq);
while (n > 0
&& (h2_ififo_try_pull(m->readyq, &id) == APR_SUCCESS)) {
--n;
stream = h2_ihash_get(m->streams, id);
if (stream) {
on_resume(on_ctx, stream);
}
}
return APR_SUCCESS;
}
apr_status_t h2_mplx_keep_active(h2_mplx *m, h2_stream *stream)
{
check_data_for(m, stream, 1);
return APR_SUCCESS;
}
int h2_mplx_awaits_data(h2_mplx *m)
{
int waiting = 1;
H2_MPLX_ENTER_ALWAYS(m);
if (h2_ihash_empty(m->streams)) {
waiting = 0;
}
else if (!m->tasks_active && !h2_ififo_count(m->readyq)
&& h2_iq_empty(m->q)) {
waiting = 0;
}
H2_MPLX_LEAVE(m);
return waiting;
}