Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix try_flush_interval < 1 and queued_chunk_flush_interval < 1 to work #568

Merged
merged 1 commit into from
Apr 19, 2015

Conversation

sonots
Copy link
Member

@sonots sonots commented Mar 27, 2015

We were supposed to support try_flush_interval < 1 and queue_chunk_flush_interval < 1, but the interval to call try_flush was still minimum 1 sec because Engine.now is Time.now.to_i.

Illustration: The problem of the old logic:

        until @finish
          time = Engine.now

          if @next_time <= time # (4) have to wait to be 1427488240.1 <= 1427488241
            @mutex.unlock
            begin
              @next_time = @output.try_flush # (1) 1427488240.1
            ensure
              @mutex.lock
            end
            next_wait = @next_time - Engine.now # (2) 0.1
          else
            next_wait = @next_time - time
          end

          # (3) will wait 0.1 sec
          cond_wait(next_wait) if next_wait > 0
        end

@next_time can be like 1427488240.1 when queued_chunk_flush_interval 0.1.
But, time exceeds the @next_time only when it became 1427488241 (1 sec passed) because Engine.now is integer.

@sonots
Copy link
Member Author

sonots commented Mar 27, 2015

cc: @shyouhei

@@ -98,7 +98,7 @@ class OutputThread
def initialize(output)
@output = output
@finish = false
@next_time = Engine.now + 1.0
@next_time = Time.now.to_f + 1.0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about just directly use Time.now + 1.0. Time objects can be compared or subtracted as-is.

irb(main):001:0> next_time = Time.now + 1.0
=> 2015-03-28 11:24:03 +0900
irb(main):002:0> next_wait = next_time - Time.now
=> 0.719691

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

float + float is faster than time + float, and time < float results in error

irb(main):001:0> Time.now < Time.now.to_f
ArgumentError: comparison of Time with 1427512006.7472174 failed
        from (irb):1:in `<'

but I think we can change it to time object here. let me try.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made change like this to use time object > https://gist.github.com/sonots/3cd09b3022460ec1aac8

But, test did not pass. I felt it makes code complicated.
Also, note that float + float is faster than time + float.

My opinion is to use float rather than time object. Any opinions? > ALL

PS. Test passed with this additional change, though > https://gist.github.com/sonots/3cd09b3022460ec1aac8#comment-1422381

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I now feel that changing to Time objects here is kinda overkill to this original issue. It is a good thing to have, but we can live without them.

@sonots
Copy link
Member Author

sonots commented Mar 29, 2015

@repeatedly ping

@shyouhei
Copy link

👍 LGTM

@repeatedly repeatedly self-assigned this Apr 5, 2015
@sonots
Copy link
Member Author

sonots commented Apr 17, 2015

I am going to merge this if there is no objection because this is critical for us.

sonots added a commit that referenced this pull request Apr 19, 2015
fix try_flush_interval < 1 and queued_chunk_flush_interval < 1 to work
@sonots sonots merged commit 3149b38 into master Apr 19, 2015
@sonots sonots deleted the fix_try_flush_interval branch April 19, 2015 05:05
@sonots
Copy link
Member Author

sonots commented Apr 19, 2015

Merged!

sonots added a commit that referenced this pull request Apr 19, 2015
fix try_flush_interval < 1 and queued_chunk_flush_interval < 1 to work
@sonots
Copy link
Member Author

sonots commented Apr 19, 2015

cherry-picked to v0.10

@repeatedly
Copy link
Member

I hope code itself is no problem.
For safety, adding new method like Engine.now is better because refactoring Time.now.to_f is hard for sub-second support.

@shyouhei
Copy link

Thank you.

In case you plan to refactor, I recommend you to just use Time.now directly as I wrote earlier. That should suffice every needs, if not that is a ruby's design flaw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants