lua_error(L);
}
- lua_Number interval_lua = lua_tonumber(L, 1);
- if (!(interval_lua >= 0 && interval_lua < UINT_MAX)) {
+ lua_Integer interval_lua = lua_tointeger(L, 1);
+ if (!(interval_lua > 0 && interval_lua < UINT_MAX)) {
format_error(L, "invalid interval specified, it must be in range > 0, < " xstr(UINT_MAX));
lua_error(L);
}
* @param table pointer to LRU
* @param key_ lookup key
* @param len_ key lengthkeys
- * @param res pointer to bool to store result of operation
+ * @param is_new pointer to bool to store result of operation
* (true if entry is newly added, false otherwise; can be NULL).
* @return pointer to data or NULL (can be even if memory could be allocated!)
*/
-#define lru_get_new(table, key_, len_, res) \
+#define lru_get_new(table, key_, len_, is_new) \
(__typeof__((table)->pdata_t)) \
lru_get_impl(&(table)->lru, (key_), (len_), \
- sizeof(*(table)->pdata_t), true, res)
+ sizeof(*(table)->pdata_t), true, is_new)
/**
* @brief Apply a function to every item in LRU.
uint64_t now = kr_now();
/* Name server is better candidate if it has address record. */
- uint8_t *it = pack_head(*addr_set);
- while (it != pack_tail(*addr_set)) {
+ for (uint8_t *it = pack_head(*addr_set); it != pack_tail(*addr_set);
+ it = pack_obj_next(it)) {
void *val = pack_obj_val(it);
size_t len = pack_obj_len(it);
unsigned favour = 0;
}
if (!is_valid) {
- goto get_next_iterator;
+ continue;
}
- /* Get RTT for this address (if known) */
+ /* Get score for the current address. */
kr_nsrep_rtt_lru_entry_t *cached = rtt_cache ?
lru_get_try(rtt_cache, val, len) :
NULL;
break;
}
}
- get_next_iterator:
- it = pack_obj_next(it);
}
/* At this point, rtt_cache_entry_ptr contains up to KR_NSREP_MAXADDR
int kr_nsrep_update_rtt(struct kr_nsrep *ns, const struct sockaddr *addr,
unsigned score, kr_nsrep_rtt_lru_t *cache, int umode)
{
- if (!cache || umode > KR_NS_MAX) {
+ if (!cache || umode > KR_NS_MAX || umode < 0) {
return kr_error(EINVAL);
}
if (score <= KR_NS_GLUED) {
score = KR_NS_GLUED + 1;
}
- /* First update is always set unless KR_NS_UPDATE_NORESET mode used. */
- if (is_new_entry) {
- if (umode == KR_NS_UPDATE_NORESET) {
- /* Zero initial value. */
- cur->score = 0;
- } else {
- /* Force KR_NS_RESET otherwise. */
- umode = KR_NS_RESET;
- }
+ /* If there's nothing to update, we reset it unless KR_NS_UPDATE_NORESET
+ * mode was requested. New items are zeroed by LRU automatically. */
+ if (is_new_entry && umode != KR_NS_UPDATE_NORESET) {
+ umode = KR_NS_RESET;
}
unsigned new_score = 0;
/* Update score, by default smooth over last two measurements. */
case KR_NS_RESET: new_score = score; break;
case KR_NS_ADD: new_score = MIN(KR_NS_MAX_SCORE - 1, cur->score + score); break;
case KR_NS_MAX: new_score = MAX(cur->score, score); break;
- default: break;
+ default: return kr_error(EINVAL);
}
/* Score limits */
if (new_score > KR_NS_MAX_SCORE) {