Class: Ticketing::Conn

Inherits:
Object
  • Object
show all
Defined in:
lib/ticketing/connection.rb

Overview

One server connection: request queue + batching writer thread + reader, with self-healing reconnect. 서버 연결 하나 — 요청 큐 + 배칭 writer 스레드 + reader, 자가 치유 재접속.

Defined Under Namespace

Classes: Session

Constant Summary collapse

STATUS_DOWN =
0
STATUS_UP =
1
STATUS_DRAINING =
2
REQ_QUEUE =
1024
WRITE_BATCH =
64
BACKOFF_MIN =
0.1
BACKOFF_MAX =
3.2
SHORT_SESSION =

A session shorter than this is a failure (auth/crash loop) — back off. 이보다 짧은 세션은 실패로 간주 — 백오프 적용.

1.0
CONNECT_TIMEOUT =
3
DRAIN_REASONS =
%w[not_active auth_failed no_leader].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addr, index, token, ssl_context, verify_host, topo, logger) ⇒ Conn

Returns a new instance of Conn.



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ticketing/connection.rb', line 81

def initialize(addr, index, token, ssl_context, verify_host, topo, logger)
  @addr = addr
  @index = index
  @token = token
  @ssl_context = ssl_context
  @verify_host = verify_host
  @topo = topo
  @logger = logger
  @status = STATUS_DOWN
  @session = nil
  @ever_connected = false
  Thread.new { run_loop }
end

Instance Attribute Details

#addrObject (readonly)

Returns the value of attribute addr.



79
80
81
# File 'lib/ticketing/connection.rb', line 79

def addr
  @addr
end

Instance Method Details

#request(op, key, frame) ⇒ Object

Send one request; pop the returned queue for the reply (array) or a TicketError. 요청 전송 — 반환 큐를 pop해 응답/오류 수신.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ticketing/connection.rb', line 104

def request(op, key, frame)
  resolver = Queue.new
  session = @session
  if session.nil?
    resolver << TicketError.aborted
    return resolver
  end
  begin
    session.queue.push([op, key, frame, resolver]) # blocking push = backpressure. 백프레셔.
  rescue ClosedQueueError
    resolver << TicketError.aborted
    return resolver
  end
  session.abort_queued if session.closed
  resolver
end

#reviveObject

Give a drained (non-leader) connection another chance. 드레인 연결 부활.



98
99
100
# File 'lib/ticketing/connection.rb', line 98

def revive
  @status = STATUS_UP if @status == STATUS_DRAINING
end

#usable?Boolean

Returns:

  • (Boolean)


95
# File 'lib/ticketing/connection.rb', line 95

def usable? = @status == STATUS_UP