master
1#***************************************************************************
2# _ _ ____ _
3# Project ___| | | | _ \| |
4# / __| | | | |_) | |
5# | (__| |_| | _ <| |___
6# \___|\___/|_| \_\_____|
7#
8# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9#
10# This software is licensed as described in the file COPYING, which
11# you should have received as part of this distribution. The terms
12# are also available at https://curl.se/docs/copyright.html.
13#
14# You may opt to use, copy, modify, merge, publish, distribute and/or sell
15# copies of the Software, and permit persons to whom the Software is
16# furnished to do so, under the terms of the COPYING file.
17#
18# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19# KIND, either express or implied.
20#
21# SPDX-License-Identifier: curl
22#
23###########################################################################
24#
25import logging
26import os
27import socket
28import subprocess
29import time
30from datetime import datetime, timedelta, timezone
31from typing import Dict, List, Optional
33from . import CurlClient
34from .env import Env
35from .ports import alloc_ports_and_do
37log = logging.getLogger(__name__)
40class Dnsd:
42 def __init__(self, env: Env):
43 self.env = env
44 self._cmd = os.path.join(env.build_dir, 'tests/server/servers')
45 self._port = 0
46 self.name = 'dnsd'
47 self._port_skey = 'dnsd'
48 self._port_specs = {
49 'dnsd': socket.SOCK_DGRAM,
50 }
51 self._dnsd_dir = os.path.join(env.gen_dir, self.name)
52 self._log_dir = self._dnsd_dir
53 self._lock_dir = os.path.join(self._dnsd_dir, 'lock')
54 self._tmp_dir = os.path.join(self._dnsd_dir, 'tmp')
55 self._log_file = os.path.join(self._log_dir, 'dnsd.log')
56 self._conf_file = os.path.join(self._log_dir, 'dnsd.cmd')
57 self._pid_file = os.path.join(self._log_dir, 'dnsd.pid')
58 self._error_log = os.path.join(self._log_dir, 'dnsd.err.log')
59 self._error_fd = None
60 self._process = None
62 self.clear_logs()
64 @property
65 def port(self) -> int:
66 return self._port
68 def close_log(self):
69 if self._error_fd:
70 self._error_fd.close()
71 self._error_fd = None
73 def clear_logs(self):
74 self._rmf(self._log_file)
75 self._rmf(self._error_log)
77 def exists(self):
78 return os.path.exists(self._cmd)
80 def is_running(self):
81 if self._process:
82 self._process.poll()
83 return self._process.returncode is None
84 return False
86 def start_if_needed(self):
87 if not self.is_running():
88 return self.start()
89 return True
91 def stop(self, wait_dead=True):
92 result = True
93 if self._process:
94 self._process.terminate()
95 self._process.wait(timeout=2)
96 self._process = None
97 result = self.wait_dead(timeout=timedelta(seconds=Env.SERVER_TIMEOUT))
98 self.close_log()
99 return result
101 def restart(self):
102 self.stop()
103 return self.start()
105 def initial_start(self):
106 self._mkpath(self._lock_dir)
107 self._mkpath(self._tmp_dir)
109 def startup(ports: Dict[str, int]) -> bool:
110 self._port = ports[self._port_skey]
111 if self.start():
112 self.env.update_ports(ports)
113 return True
114 self.stop()
115 self._port = 0
116 return False
118 return alloc_ports_and_do(self._port_specs, startup,
119 self.env.gen_root, max_tries=3)
121 def start(self, wait_live=True):
122 assert self._port > 0
123 if self._process:
124 self.stop()
125 self.set_answers() # empty answers by default
126 self._rmf(self._log_file)
127 args = [
128 self._cmd, 'dnsd',
129 '--port', str(self._port),
130 '--logdir', f'{self._log_dir}',
131 '--logfile', f'{self._log_file}',
132 '--pidfile', f'{self._pid_file}',
133 ]
134 self._error_fd = open(self._error_log, 'a') # noqa: SIM115
135 self._process = subprocess.Popen(args=args, stderr=self._error_fd)
136 if self._process.returncode is not None:
137 return False
138 return self.wait_live(timeout=timedelta(seconds=Env.SERVER_TIMEOUT))
140 def wait_dead(self, timeout: timedelta):
141 curl = CurlClient(env=self.env, run_dir=self._tmp_dir)
142 try_until = datetime.now(timezone.utc) + timeout
143 while datetime.now(timezone.utc) < try_until:
144 r = curl.http_get(url=f'http://127.0.0.1:{self._port}/')
145 if r.exit_code != 0:
146 return True
147 time.sleep(.1)
148 log.debug(f"Server still responding after {timeout}")
149 return False
151 def wait_live(self, timeout: timedelta):
152 curl = CurlClient(env=self.env, run_dir=self._tmp_dir,
153 timeout=timeout.total_seconds())
154 try_until = datetime.now(timezone.utc) + timeout
155 while datetime.now(timezone.utc) < try_until:
156 r = curl.http_get(url=f'http://127.0.0.1:{self._port}/')
157 if r.exit_code == 0:
158 return True
159 time.sleep(.1)
160 log.error(f"Server still not responding after {timeout}")
161 return False
163 def _rmf(self, path):
164 if os.path.exists(path):
165 os.remove(path)
167 def _mkpath(self, path):
168 if not os.path.exists(path):
169 os.makedirs(path)
171 def set_answers(self, addr_a: Optional[List[str]] = None,
172 addr_aaaa: Optional[List[str]] = None,
173 https: Optional[List[str]] = None,
174 delay_a_ms: int = 0,
175 delay_aaaa_ms: int = 0,
176 delay_https_ms: int = 0,
177 rcode_a: int = 0,
178 rcode_aaaa: int = 0):
179 conf = []
180 if addr_a:
181 conf.extend([f'A: {addr}' for addr in addr_a])
182 if addr_aaaa:
183 conf.extend([f'AAAA: {addr}' for addr in addr_aaaa])
184 if https:
185 conf.extend([f'HTTPS: {hline}' for hline in https])
186 if delay_a_ms:
187 conf.append(f'Delay-A: {delay_a_ms}')
188 if delay_aaaa_ms:
189 conf.append(f'Delay-AAAA: {delay_aaaa_ms}')
190 if delay_https_ms:
191 conf.append(f'Delay-HTTPS: {delay_https_ms}')
192 if rcode_a:
193 conf.append(f'Rcode-A: {rcode_a}')
194 if rcode_aaaa:
195 conf.append(f'Rcode-AAAA: {rcode_aaaa}')
196 conf.append('\n')
197 with open(self._conf_file, 'w') as fd:
198 fd.write("\n".join(conf))
Diagnostics4 git calls · 130 ms
CallStatusSizeTime0130 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2002.1 KiB124 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=124 git.response.bytes=2173 git.response.cache=MISS git.response.duration_ms=124 git.fetch.wants=4411b07391ae2b9c87c1ce58ee980ee43ae660a3 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2173 git.pkt.payload.max=2152 git.sideband.band1.bytes=2151 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e2d8682ace14c2
- Request
- d0473210…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format