Make group weight range [1, 256], inclusive

We do -+1 on serialization and deserialization since the field is 1
byte.  This change also parameterized range so that we can change it
easily.
This commit is contained in:
Tatsuhiro Tsujikawa 2014-04-01 22:54:20 +09:00
parent f2d945734e
commit da5db205ca
6 changed files with 58 additions and 26 deletions

View File

@ -123,7 +123,14 @@ typedef struct {
* *
* The maximum weight of priority group. * The maximum weight of priority group.
*/ */
#define NGHTTP2_MAX_WEIGHT 255 #define NGHTTP2_MAX_WEIGHT 256
/**
* @macro
*
* The minimum weight of priority group.
*/
#define NGHTTP2_MIN_WEIGHT 1
/** /**
* @macro * @macro
@ -717,7 +724,7 @@ typedef struct {
/** /**
* The weight of the priority group * The weight of the priority group
*/ */
uint8_t weight; int32_t weight;
} nghttp2_priority_group; } nghttp2_priority_group;
/** /**
@ -2073,13 +2080,21 @@ const char* nghttp2_strerror(int lib_error_code);
* @function * @function
* *
* Initializes |pri_spec| with priority group ID |pri_group_id| and * Initializes |pri_spec| with priority group ID |pri_group_id| and
* its weight |weight|. To specify weight for the default priority * its weight |weight|.
* group (which is the same as the stream ID of the stream) in *
* `nghttp2_submit_request()` and `nghttp2_submit_headers()` and its * The |weight| must be in [:enum:`NGHTTP2_MIN_WEIGHT`,
* stream ID is not known in advance, specify -1 to |pri_group_id|. * :enum:`NGHTTP2_MAX_WEIGHT`], inclusive. If |weight| is strictly
* less than :enum:`NGHTTP2_MIN_WEIGHT`, it becomes
* :enum:`NGHTTP2_MIN_WEIGHT`. If it is strictly greater than
* :enum:`NGHTTP2_MAX_WEIGHT`, it becomes :enum:`NGHTTP2_MAX_WEIGHT`.
*
* To specify weight for the default priority group (which is the same
* as the stream ID of the stream) in `nghttp2_submit_request()` and
* `nghttp2_submit_headers()` and its stream ID is not known in
* advance, specify -1 to |pri_group_id|.
*/ */
void nghttp2_priority_spec_group_init(nghttp2_priority_spec *pri_spec, void nghttp2_priority_spec_group_init(nghttp2_priority_spec *pri_spec,
int32_t pri_group_id, uint8_t weight); int32_t pri_group_id, int32_t weight);
/** /**
* @function * @function

View File

@ -390,7 +390,7 @@ void nghttp2_frame_pack_priority_spec(uint8_t *buf,
case NGHTTP2_PRIORITY_TYPE_GROUP: case NGHTTP2_PRIORITY_TYPE_GROUP:
nghttp2_put_uint32be(buf, pri_spec->group.pri_group_id); nghttp2_put_uint32be(buf, pri_spec->group.pri_group_id);
buf[4] = pri_spec->group.weight; buf[4] = pri_spec->group.weight - 1;
return; return;
case NGHTTP2_PRIORITY_TYPE_DEP: case NGHTTP2_PRIORITY_TYPE_DEP:
@ -413,10 +413,10 @@ void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
{ {
if(flags & NGHTTP2_FLAG_PRIORITY_GROUP) { if(flags & NGHTTP2_FLAG_PRIORITY_GROUP) {
int32_t pri_group_id; int32_t pri_group_id;
uint8_t weight; int32_t weight;
pri_group_id = nghttp2_get_uint32(payload) & NGHTTP2_PRI_GROUP_ID_MASK; pri_group_id = nghttp2_get_uint32(payload) & NGHTTP2_PRI_GROUP_ID_MASK;
weight = payload[4]; weight = payload[4] + 1;
nghttp2_priority_spec_group_init(pri_spec, pri_group_id, weight); nghttp2_priority_spec_group_init(pri_spec, pri_group_id, weight);
} else if(flags & NGHTTP2_FLAG_PRIORITY_DEPENDENCY) { } else if(flags & NGHTTP2_FLAG_PRIORITY_DEPENDENCY) {

View File

@ -33,11 +33,11 @@
#include "nghttp2_frame.h" #include "nghttp2_frame.h"
/* A bit higher weight for non-DATA frames */ /* A bit higher weight for non-DATA frames */
#define NGHTTP2_OB_EX_WEIGHT 256 #define NGHTTP2_OB_EX_WEIGHT 300
/* Higher weight for SETTINGS */ /* Higher weight for SETTINGS */
#define NGHTTP2_OB_SETTINGS_WEIGHT 257 #define NGHTTP2_OB_SETTINGS_WEIGHT 301
/* Highest weight for PING */ /* Highest weight for PING */
#define NGHTTP2_OB_PING_WEIGHT 258 #define NGHTTP2_OB_PING_WEIGHT 302
typedef struct { typedef struct {
nghttp2_data_provider *data_prd; nghttp2_data_provider *data_prd;

View File

@ -25,7 +25,7 @@
#include "nghttp2_priority_spec.h" #include "nghttp2_priority_spec.h"
void nghttp2_priority_spec_group_init(nghttp2_priority_spec *pri_spec, void nghttp2_priority_spec_group_init(nghttp2_priority_spec *pri_spec,
int32_t pri_group_id, uint8_t weight) int32_t pri_group_id, int32_t weight)
{ {
pri_spec->pri_type = NGHTTP2_PRIORITY_TYPE_GROUP; pri_spec->pri_type = NGHTTP2_PRIORITY_TYPE_GROUP;
pri_spec->group.pri_group_id = pri_group_id; pri_spec->group.pri_group_id = pri_group_id;

View File

@ -748,7 +748,7 @@ nghttp2_stream* nghttp2_session_open_stream(nghttp2_session *session,
nghttp2_stream *dep_stream; nghttp2_stream *dep_stream;
nghttp2_stream *root_stream; nghttp2_stream *root_stream;
int32_t pri_group_id; int32_t pri_group_id;
uint8_t weight; int32_t weight;
nghttp2_stream_group *stream_group; nghttp2_stream_group *stream_group;
dep_stream = NULL; dep_stream = NULL;
@ -1883,7 +1883,7 @@ static int session_call_on_frame_send(nghttp2_session *session,
static void outbound_item_cycle_weight(nghttp2_outbound_item *item, static void outbound_item_cycle_weight(nghttp2_outbound_item *item,
int32_t ini_weight) int32_t ini_weight)
{ {
if(item->weight == 0 || item->weight > ini_weight) { if(item->weight == NGHTTP2_MIN_WEIGHT || item->weight > ini_weight) {
item->weight = ini_weight; item->weight = ini_weight;
} else { } else {
--item->weight; --item->weight;

View File

@ -102,6 +102,17 @@ static int nghttp2_submit_headers_shared
return rv; return rv;
} }
static void adjust_priority_spec_group_weight(nghttp2_priority_spec *pri_spec)
{
assert(pri_spec->pri_type == NGHTTP2_PRIORITY_TYPE_GROUP);
if(pri_spec->group.weight < NGHTTP2_MIN_WEIGHT) {
pri_spec->group.weight = NGHTTP2_MIN_WEIGHT;
} else if(pri_spec->group.weight > NGHTTP2_MAX_WEIGHT) {
pri_spec->group.weight = NGHTTP2_MAX_WEIGHT;
}
}
static int nghttp2_submit_headers_shared_nva static int nghttp2_submit_headers_shared_nva
(nghttp2_session *session, (nghttp2_session *session,
uint8_t flags, uint8_t flags,
@ -114,24 +125,25 @@ static int nghttp2_submit_headers_shared_nva
{ {
ssize_t rv; ssize_t rv;
nghttp2_nv *nva_copy; nghttp2_nv *nva_copy;
nghttp2_priority_spec pri_spec_none = { nghttp2_priority_spec copy_pri_spec = {
NGHTTP2_PRIORITY_TYPE_NONE NGHTTP2_PRIORITY_TYPE_NONE
}; };
const nghttp2_priority_spec *pri_spec_ptr;
rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen); rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen);
if(rv < 0) { if(rv < 0) {
return rv; return rv;
} }
if(pri_spec == NULL) { if(pri_spec) {
pri_spec_ptr = &pri_spec_none; copy_pri_spec = *pri_spec;
} else {
pri_spec_ptr = pri_spec; if(copy_pri_spec.pri_type == NGHTTP2_PRIORITY_TYPE_GROUP) {
adjust_priority_spec_group_weight(&copy_pri_spec);
}
} }
return nghttp2_submit_headers_shared(session, flags, stream_id, return nghttp2_submit_headers_shared(session, flags, stream_id,
pri_spec_ptr, nva_copy, rv, data_prd, &copy_pri_spec, nva_copy, rv, data_prd,
stream_user_data); stream_user_data);
} }
@ -176,16 +188,21 @@ int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags,
{ {
int rv; int rv;
nghttp2_frame *frame; nghttp2_frame *frame;
nghttp2_priority_spec copy_pri_spec;
if(pri_spec == NULL) { if(pri_spec == NULL) {
return NGHTTP2_ERR_INVALID_ARGUMENT; return NGHTTP2_ERR_INVALID_ARGUMENT;
} }
switch(pri_spec->pri_type) { copy_pri_spec = *pri_spec;
switch(copy_pri_spec.pri_type) {
case NGHTTP2_PRIORITY_TYPE_GROUP: case NGHTTP2_PRIORITY_TYPE_GROUP:
adjust_priority_spec_group_weight(&copy_pri_spec);
break; break;
case NGHTTP2_PRIORITY_TYPE_DEP: case NGHTTP2_PRIORITY_TYPE_DEP:
if(stream_id == pri_spec->dep.stream_id) { if(stream_id == copy_pri_spec.dep.stream_id) {
return NGHTTP2_ERR_INVALID_ARGUMENT; return NGHTTP2_ERR_INVALID_ARGUMENT;
} }
@ -200,7 +217,7 @@ int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags,
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
nghttp2_frame_priority_init(&frame->priority, stream_id, pri_spec); nghttp2_frame_priority_init(&frame->priority, stream_id, &copy_pri_spec);
rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL); rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL);