Project

General

Profile

Actions

Task #1290

closed

Global io_service and Scheduler

Added by Junxiao Shi over 10 years ago. Updated over 10 years ago.

Status:
Closed
Priority:
Low
Category:
Core
Target version:
Start date:
02/25/2014
Due date:
% Done:

100%

Estimated time:
2.50 h

Description

Create a global instance of io_service and Scheduler.

namespace nfd {
boost::asio::io_service& getGlobalIoService();

namespace scheduler {
struct EventId;
typedef function<void()> Event;
EventId schedule(time::Duration after, Event event);
void cancel(const EventId& eventId);
} // namespace scheduler
using scheduler::EventId; // import to nfd namespace

} // namespace nfd

The global instances can be replaced/cleared/reset during unit testing.


This task is divided to two stages:

Stage 1

  • Create global io_service and Scheduler.
  • nfd::Scheduler class still exists. Its global instance is accessed at nfd::scheduler::getGlobalScheduler().
  • Use the global io_service in main().
  • Forwarder uses the global Scheduler.
  • Unit tests can have their own io_service and Scheduler, if they don't interact with Forwarder.

Stage 2

  • Delete references to io_service and Scheduler in all classes and constructors.
  • Change all scheduler calls to use nfd::scheduler::schedule and nfd::scheduler::cancel.
  • Delete or move away nfd::Scheduler class. Delete nfd::getGlobalScheduler() function.
  • Unit tests also use the global instances, and are provided a way to reset / replace the global io_service and Scheduler.
Actions #1

Updated by Junxiao Shi over 10 years ago

  • Description updated (diff)
  • Status changed from New to In Progress
  • Start date set to 02/25/2014
Actions #2

Updated by Junxiao Shi over 10 years ago

  • Description updated (diff)
  • Status changed from In Progress to Code review
  • % Done changed from 0 to 40
Actions #3

Updated by Junxiao Shi over 10 years ago

  • Estimated time changed from 1.50 h to 2.50 h
Actions #4

Updated by Davide Pesavento over 10 years ago

I think this is a step backwards. Singletons and global variables are usually bad. We should aim at reducing their number, not adding more. The dependency injection pattern should be used instead, because it greatly simplifies unit testability.

Actions #5

Updated by Junxiao Shi over 10 years ago

I'm facing difficulty in cancelling a scheduled event at StrategyInfo subclass's destructor.

The alternate to having a global Scheduler is to have a reference to the Scheduler instance in every StrategyInfo subclass instance that contains a scheduled event. This has a memory cost.

Actions #6

Updated by Junxiao Shi over 10 years ago

  • Status changed from Code review to In Progress
  • Priority changed from High to Low
  • % Done changed from 40 to 50

Stage 1 is merged. Stage 2 will start later.

Actions #7

Updated by Junxiao Shi over 10 years ago

  • Status changed from In Progress to Code review
  • % Done changed from 50 to 60

All test cases can have a fresh global io_service now.

Actions #8

Updated by Junxiao Shi over 10 years ago

  • Status changed from Code review to In Progress
  • Assignee changed from Junxiao Shi to Alex Afanasyev
Actions #9

Updated by Junxiao Shi over 10 years ago

  • Status changed from In Progress to Closed
  • % Done changed from 60 to 100

Already merged.

Actions #10

Updated by Junxiao Shi over 10 years ago

  • Status changed from Closed to Code review

If994e3bd303f0788d8e945351c182bbd4ab5e0b2 deletes deprecated getGlobalScheduler() public API.

Actions #11

Updated by Junxiao Shi over 10 years ago

  • Status changed from Code review to Closed
Actions

Also available in: Atom PDF