@@ -131,54 +131,56 @@ class THttpProxy : public NActors::TActorBootstrapped<THttpProxy>, public THttpC
131131 TString addressPart;
132132 TIpPort portPart = 0 ;
133133 CrackAddress (host, addressPart, portPart);
134- if (IsIPv6 (addressPart)) {
135- if (it == Hosts.end ()) {
136- it = Hosts.emplace (host, THostEntry ()).first ;
134+ // TODO(xenoxeno): move to another, possible blocking actor
135+ try {
136+ TNetworkAddress addr (addressPart, portPart);
137+ auto pAddr = addr.Begin ();
138+ while (pAddr != addr.End () && pAddr->ai_family != AF_INET && pAddr->ai_family != AF_INET6) {
139+ ++pAddr;
137140 }
138- it->second .Address = std::make_shared<TSockAddrInet6>(addressPart.data (), portPart);
139- it->second .DeadlineTime = ctx.Now () + HostsTimeToLive;
140- } else if (IsIPv4 (addressPart)) {
141- if (it == Hosts.end ()) {
142- it = Hosts.emplace (host, THostEntry ()).first ;
141+ if (pAddr == addr.End ()) {
142+ ctx.Send (event->Sender , new TEvHttpProxy::TEvResolveHostResponse (" Invalid address family resolved" ));
143+ return ;
143144 }
144- it->second .Address = std::make_shared<TSockAddrInet>(addressPart.data (), portPart);
145- it->second .DeadlineTime = ctx.Now () + HostsTimeToLive;
146- } else {
147- // TODO(xenoxeno): move to another, possible blocking actor
148- try {
149- TNetworkAddress addr (addressPart, portPart);
150- auto pAddr = addr.Begin ();
151- while (pAddr != addr.End () && pAddr->ai_family != AF_INET && pAddr->ai_family != AF_INET6) {
152- ++pAddr;
153- }
154- if (pAddr == addr.End ()) {
155- ctx.Send (event->Sender , new TEvHttpProxy::TEvResolveHostResponse (" Invalid address family resolved" ));
156- return ;
157- }
158- THttpConfig::SocketAddressType address;
159- switch (pAddr->ai_family ) {
160- case AF_INET:
161- address = std::make_shared<TSockAddrInet>();
162- break ;
163- case AF_INET6:
164- address = std::make_shared<TSockAddrInet6>();
165- break ;
166- }
167- if (address) {
168- memcpy (address->SockAddr (), pAddr->ai_addr , pAddr->ai_addrlen );
169- LOG_DEBUG_S (ctx, HttpLog, " Host " << host << " resolved to " << address->ToString ());
170- if (it == Hosts.end ()) {
171- it = Hosts.emplace (host, THostEntry ()).first ;
172- }
173- it->second .Address = address;
174- it->second .DeadlineTime = ctx.Now () + HostsTimeToLive;
145+ THttpConfig::SocketAddressType address;
146+ switch (pAddr->ai_family ) {
147+ case AF_INET:
148+ address = std::make_shared<TSockAddrInet>();
149+ break ;
150+ case AF_INET6:
151+ address = std::make_shared<TSockAddrInet6>();
152+ break ;
153+ }
154+ if (address) {
155+ memcpy (address->SockAddr (), pAddr->ai_addr , pAddr->ai_addrlen );
156+ LOG_DEBUG_S (ctx, HttpLog, " Host " << host << " resolved to " << address->ToString ());
157+ if (it == Hosts.end ()) {
158+ it = Hosts.emplace (host, THostEntry ()).first ;
175159 }
160+ it->second .Address = address;
161+ it->second .DeadlineTime = ctx.Now () + HostsTimeToLive;
176162 }
177- catch (const yexception& e) {
178- ctx.Send (event->Sender , new TEvHttpProxy::TEvResolveHostResponse (e.what ()));
163+ }
164+ catch (const TNetworkResolutionError& e) {
165+ if (it != Hosts.end ()) {
166+ ctx.Send (event->Sender , new TEvHttpProxy::TEvResolveHostResponse (it->first , it->second .Address ));
167+ return ;
168+ } else {
169+ ctx.Send (event->Sender ,
170+ new TEvHttpProxy::TEvResolveHostResponse (
171+ TStringBuilder ()
172+ << " Resolution failed and no stale cached value has been found to fallback.\n "
173+ << " Resolution error: "
174+ << e.what ()
175+ )
176+ );
179177 return ;
180178 }
181179 }
180+ catch (const yexception& e) {
181+ ctx.Send (event->Sender , new TEvHttpProxy::TEvResolveHostResponse (e.what ()));
182+ return ;
183+ }
182184 }
183185 ctx.Send (event->Sender , new TEvHttpProxy::TEvResolveHostResponse (it->first , it->second .Address ));
184186 }
0 commit comments