Skip to content

Commit

Permalink
fix session timeout values
Browse files Browse the repository at this point in the history
Signed-off-by: Yuji Ito <llamerada.jp@gmail.com>
  • Loading branch information
llamerada-jp committed Jun 25, 2023
1 parent 5cf71ca commit e578a21
Show file tree
Hide file tree
Showing 26 changed files with 96 additions and 57 deletions.
3 changes: 2 additions & 1 deletion go/colonio/colonio.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ void cgo_wrap_colonio_logger(colonio_t colonio, const char* message, unsigned in
cgoWrapColonioLogger(colonio, (void*)message, (int)len);
}

colonio_error_t* cgo_colonio_init(colonio_t* colonio, int v, unsigned int u) {
colonio_error_t* cgo_colonio_init(colonio_t* colonio, unsigned int timeout, int v, unsigned int u) {
colonio_config_t config;
colonio_config_set_default(&config);
config.seed_session_timeout_ms = timeout;
config.disable_seed_verification = v == 1 ? true : false;
config.max_user_threads = u;
config.logger_func = cgo_wrap_colonio_logger;
Expand Down
5 changes: 3 additions & 2 deletions go/colonio/colonio_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ typedef void* colonio_t;
typedef void* colonio_value_t;
// colonio
colonio_error_t* cgo_colonio_init(colonio_t* colonio, int v, unsigned int u);
colonio_error_t* cgo_colonio_init(colonio_t* colonio, unsigned int timeout, int v, unsigned int u);
colonio_error_t* cgo_colonio_connect(colonio_t colonio, _GoString_ url, _GoString_ token);
int cgo_colonio_is_connected(colonio_t colonio);
Expand Down Expand Up @@ -116,6 +116,7 @@ func convertError(errC *C.struct_colonio_error_s) error {

func NewConfig() *ColonioConfig {
return &ColonioConfig{
SeedSessionTimeoutMs: 30 * 1000,
DisableSeedVerification: false,
MaxUserThreads: 1,
LoggerFunc: func(s string) {
Expand All @@ -140,7 +141,7 @@ func NewColonio(config *ColonioConfig) (Colonio, error) {
if config.DisableSeedVerification {
v = 1
}
errC := C.cgo_colonio_init(&impl.colonioC, C.int(v), C.uint(config.MaxUserThreads))
errC := C.cgo_colonio_init(&impl.colonioC, C.uint(config.SeedSessionTimeoutMs), C.int(v), C.uint(config.MaxUserThreads))
if errC != nil {
return nil, convertError(errC)
}
Expand Down
2 changes: 2 additions & 0 deletions go/colonio/colonio_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func onResponse(_ js.Value, args []js.Value) interface{} {

func NewConfig() *ColonioConfig {
return &ColonioConfig{
SeedSessionTimeoutMs: 30 * 1000,
DisableSeedVerification: false,
MaxUserThreads: 1,
LoggerFunc: func(s string) {
Expand All @@ -185,6 +186,7 @@ func NewConfig() *ColonioConfig {
func NewColonio(config *ColonioConfig) (Colonio, error) {
impl := &colonioImpl{
colonioJ: helperJ.Call("newColonio",
js.ValueOf(config.SeedSessionTimeoutMs),
js.ValueOf(config.DisableSeedVerification),
js.FuncOf(func(this js.Value, args []js.Value) interface{} {
config.LoggerFunc(args[0].String())
Expand Down
1 change: 1 addition & 0 deletions go/colonio/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
)

type ColonioConfig struct {
SeedSessionTimeoutMs uint
DisableSeedVerification bool
MaxUserThreads uint
LoggerFunc func(string)
Expand Down
11 changes: 5 additions & 6 deletions go/seed/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,14 @@ type ConfigNode struct {
}

type Config struct {
Revision float64 `json:"revision,omitempty"`
KeepAliveTimeout int64 `json:"keepAliveTimeout"`
PollingTimeout int64 `json:"pollingTimeout"`
Node *ConfigNode `json:"node,omitempty"`
SessionTimeout int64 `json:"sessionTimeout"`
PollingTimeout int64 `json:"pollingTimeout"`
Node *ConfigNode `json:"node,omitempty"`
}

func (c *Config) validate() error {
if c.KeepAliveTimeout <= 0 {
return errors.New("Config value of `keepAliveTimeout` must be larger then 0")
if c.SessionTimeout <= 0 {
return errors.New("Config value of `sessionTimeout` must be larger then 0")
}

if c.PollingTimeout <= 0 {
Expand Down
23 changes: 11 additions & 12 deletions go/seed/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ type Seed struct {
// packets to relay
packets []packet

config string
keepAliveTimeout time.Duration
pollingTimeout time.Duration
config string
sessionTimeout time.Duration
pollingTimeout time.Duration

verifier TokenVerifier
}
Expand All @@ -89,20 +89,19 @@ func NewSeed(config *Config, verifier TokenVerifier) (*Seed, error) {
}

nodeConfig := config.Node
nodeConfig.Revision = config.Revision
nodeConfigJS, err := json.Marshal(nodeConfig)
if err != nil {
return nil, err
}

seed := &Seed{
mutex: sync.Mutex{},
nodes: make(map[string]*node),
sessions: make(map[string]string),
config: string(nodeConfigJS),
keepAliveTimeout: time.Duration(config.KeepAliveTimeout) * time.Millisecond,
pollingTimeout: time.Duration(config.PollingTimeout) * time.Millisecond,
verifier: verifier,
mutex: sync.Mutex{},
nodes: make(map[string]*node),
sessions: make(map[string]string),
config: string(nodeConfigJS),
sessionTimeout: time.Duration(config.SessionTimeout) * time.Millisecond,
pollingTimeout: time.Duration(config.PollingTimeout) * time.Millisecond,
verifier: verifier,
}

mux := http.NewServeMux()
Expand Down Expand Up @@ -478,7 +477,7 @@ func (seed *Seed) cleanup() error {
}

// disconnect node if it had pass keep alive timeout
if now.After(node.timestamp.Add(seed.keepAliveTimeout)) {
if now.After(node.timestamp.Add(seed.sessionTimeout)) {
seed.disconnect(nid, true)
}
}
Expand Down
10 changes: 5 additions & 5 deletions go/seed/seed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import (

func generateEmptySeed() *Seed {
return &Seed{
mutex: sync.Mutex{},
nodes: make(map[string]*node),
sessions: make(map[string]string),
keepAliveTimeout: 30 * time.Second,
pollingTimeout: 10 * time.Second,
mutex: sync.Mutex{},
nodes: make(map[string]*node),
sessions: make(map[string]string),
sessionTimeout: 30 * time.Second,
pollingTimeout: 10 * time.Second,
}
}

Expand Down
8 changes: 4 additions & 4 deletions go/test/seed.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"/colonio_go.js": "../../src/js/"
},
"node": {
"revision": 20201121.1,
"coordSystem2D": {
"type": "sphere",
"radius": 6378137
Expand All @@ -27,10 +28,9 @@
},
"path": "/test",
"port": 8080,
"keepAliveTimeout": 30000,
"PollingTimeout": 10000,
"sessionTimeout": 30000,
"pollingTimeout": 10000,
"certFile": "../../localhost.crt",
"keyFile": "../../localhost.key",
"useTcp": true,
"revision": 20201121.1
"useTcp": true
}
1 change: 1 addition & 0 deletions src/colonio/colonio.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ typedef struct colonio_config_s {
bool disable_callback_thread;
bool disable_seed_verification;
unsigned int max_user_threads;
unsigned int seed_session_timeout_ms;
void (*logger_func)(colonio_t, const char*, unsigned int);
} colonio_config_t;

Expand Down
10 changes: 10 additions & 0 deletions src/colonio/colonio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,16 @@ class ColonioConfig {
*/
unsigned int max_user_threads;

/**
* @brief `session_timeout_ms` describes the timeout of session between node with the seed.
*
* After timeout from last translate between the seed, the session infomation will be cleared and will be send
* authenticate packet before other packet to the seed.
*
* default: 30000
*/
unsigned int seed_session_timeout_ms;

/**
* @brief `logger` is for customizing the the log receiver.
*
Expand Down
1 change: 1 addition & 0 deletions src/core/colonio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ ColonioConfig::ColonioConfig() :
disable_callback_thread(false),
disable_seed_verification(false),
max_user_threads(1),
seed_session_timeout_ms(30 * 1000),
logger_func(default_logger_func) {
}

Expand Down
3 changes: 2 additions & 1 deletion src/core/colonio_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,8 @@ void ColonioImpl::allocate_resources() {
coord_system.reset();
command_manager = std::make_unique<CommandManager>(logger, random, *scheduler, local_nid, *this);
network = std::make_unique<Network>(
logger, random, *scheduler, *command_manager, local_nid, *this, local_config.disable_seed_verification);
logger, random, *scheduler, *command_manager, local_nid, *this, local_config.seed_session_timeout_ms,
local_config.disable_seed_verification);
messaging.reset();
}

Expand Down
1 change: 0 additions & 1 deletion src/core/definition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ static const unsigned int FIRST_LINK_RETRY_MAX = 3;
static const int64_t LINK_TRIAL_TIME_MIN = 60000;
static const unsigned int LINKS_MIN = 4;
static const int64_t SEED_CONNECT_INTERVAL = 10000;
static const int64_t SEED_SESSION_TIMEOUT = 10 * 1000; // [msec]
static const uint32_t PACKET_ID_NONE = 0x0;

} // namespace colonio
2 changes: 2 additions & 0 deletions src/core/export_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void colonio_config_set_default(colonio_config_t* config) {
config->disable_callback_thread = false;
config->disable_seed_verification = false;
config->max_user_threads = 1;
config->seed_session_timeout_ms = 30 * 1000;
config->logger_func = nullptr;
}

Expand All @@ -59,6 +60,7 @@ colonio_error_t* colonio_init(colonio_t* c, const colonio_config_t* cf) {
config.disable_callback_thread = cf->disable_callback_thread;
config.disable_seed_verification = cf->disable_seed_verification;
config.max_user_threads = cf->max_user_threads;
config.seed_session_timeout_ms = cf->seed_session_timeout_ms;
if (cf->logger_func != nullptr) {
auto f = cf->logger_func;
config.logger_func = [c, f](Colonio& _, const std::string& json) {
Expand Down
9 changes: 6 additions & 3 deletions src/core/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ namespace colonio {
NetworkDelegate::~NetworkDelegate() {
}

Network::Network(Logger& l, Random& r, Scheduler& s, CommandManager& c, const NodeID& n, NetworkDelegate& d, bool v) :
Network::Network(
Logger& l, Random& r, Scheduler& s, CommandManager& c, const NodeID& n, NetworkDelegate& d, unsigned int timeout,
bool v) :
SEED_SESSION_TIMEOUT(timeout),
DISABLE_SEED_VERIFICATION(v),
logger(l),
random(r),
Expand Down Expand Up @@ -60,8 +63,8 @@ void Network::connect(

enforce_online = true;

seed_accessor =
std::make_unique<SeedAccessor>(logger, scheduler, local_nid, *this, url, token, DISABLE_SEED_VERIFICATION);
seed_accessor = std::make_unique<SeedAccessor>(
logger, scheduler, local_nid, *this, url, token, SEED_SESSION_TIMEOUT, DISABLE_SEED_VERIFICATION);
node_accessor = std::make_unique<NodeAccessor>(logger, scheduler, command_manager, local_nid, *this);

scheduler.add_task(this, std::bind(&Network::update_accessor_state, this));
Expand Down
5 changes: 4 additions & 1 deletion src/core/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class NetworkDelegate {

class Network : public NodeAccessorDelegate, public RoutingDelegate, public SeedAccessorDelegate {
public:
Network(Logger& l, Random& r, Scheduler& s, CommandManager& c, const NodeID& n, NetworkDelegate&, bool v);
Network(
Logger& l, Random& r, Scheduler& s, CommandManager& c, const NodeID& n, NetworkDelegate&, unsigned int timeout,
bool v);
virtual ~Network();

void connect(
Expand All @@ -59,6 +61,7 @@ class Network : public NodeAccessorDelegate, public RoutingDelegate, public Seed
const NodeID& get_relay_nid_2d(const Coordinate& position);

private:
const unsigned int SEED_SESSION_TIMEOUT;
const bool DISABLE_SEED_VERIFICATION;

Logger& logger;
Expand Down
5 changes: 3 additions & 2 deletions src/core/seed_accessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ SeedAccessorDelegate::~SeedAccessorDelegate() {

SeedAccessor::SeedAccessor(
Logger& l, Scheduler& s, const NodeID& n, SeedAccessorDelegate& d, const std::string& u, const std::string& t,
bool v) :
unsigned int timeout, bool v) :
logger(l),
scheduler(s),
local_nid(n),
delegate(d),
token(t),
SESSION_TIMEOUT(timeout),
polling_flag(false),
running_auth(false),
is_online(false),
Expand Down Expand Up @@ -130,7 +131,7 @@ void SeedAccessor::trigger() {

// disconnect when polling is off and waiting queue is empty
if (!polling_flag && waiting.empty() && last_send_time != 0) {
if (last_send_time + SEED_SESSION_TIMEOUT < Utils::get_current_msec() && !session.empty()) {
if (last_send_time + SESSION_TIMEOUT < Utils::get_current_msec() && !session.empty()) {
disconnect();
}
return;
Expand Down
4 changes: 3 additions & 1 deletion src/core/seed_accessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class SeedAccessor {
public:
SeedAccessor(
Logger& l, Scheduler& s, const NodeID& n, SeedAccessorDelegate& d, const std::string& u, const std::string& t,
bool v);
unsigned int timeout, bool v);
virtual ~SeedAccessor();
SeedAccessor(const SeedAccessor&) = delete;
SeedAccessor& operator=(const SeedAccessor&) = delete;
Expand All @@ -82,6 +82,8 @@ class SeedAccessor {
SeedAccessorDelegate& delegate;

const std::string token;
const unsigned int SESSION_TIMEOUT;

/** Connection to the server */
std::unique_ptr<SeedLink> link;
// session id. it is empty if session disabled
Expand Down
3 changes: 2 additions & 1 deletion src/js/colonio_go.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/js/colonio_go.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ class ColonioGo {
this.mod = mod;
}

newColonio(v: boolean, logger: (log: string) => void): Colonio {
newColonio(timeout: number, v: boolean, logger: (log: string) => void): Colonio {
let config = new this.mod.ColonioConfig();
config.seedSessionTimeoutMs = timeout;
config.disableSeedVerification = v;
config.loggerFuncRaw = (_: Colonio, log: string): void => {
logger(log);
Expand Down
1 change: 1 addition & 0 deletions src/js/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ declare class Value {
write(valueC?: number): number;
}
declare class ColonioConfig {
seedSessionTimeoutMs: number;
disableSeedVerification: boolean;
loggerFuncRaw: (c: Colonio, json: string) => void;
loggerFunc: (c: Colonio, log: LogEntry) => void;
Expand Down
4 changes: 3 additions & 1 deletion src/js/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,13 @@ class Value {
}

class ColonioConfig {
seedSessionTimeoutMs: number;
disableSeedVerification: boolean;
loggerFuncRaw: (c: Colonio, json: string) => void;
loggerFunc: (c: Colonio, log: LogEntry) => void;

constructor() {
this.seedSessionTimeoutMs = 30 * 1000;
this.disableSeedVerification = false;

this.loggerFuncRaw = (c: Colonio, json: string): void => {
Expand Down Expand Up @@ -669,7 +671,7 @@ class Colonio {

constructor(config: ColonioConfig) {
// init
this._colonio = ccall("js_init", "number", ["boolean"], [config.disableSeedVerification]);
this._colonio = ccall("js_init", "number", ["number", "boolean"], [config.seedSessionTimeoutMs, config.disableSeedVerification]);
colonioMap.set(this._colonio, this);

// logger
Expand Down
5 changes: 3 additions & 2 deletions src/js/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ EMSCRIPTEN_KEEPALIVE bool js_error_get_line(PTR_T err);
EMSCRIPTEN_KEEPALIVE PTR_T js_error_get_file(PTR_T err);
EMSCRIPTEN_KEEPALIVE int js_error_get_file_length(PTR_T err);
// colonio
EMSCRIPTEN_KEEPALIVE COLONIO_T js_init(bool disable_seed_verification);
EMSCRIPTEN_KEEPALIVE COLONIO_T js_init(unsigned int seed_session_timeout_ms, bool disable_seed_verification);
EMSCRIPTEN_KEEPALIVE void js_connect(
COLONIO_T c, PTR_T url, unsigned int url_siz, PTR_T token, unsigned int token_siz, PTR_T on_success,
PTR_T on_failure);
Expand Down Expand Up @@ -204,10 +204,11 @@ int js_error_get_file_length(PTR_T err) {
return e->file_siz;
}

COLONIO_T js_init(bool disable_seed_verification) {
COLONIO_T js_init(unsigned int seed_session_timeout_ms, bool disable_seed_verification) {
colonio_config_t config;
colonio_config_set_default(&config);
config.disable_callback_thread = true;
config.seed_session_timeout_ms = seed_session_timeout_ms;
config.disable_seed_verification = disable_seed_verification;
config.logger_func = logger_func;

Expand Down
Loading

0 comments on commit e578a21

Please sign in to comment.