blob: 51ea7e33014d99d496bf396fceeab3cd1e615955 [file] [log] [blame]
Mark Slee89e2bb82007-03-01 00:20:36 +00001#
David Reissea2cba82009-03-30 21:35:00 +00002# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
Mark Slee89e2bb82007-03-01 00:20:36 +000019
David Reisse29995e2008-07-31 20:15:17 +000020import errno
Nobuaki Sukegawa1c8b5cb2016-02-14 22:04:38 +090021import logging
Bryan Duxbury69720412012-01-03 17:32:30 +000022import os
Mark Sleecde2b612006-09-03 21:13:07 +000023import socket
David Reiss73af3b72010-08-30 21:57:07 +000024import sys
Mark Sleecde2b612006-09-03 21:13:07 +000025
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090026from .TTransport import TTransportBase, TTransportException, TServerTransportBase
Bryan Duxbury69720412012-01-03 17:32:30 +000027
Nobuaki Sukegawa1c8b5cb2016-02-14 22:04:38 +090028logger = logging.getLogger(__name__)
29
Bryan Duxbury69720412012-01-03 17:32:30 +000030
David Reisse29995e2008-07-31 20:15:17 +000031class TSocketBase(TTransportBase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090032 def _resolveAddr(self):
33 if self._unix_socket is not None:
34 return [(socket.AF_UNIX, socket.SOCK_STREAM, None, None,
35 self._unix_socket)]
36 else:
37 return socket.getaddrinfo(self.host,
38 self.port,
39 self._socket_family,
40 socket.SOCK_STREAM,
41 0,
max ulidtko9b9567b2020-04-27 16:04:27 +030042 socket.AI_PASSIVE)
Mark Sleecde2b612006-09-03 21:13:07 +000043
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090044 def close(self):
45 if self.handle:
46 self.handle.close()
47 self.handle = None
David Reisse29995e2008-07-31 20:15:17 +000048
Bryan Duxbury69720412012-01-03 17:32:30 +000049
David Reisse29995e2008-07-31 20:15:17 +000050class TSocket(TSocketBase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090051 """Socket implementation of TTransport base."""
Mark Sleecde2b612006-09-03 21:13:07 +000052
Junf6511c92019-02-01 12:07:58 +080053 def __init__(self, host='localhost', port=9090, unix_socket=None,
54 socket_family=socket.AF_UNSPEC,
55 socket_keepalive=False):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090056 """Initialize a TSocket
David Reissc16a8f62007-12-14 23:46:47 +000057
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090058 @param host(str) The host to connect to.
59 @param port(int) The (TCP) port to connect to.
60 @param unix_socket(str) The filename of a unix socket to connect to.
61 (host and port will be ignored.)
62 @param socket_family(int) The socket family to use with this socket.
Junf6511c92019-02-01 12:07:58 +080063 @param socket_keepalive(bool) enable TCP keepalive, default off.
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090064 """
65 self.host = host
66 self.port = port
67 self.handle = None
68 self._unix_socket = unix_socket
69 self._timeout = None
70 self._socket_family = socket_family
Junf6511c92019-02-01 12:07:58 +080071 self._socket_keepalive = socket_keepalive
David Reiss0c90f6f2008-02-06 22:18:40 +000072
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090073 def setHandle(self, h):
74 self.handle = h
Mark Sleec9676562006-09-05 17:34:52 +000075
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090076 def isOpen(self):
Neil Williams01d53f42020-07-07 07:27:29 -070077 if self.handle is None:
78 return False
79
80 # this lets us cheaply see if the other end of the socket is still
81 # connected. if disconnected, we'll get EOF back (expressed as zero
82 # bytes of data) otherwise we'll get one byte or an error indicating
83 # we'd have to block for data.
84 #
85 # note that we're not doing this with socket.MSG_DONTWAIT because 1)
86 # it's linux-specific and 2) gevent-patched sockets hide EAGAIN from us
87 # when timeout is non-zero.
88 original_timeout = self.handle.gettimeout()
89 try:
90 self.handle.settimeout(0)
91 try:
92 peeked_bytes = self.handle.recv(1, socket.MSG_PEEK)
Csaba Ringhoferefe5e022024-08-23 14:08:35 +020093 # the length will be zero if we got EOF (indicating connection closed)
94 if len(peeked_bytes) == 1:
95 return True
Neil Williams01d53f42020-07-07 07:27:29 -070096 except (socket.error, OSError) as exc: # on modern python this is just BlockingIOError
97 if exc.errno in (errno.EWOULDBLOCK, errno.EAGAIN):
98 return True
Michael Smithe3eb9af2022-06-08 17:23:27 -070099 except ValueError:
100 # SSLSocket fails on recv with non-zero flags; fallback to the old behavior
101 return True
Neil Williams01d53f42020-07-07 07:27:29 -0700102 finally:
103 self.handle.settimeout(original_timeout)
104
Csaba Ringhoferefe5e022024-08-23 14:08:35 +0200105 # The caller may assume that after isOpen() returns False, calling close()
106 # is not needed, so it is safer to close the socket here.
107 self.close()
108 return False
Aditya Agarwalf954f972007-02-06 01:26:12 +0000109
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900110 def setTimeout(self, ms):
111 if ms is None:
112 self._timeout = None
113 else:
114 self._timeout = ms / 1000.0
David Reiss0c90f6f2008-02-06 22:18:40 +0000115
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900116 if self.handle is not None:
117 self.handle.settimeout(self._timeout)
Mark Sleecde2b612006-09-03 21:13:07 +0000118
Nobuaki Sukegawa1c8b5cb2016-02-14 22:04:38 +0900119 def _do_open(self, family, socktype):
120 return socket.socket(family, socktype)
121
122 @property
123 def _address(self):
124 return self._unix_socket if self._unix_socket else '%s:%d' % (self.host, self.port)
125
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900126 def open(self):
Nobuaki Sukegawa1c8b5cb2016-02-14 22:04:38 +0900127 if self.handle:
James E. King III3131fe92019-07-02 14:21:05 -0400128 raise TTransportException(type=TTransportException.ALREADY_OPEN, message="already open")
Mark Slee22974602007-07-06 22:20:19 +0000129 try:
Nobuaki Sukegawa1c8b5cb2016-02-14 22:04:38 +0900130 addrs = self._resolveAddr()
James E. King III3131fe92019-07-02 14:21:05 -0400131 except socket.gaierror as gai:
Nobuaki Sukegawa1c8b5cb2016-02-14 22:04:38 +0900132 msg = 'failed to resolve sockaddr for ' + str(self._address)
133 logger.exception(msg)
James E. King III3131fe92019-07-02 14:21:05 -0400134 raise TTransportException(type=TTransportException.NOT_OPEN, message=msg, inner=gai)
Nobuaki Sukegawa1c8b5cb2016-02-14 22:04:38 +0900135 for family, socktype, _, _, sockaddr in addrs:
136 handle = self._do_open(family, socktype)
Junf6511c92019-02-01 12:07:58 +0800137
Nick Witherse0ee2c72022-08-31 16:22:27 +1000138 # TCP keep-alive
Junf6511c92019-02-01 12:07:58 +0800139 if self._socket_keepalive:
Nick Witherse0ee2c72022-08-31 16:22:27 +1000140 handle.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
Junf6511c92019-02-01 12:07:58 +0800141
Nobuaki Sukegawa1c8b5cb2016-02-14 22:04:38 +0900142 handle.settimeout(self._timeout)
143 try:
144 handle.connect(sockaddr)
145 self.handle = handle
146 return
147 except socket.error:
148 handle.close()
149 logger.info('Could not connect to %s', sockaddr, exc_info=True)
150 msg = 'Could not connect to any of %s' % list(map(lambda a: a[4],
151 addrs))
152 logger.error(msg)
James E. King III3131fe92019-07-02 14:21:05 -0400153 raise TTransportException(type=TTransportException.NOT_OPEN, message=msg)
Mark Sleecde2b612006-09-03 21:13:07 +0000154
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900155 def read(self, sz):
156 try:
157 buff = self.handle.recv(sz)
bwangelme0ed4a1d2024-04-15 12:17:40 +0800158 except socket.timeout as e:
159 raise TTransportException(type=TTransportException.TIMED_OUT, message="read timeout", inner=e)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900160 except socket.error as e:
161 if (e.args[0] == errno.ECONNRESET and
162 (sys.platform == 'darwin' or sys.platform.startswith('freebsd'))):
163 # freebsd and Mach don't follow POSIX semantic of recv
164 # and fail with ECONNRESET if peer performed shutdown.
165 # See corresponding comment and code in TSocket::read()
166 # in lib/cpp/src/transport/TSocket.cpp.
167 self.close()
168 # Trigger the check to raise the END_OF_FILE exception below.
169 buff = ''
170 else:
James E. King III3131fe92019-07-02 14:21:05 -0400171 raise TTransportException(message="unexpected exception", inner=e)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900172 if len(buff) == 0:
173 raise TTransportException(type=TTransportException.END_OF_FILE,
174 message='TSocket read 0 bytes')
175 return buff
Mark Sleecde2b612006-09-03 21:13:07 +0000176
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900177 def write(self, buff):
178 if not self.handle:
179 raise TTransportException(type=TTransportException.NOT_OPEN,
180 message='Transport not open')
181 sent = 0
182 have = len(buff)
183 while sent < have:
James E. King III3131fe92019-07-02 14:21:05 -0400184 try:
185 plus = self.handle.send(buff)
186 if plus == 0:
187 raise TTransportException(type=TTransportException.END_OF_FILE,
188 message='TSocket sent 0 bytes')
189 sent += plus
190 buff = buff[plus:]
191 except socket.error as e:
192 raise TTransportException(message="unexpected exception", inner=e)
Mark Sleecde2b612006-09-03 21:13:07 +0000193
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900194 def flush(self):
195 pass
Mark Sleec9676562006-09-05 17:34:52 +0000196
Bryan Duxbury69720412012-01-03 17:32:30 +0000197
David Reissd73255d2009-03-24 22:51:02 +0000198class TServerSocket(TSocketBase, TServerTransportBase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900199 """Socket implementation of TServerTransport base."""
Mark Sleec9676562006-09-05 17:34:52 +0000200
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900201 def __init__(self, host=None, port=9090, unix_socket=None, socket_family=socket.AF_UNSPEC):
202 self.host = host
203 self.port = port
204 self._unix_socket = unix_socket
205 self._socket_family = socket_family
206 self.handle = None
lshgdut7af79c82018-11-21 10:09:42 +0800207 self._backlog = 128
208
209 def setBacklog(self, backlog=None):
210 if not self.handle:
211 self._backlog = backlog
212 else:
213 # We cann't update backlog when it is already listening, since the
214 # handle has been created.
Alexander Shadchin0bc2cb92024-07-21 23:27:26 +0300215 logger.warning('You have to set backlog before listen.')
Mark Slee256bdc42007-11-27 08:42:19 +0000216
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900217 def listen(self):
218 res0 = self._resolveAddr()
219 socket_family = self._socket_family == socket.AF_UNSPEC and socket.AF_INET6 or self._socket_family
220 for res in res0:
221 if res[0] is socket_family or res is res0[-1]:
222 break
Mark Slee22974602007-07-06 22:20:19 +0000223
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900224 # We need remove the old unix socket if the file exists and
225 # nobody is listening on it.
226 if self._unix_socket:
227 tmp = socket.socket(res[0], res[1])
228 try:
229 tmp.connect(res[4])
230 except socket.error as err:
231 eno, message = err.args
232 if eno == errno.ECONNREFUSED:
233 os.unlink(res[4])
David Reisse29995e2008-07-31 20:15:17 +0000234
Ling Liae3e96b2023-03-16 17:34:51 -0700235 self.handle = s = socket.socket(res[0], res[1])
236 if s.family is socket.AF_INET6:
237 s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
238 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
239 if hasattr(s, 'settimeout'):
240 s.settimeout(None)
241 s.bind(res[4])
242 s.listen(self._backlog)
Mark Sleec9676562006-09-05 17:34:52 +0000243
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900244 def accept(self):
245 client, addr = self.handle.accept()
246 result = TSocket()
247 result.setHandle(client)
248 return result