Giter Site home page Giter Site logo

timingwheel's Introduction

TimingWheel

C#版分层时间轮算法,参考 kafka TimingWheel 实现。

NuGet

.NET Standard 2.0+
Install-Package TimingWheel.Net

使用

// 定义一个秒级时间轮
var timer = TimingWheelTimer.Build(TimeSpan.FromSeconds(1), 60);
timer.Start();

// 添加延时任务
var task1 = timer.AddTask(TimeSpan.FromSeconds(5), () =>
{
    Console.WriteLine($"{DateTime.Now}, Task1: 执行");
});
var task2 = timer.AddTask(TimeSpan.FromSeconds(5), () =>
{
    Console.WriteLine($"{DateTime.Now}, Task2: 执行");
    throw new Exception();
});

Console.WriteLine($"{DateTime.Now}, Task1: {task1.TaskStatus}");
Console.WriteLine($"{DateTime.Now}, Task2: {task2.TaskStatus}");

// 等待到期执行
await Task.Delay(TimeSpan.FromSeconds(6));

Console.WriteLine($"{DateTime.Now}, Task1: {task1.TaskStatus}");
Console.WriteLine($"{DateTime.Now}, Task2: {task2.TaskStatus}");

timer.Stop();

// 控制台输出
11/5 星期四 14:48:25, Task1: Wait
11/5 星期四 14:48:25, Task2: Wait
11/5 星期四 14:48:30, Task1: 执行
11/5 星期四 14:48:30, Task2: 执行
11/5 星期四 14:48:31, Task1: Success
11/5 星期四 14:48:31, Task2: Fail

原理

待补充...

更新日志

v1.0.1更新内容:
1、细分任务状态

v1.0.2更新内容:
1、sorry,拼写错误:TimeTaskStaus -> TimeTaskStatus

v1.0.3更新内容:
1、修复时间槽入队时的临界值bug

timingwheel's People

Contributors

linys2333 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.