summaryrefslogtreecommitdiff
path: root/tools/server-side/svnpubsub/svnpubsub/client.py
blob: 871a5e925b0bc4045e86b33e3a63cac4cfdf5a49 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# Generic client for SvnPubSub
#
# ### usage...
#
#
# EVENTS
#
#   connected: a connection to the server has been opened (though not
#                 necessarily established)
#   closed:    the connection was closed. reconnect will be attempted.
#   error:     an error closed the connection. reconnect will be attempted.
#   ping:      the server has sent a keepalive
#   stale:     no activity has been seen, so the connection will be closed
#                 and reopened
#

import asyncore
import asynchat
import socket
import functools
import time
import json
try:
  import urlparse
except ImportError:
  import urllib.parse as urlparse

# How long the polling loop should wait for activity before returning.
TIMEOUT = 30.0

# Always delay a bit when trying to reconnect. This is not precise, but sets
# a minimum amount of delay. At the moment, there is no further backoff.
RECONNECT_DELAY = 25.0

# If we don't see anything from the server for this amount time, then we
# will drop and reconnect. The TCP connection may have gone down without
# us noticing it somehow.
STALE_DELAY = 60.0


class SvnpubsubClientException(Exception):
  pass

class Client(asynchat.async_chat):

  def __init__(self, url, commit_callback, event_callback,
               metadata_callback = None):
    asynchat.async_chat.__init__(self)

    self.last_activity = time.time()
    self.ibuffer = []

    self.url = url
    parsed_url = urlparse.urlsplit(url)
    if parsed_url.scheme != 'http':
      raise ValueError("URL scheme must be http: '%s'" % url)
    host = parsed_url.hostname
    port = parsed_url.port
    resource = parsed_url.path
    if parsed_url.query:
      resource += "?%s" % parsed_url.query
    if parsed_url.fragment:
      resource += "#%s" % parsed_url.fragment

    self.event_callback = event_callback

    self.parser = JSONRecordHandler(commit_callback, event_callback,
                                    metadata_callback)

    # Wait for the end of headers. Then we start parsing JSON.
    self.set_terminator(b'\r\n\r\n')
    self.skipping_headers = True

    self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
      self.connect((host, port))
    except:
      self.handle_error()
      return

    self.push(('GET %s HTTP/1.0\r\n\r\n' % resource).encode('ascii'))

  def handle_connect(self):
    self.event_callback('connected', None)

  def handle_close(self):
    self.event_callback('closed', None)
    self.close()

  def handle_error(self):
    self.event_callback('error', None)
    self.close()

  def found_terminator(self):
    if self.skipping_headers:
      self.skipping_headers = False
      # Each JSON record is terminated by a null character
      self.set_terminator(b'\0')
    else:
      record = b"".join(self.ibuffer)
      self.ibuffer = []
      self.parser.feed(record.decode())

  def collect_incoming_data(self, data):
    # Remember the last time we saw activity
    self.last_activity = time.time()

    if not self.skipping_headers:
      self.ibuffer.append(data)


class Notification(object):
  def __init__(self, data):
    self.__dict__.update(data)

class Commit(Notification):
  KIND = 'COMMIT'

class Metadata(Notification):
  KIND = 'METADATA'


class JSONRecordHandler:
  def __init__(self, commit_callback, event_callback, metadata_callback):
    self.commit_callback = commit_callback
    self.event_callback = event_callback
    self.metadata_callback = metadata_callback

  EXPECTED_VERSION = 1

  def feed(self, record):
    obj = json.loads(record)
    if 'svnpubsub' in obj:
      actual_version = obj['svnpubsub'].get('version')
      if actual_version != self.EXPECTED_VERSION:
        raise SvnpubsubClientException(
          "Unknown svnpubsub format: %r != %d"
          % (actual_version, self.EXPECTED_VERSION))
      self.event_callback('version', obj['svnpubsub']['version'])
    elif 'commit' in obj:
      commit = Commit(obj['commit'])
      self.commit_callback(commit)
    elif 'stillalive' in obj:
      self.event_callback('ping', obj['stillalive'])
    elif 'metadata' in obj and self.metadata_callback:
      metadata = Metadata(obj['metadata'])
      self.metadata_callback(metadata)


class MultiClient(object):
  def __init__(self, urls, commit_callback, event_callback,
               metadata_callback = None):
    self.commit_callback = commit_callback
    self.event_callback = event_callback
    self.metadata_callback = metadata_callback

    # No target time, as no work to do
    self.target_time = 0
    self.work_items = [ ]

    for url in urls:
      self._add_channel(url)

  def _reconnect(self, url, event_name, event_arg):
    if event_name == 'closed' or event_name == 'error':
      # Stupid connection closed for some reason. Set up a reconnect. Note
      # that it should have been removed from asyncore.socket_map already.
      self._reconnect_later(url)

    # Call the user's callback now.
    self.event_callback(url, event_name, event_arg)

  def _reconnect_later(self, url):
    # Set up a work item to reconnect in a little while.
    self.work_items.append(url)

    # Only set a target if one has not been set yet. Otherwise, we could
    # create a race condition of continually moving out towards the future
    if not self.target_time:
      self.target_time = time.time() + RECONNECT_DELAY

  def _add_channel(self, url):
    # Simply instantiating the client will install it into the global map
    # for processing in the main event loop.
    if self.metadata_callback:
      Client(url,
             functools.partial(self.commit_callback, url),
             functools.partial(self._reconnect, url),
             functools.partial(self.metadata_callback, url))
    else:
      Client(url,
             functools.partial(self.commit_callback, url),
             functools.partial(self._reconnect, url))

  def _check_stale(self):
    now = time.time()
    for client in asyncore.socket_map.values():
      if client.last_activity + STALE_DELAY < now:
        # Whoops. No activity in a while. Signal this fact, Close the
        # Client, then have it reconnected later on.
        self.event_callback(client.url, 'stale', client.last_activity)

        # This should remove it from .socket_map.
        client.close()

        self._reconnect_later(client.url)

  def _maybe_work(self):
    # If we haven't reach the targetted time, or have no work to do,
    # then fast-path exit
    if time.time() < self.target_time or not self.work_items:
      return

    # We'll take care of all the work items, so no target for future work
    self.target_time = 0

    # Play a little dance just in case work gets added while we're
    # currently working on stuff
    work = self.work_items
    self.work_items = [ ]

    for url in work:
      self._add_channel(url)

  def run_forever(self):
    while True:
      if asyncore.socket_map:
        asyncore.loop(timeout=TIMEOUT, count=1)
      else:
        time.sleep(TIMEOUT)

      self._check_stale()
      self._maybe_work()