File: streaming_failed_actions.rb

package info (click to toggle)
mikutter 3.8.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,544 kB
  • sloc: ruby: 20,548; sh: 99; makefile: 19
file content (105 lines) | stat: -rw-r--r-- 2,978 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- coding: utf-8 -*-
require_relative "basic"

class MikuTwitter::StreamingFailedActions

  attr_reader :last_code, :wait_time, :fail_count

  def initialize(name, plugin)
    @name = name
    @plugin = plugin
    @last_code = '200'.freeze
    @wait_time = @fail_count = 0
  end

  def notify(e)
    if e.respond_to?(:code)
      if e.code != @last_code
        case e.code
        when '200'.freeze
          success
        when '401'.freeze         # unauthorized
          client_bug e
        when '403'.freeze         # forbidden
          client_bug e
        when '404'.freeze         # unknown
          client_bug e
        when '406'.freeze         # not acceptable
          client_bug e
        when '413'.freeze         # too long
          client_bug e
        when '416'.freeze         # range unacceptable
          client_bug e
        when '420'.freeze         # rate limited
          rate_limit e
        when '500'.freeze         # server internal error
          flying_whale e
        when '503'.freeze         # service overloaded
          flying_whale e
        end
      end
      httperror
      @last_code = e.code.freeze
    elsif e.is_a?(Exception) or e.is_a?(Thread)
      tcperror
    end
  end

  # 接続できた時の処理
  # ==== Args
  # [e] レスポンス(Net::HTTPResponse)
  def success
    Plugin.call(:streaming_connection_status_connected,
                @name, @last_code)
    @wait_time = @fail_count = 0
    @last_code = '200'.freeze end

  # こちらの問題が原因でTwitterサーバからエラーが返って来ている場合の処理。
  # ただし、過去には何度もサーバ側の不具合で4xx系のエラーが返って来ていたことが
  # あったのであまり宛てにするべきではない
  # ==== Args
  # [res] レスポンス(Net::HTTPResponse)
  def client_bug(res)
    Plugin.call(:streaming_connection_status_failed,
                @name, get_error_str(res)) end

  # 規制された時の処理
  # ==== Args
  # [res] レスポンス(Net::HTTPResponse)
  def rate_limit(res)
    Plugin.call(:streaming_connection_status_ratelimit,
                @name, get_error_str(res)) end

  # サーバエラー・過負荷時の処理
  # ==== Args
  # [e] レスポンス(Net::HTTPResponse)
  def flying_whale(e)
    Plugin.call(:streaming_connection_status_flying_whale,
                @name, get_error_str(res)) end

  private

  def get_error_str(e)
    result = ""
    result += e.code if e.respond_to? :code
    result += " "+e.body.chomp if e.respond_to? :body
    return e.to_s if result.empty?
    result end

  def tcperror
    @fail_count += 1
    if 1 < @fail_count
      @wait_time += 0.25
      if @wait_time > 16
        @wait_time = 16 end end end

  def httperror
    @fail_count += 1
    if 1 < @fail_count
      if 2 == @fail_count
        @wait_time = 10
      else
        @wait_time *= 2
        if @wait_time > 240
          @wait_time = 240 end end end end
end