Session expiry interval support - not working for file persistence.
parent
12431d7d24
commit
ac91144495
@ -0,0 +1,103 @@
|
||||
/*
|
||||
Copyright (c) 2019 Roger Light <roger@atchoo.org>
|
||||
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License v1.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
The Eclipse Public License is available at
|
||||
http://www.eclipse.org/legal/epl-v10.html
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
Contributors:
|
||||
Roger Light - initial implementation and documentation.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <utlist.h>
|
||||
|
||||
#include "mosquitto_broker_internal.h"
|
||||
#include "memory_mosq.h"
|
||||
#include "time_mosq.h"
|
||||
|
||||
static struct session_expiry_list *expiry_list = NULL;
|
||||
static time_t last_check = 0;
|
||||
|
||||
|
||||
static int session_expiry__cmp(struct session_expiry_list *i1, struct session_expiry_list *i2)
|
||||
{
|
||||
return i1->context->session_expiry_interval - i2->context->session_expiry_interval;
|
||||
}
|
||||
|
||||
|
||||
int session_expiry__add(struct mosquitto *context)
|
||||
{
|
||||
struct session_expiry_list *item;
|
||||
|
||||
item = mosquitto__calloc(1, sizeof(struct session_expiry_list));
|
||||
if(!item) return MOSQ_ERR_NOMEM;
|
||||
|
||||
item->context = context;
|
||||
item->context->session_expiry_time = time(NULL) + item->context->session_expiry_interval;
|
||||
context->expiry_list_item = item;
|
||||
|
||||
DL_INSERT_INORDER(expiry_list, item, session_expiry__cmp);
|
||||
|
||||
return MOSQ_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void session_expiry__remove(struct mosquitto *context)
|
||||
{
|
||||
if(context->expiry_list_item){
|
||||
DL_DELETE(expiry_list, context->expiry_list_item);
|
||||
mosquitto__free(context->expiry_list_item);
|
||||
context->expiry_list_item = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Call on broker shutdown only */
|
||||
void session_expiry__remove_all(struct mosquitto_db *db)
|
||||
{
|
||||
struct session_expiry_list *item, *tmp;
|
||||
struct mosquitto *context;
|
||||
|
||||
DL_FOREACH_SAFE(expiry_list, item, tmp){
|
||||
context = item->context;
|
||||
session_expiry__remove(context);
|
||||
context->session_expiry_interval = 0;
|
||||
context->will_delay_interval = 0;
|
||||
context__send_will(db, context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void session_expiry__check(struct mosquitto_db *db, time_t now)
|
||||
{
|
||||
struct session_expiry_list *item, *tmp;
|
||||
struct mosquitto *context;
|
||||
|
||||
if(now <= last_check) return;
|
||||
|
||||
last_check = now;
|
||||
|
||||
DL_FOREACH_SAFE(expiry_list, item, tmp){
|
||||
if(item->context->session_expiry_time < now){
|
||||
context = item->context;
|
||||
session_expiry__remove(context);
|
||||
|
||||
context->session_expiry_interval = 0;
|
||||
context__send_will(db, context);
|
||||
context__add_to_disused(db, context);
|
||||
}else{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# MQTT v5. Test whether session expiry interval works correctly.
|
||||
|
||||
from mosq_test_helper import *
|
||||
|
||||
rc = 1
|
||||
keepalive = 60
|
||||
|
||||
props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_SESSION_EXPIRY_INTERVAL, 1)
|
||||
connect_packet = mosq_test.gen_connect("clean-qos2-test", keepalive=keepalive, clean_session=False, proto_ver=5, properties=props)
|
||||
connack1_packet = mosq_test.gen_connack(flags=0, rc=0, proto_ver=5)
|
||||
|
||||
connack2_packet = mosq_test.gen_connack(flags=1, rc=0, proto_ver=5)
|
||||
|
||||
props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_SESSION_EXPIRY_INTERVAL, 3)
|
||||
disconnect_packet = mosq_test.gen_disconnect(proto_ver=5, properties=props)
|
||||
|
||||
props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_SESSION_EXPIRY_INTERVAL, 0)
|
||||
disconnect2_packet = mosq_test.gen_disconnect(proto_ver=5, properties=props)
|
||||
|
||||
port = mosq_test.get_port()
|
||||
broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port)
|
||||
|
||||
try:
|
||||
# First connect, clean start is false, we expect a normal connack
|
||||
sock = mosq_test.do_client_connect(connect_packet, connack1_packet, port=port, connack_error="connack 1")
|
||||
# Forceful disconnect
|
||||
sock.close()
|
||||
|
||||
# Immediate second connect, clean start is false, we expect a connack with
|
||||
# previous state
|
||||
sock = mosq_test.do_client_connect(connect_packet, connack2_packet, port=port, connack_error="connack 2")
|
||||
sock.close()
|
||||
|
||||
# Session should expire in one second, so sleep longer
|
||||
time.sleep(2)
|
||||
|
||||
# Third connect, clean start is false, session should have expired so we
|
||||
# expect a normal connack
|
||||
sock = mosq_test.do_client_connect(connect_packet, connack1_packet, port=port, connack_error="connack 3")
|
||||
# Send DISCONNECT with new session expiry, then close
|
||||
sock.send(disconnect_packet)
|
||||
sock.close()
|
||||
|
||||
# Immediate reconnect, clean start is false, we expect a connack with
|
||||
# previous state
|
||||
sock = mosq_test.do_client_connect(connect_packet, connack2_packet, port=port, connack_error="connack 4")
|
||||
# Send DISCONNECT with new session expiry, then close
|
||||
sock.send(disconnect_packet)
|
||||
sock.close()
|
||||
|
||||
# Session should expire in three seconds if it has been updated, sleep for
|
||||
# 2 to check it is updated from 1 second.
|
||||
time.sleep(2)
|
||||
|
||||
# Immediate reconnect, clean start is false, we expect a connack with
|
||||
# previous state
|
||||
sock = mosq_test.do_client_connect(connect_packet, connack2_packet, port=port, connack_error="connack 5")
|
||||
# Send DISCONNECT with new session expiry, then close
|
||||
sock.send(disconnect_packet)
|
||||
sock.close()
|
||||
|
||||
# Session should expire in three seconds, so sleep longer
|
||||
time.sleep(4)
|
||||
# Third connect, clean start is false, session should have expired so we
|
||||
# expect a normal connack
|
||||
sock = mosq_test.do_client_connect(connect_packet, connack1_packet, port=port, connack_error="connack 6")
|
||||
# Send DISCONNECT with 0 session expiry, then close
|
||||
sock.send(disconnect2_packet)
|
||||
sock.close()
|
||||
|
||||
# Immediate reconnect, session should have been removed.
|
||||
sock = mosq_test.do_client_connect(connect_packet, connack1_packet, port=port, connack_error="connack 7")
|
||||
sock.close()
|
||||
rc = 0
|
||||
|
||||
sock.close()
|
||||
finally:
|
||||
broker.terminate()
|
||||
broker.wait()
|
||||
(stdo, stde) = broker.communicate()
|
||||
if rc:
|
||||
print(stde)
|
||||
|
||||
exit(rc)
|
||||
|
Loading…
Reference in New Issue