ztimer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4276cd850421d06becfd20e5f2aac36adffa711d
4
+ data.tar.gz: 1e7de82540217efb028a25b7ac14b49ae185bf62
5
+ SHA512:
6
+ metadata.gz: becb4c8ce8fdcbe33bda649b0d554a9d49866e35e0004fdfc631acb01f47753c4368381874126dc9b4459e01c0ea80e0f44b410be86ed873f2f61c3225b30233
7
+ data.tar.gz: d016de3e26f0a8da5bc5366287c36931b96ad0679d916e64c7e4c580206a5ad0e7e0c5c0f5e596344bdd228cd36dfd5d4ff672a6f01050474aff65c39820aacf
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /*.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at serioja90@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ztimer.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Ztimer
2
+
3
+ **Ztimer** is a Ruby gem that allows to get asynchronous delayed notifications. You can enqueue callbacks to be
4
+ called after some amount of time.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'ztimer'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install ztimer
21
+
22
+ ## Usage
23
+
24
+ All you have to do is to tell **Ztimer** the delay after which your code have to be executed. An example of how to use it
25
+ is illustrated below:
26
+
27
+ ```ruby
28
+ require 'ztimer'
29
+
30
+ delay = 5000 # milliseconds
31
+ Ztimer.after(delay) do
32
+ puts "Doing something useful..."
33
+ end
34
+ ```
35
+
36
+ By default **Ztimer** will run at maximum 20 notifications concurrently, so that if you have 100 notifications to be
37
+ executed at the same time, at maximum 20 of them will run at the same time. This is necessary in order to prevent uncontrolled
38
+ threads spawn when many notifications have to be sent at the same time.
39
+
40
+ Anyway, you can change the concurrency by calling `Ztimer.concurrency = <concurrency>`, where `<concurrency>` is the maximum number
41
+ of `Ztimer` workers allowed to run in parallel (ex: `Ztimer.concurrency = 50`).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/serioja90/ztimer. This project is intended to be a safe,
46
+ welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
47
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ztimer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+
2
+ module Ztimer
3
+ class Slot
4
+ attr_reader :expires_at, :callback
5
+ attr_accessor :started_at, :executed_at
6
+
7
+ def initialize(expires_at, &callback)
8
+ @expires_at = expires_at
9
+ @callback = callback
10
+ @started_at = nil
11
+ @executed_at = nil
12
+ end
13
+
14
+ def <=>(other)
15
+ return @expires_at <=> other.expires_at
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Ztimer
2
+ VERSION = "0.1.0"
3
+ end
data/lib/ztimer.rb ADDED
@@ -0,0 +1,89 @@
1
+ require 'set'
2
+ require 'hitimes'
3
+ require "ztimer/version"
4
+ require "ztimer/slot"
5
+
6
+ module Ztimer
7
+ @concurrency = 20
8
+ @slots = SortedSet.new
9
+ @metric = Hitimes::Metric.new("Notifier")
10
+ @monitor = nil
11
+ @running = 0
12
+ @lock = Mutex.new
13
+ @mutex = Mutex.new
14
+ @queue = Queue.new
15
+
16
+ class << self
17
+ attr_reader :concurrency, :running
18
+
19
+ def after(milliseconds, &callback)
20
+ expires_at = @metric.utc_microseconds + milliseconds * 1000
21
+ slot = Slot.new(expires_at, &callback)
22
+ add(slot)
23
+
24
+ return slot
25
+ end
26
+
27
+ def jobs_count
28
+ return @slots.count
29
+ end
30
+
31
+ def concurrency=(new_value)
32
+ raise ArgumentError.new("Invalid concurrency value: #{new_value}") unless new_value.is_a?(Fixnum) && new_value > 1
33
+ @concurrency = new_value
34
+ end
35
+
36
+ protected
37
+
38
+ def add(slot)
39
+ @mutex.synchronize do
40
+ @slots << slot
41
+ restart_monitor if @slots.first == slot || @monitor.nil? || !@monitor.alive?
42
+ end
43
+ end
44
+
45
+ def restart_monitor
46
+ @monitor.kill if @monitor
47
+
48
+ @monitor = Thread.new do
49
+ loop do
50
+ break if @slots.empty?
51
+
52
+ delay = @slots.first.expires_at - @metric.utc_microseconds
53
+ select(nil, nil, nil, delay / 1_000_000.to_f) if delay > 1 # 1 microsecond of cranularity
54
+
55
+ while @slots.first && (@slots.first.expires_at < @metric.utc_microseconds) do
56
+ @mutex.synchronize do
57
+ slot = @slots.first
58
+ slot.started_at = @metric.utc_microseconds
59
+ execute(slot)
60
+ @slots.delete slot
61
+ end
62
+ end
63
+ end
64
+ end
65
+ @monitor.abort_on_exception = true
66
+ end
67
+
68
+ def execute(slot)
69
+ @queue.push slot
70
+
71
+ @lock.synchronize do
72
+ [@concurrency - @running, @queue.size].min.times do
73
+ @running += 1
74
+ worker = Thread.new do
75
+ begin
76
+ while !@queue.empty? && (slot = @queue.pop(true))
77
+ slot.callback.call(slot) unless slot.callback.nil?
78
+ end
79
+ rescue => e
80
+ STDERR.puts e.inspect + (e.backtrace ? "\n" + e.backtrace.join("\n") : "")
81
+ end
82
+ @lock.synchronize { @running -= 1 }
83
+ end
84
+ worker.abort_on_exception = true
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
data/ztimer.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ztimer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ztimer"
8
+ spec.version = Ztimer::VERSION
9
+ spec.authors = ["Groza Sergiu"]
10
+ spec.email = ["serioja90@gmail.com"]
11
+
12
+ spec.summary = %q{An asyncrhonous timer}
13
+ spec.description = %q{Ruby asyncrhonous timer that allows you to enqueue tasks to be executed asyncrhonously after a delay}
14
+ spec.homepage = "https://github.com/serioja90/ztimer"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_dependency "hitimes", "~> 1.2"
26
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ztimer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Groza Sergiu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: hitimes
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.2'
69
+ description: Ruby asyncrhonous timer that allows you to enqueue tasks to be executed
70
+ asyncrhonously after a delay
71
+ email:
72
+ - serioja90@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/ztimer.rb
87
+ - lib/ztimer/slot.rb
88
+ - lib/ztimer/version.rb
89
+ - ztimer.gemspec
90
+ homepage: https://github.com/serioja90/ztimer
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.6
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: An asyncrhonous timer
114
+ test_files: []
115
+ has_rdoc: