Skip to main content

Smart Locations

danger

Breaking change: create_smart_location_measurements is removed. It is replaced by two methods, and the refresh and fast_mode parameters are gone with it:

BeforeNow
create_smart_location_measurements(id, refresh: false, config, fast_mode)get_smart_locations(id, config)
create_smart_location_measurements(id, refresh: true, config, fast_mode)update_smart_locations(id, config)
fast_mode: truean empty SmartLocationSearchConfig() — the fast search is now the default
fast_mode: falserecommendedSmartLocationSearchConfig() — the full search

An empty config now means the fast search. It used to measure a list of five with a throughput test; it now answers with one good location and no throughput test. If you build a location list for the user to pick from, pass recommendedSmartLocationSearchConfig().

Step-by-step instructions: Migrating from 34.1.0.

danger

Earlier breaking change: instanceDiscovery().getConfiguration() is removed, and the Smart Locations methods and ping_locations take their configuration as a parameter. See Migrating: configuration is now a parameter for what to call instead. This does not affect sdkManager.getConfiguration(), used below for the network name and id.

Smart Locations are designed to identify the fastest servers tailored for individual customers. The definition of "fast" encompasses a variety of factors, which are detailed in the implementation of the Smart Location Algorithm.

Before integrating, read the measurement integration checklist. It lists the rules an app must follow so that measurements never run through the VPN tunnel and the cache stays valid.

Caching for Smart Locations​

The SDK will internally cache smart locations for 30 days. It will internally use

  • Country
  • Region
  • ISP Name
  • Autonomous System Number (ASN)
  • WiFi SSID or Network Name
  • WiFi BSSID or Network Name

It will therefore rely on the get_user_location() method, to obtain the user's current network parameters. As these network parameters cached as well, it's important to tell the SDK, of the user's network change and of every VPN connect or disconnect, by calling the SDK Manager networkChanged(change) method with the matching NetworkChange.

However, the SDK can't obtain the WiFi SSID or BSSID, so apps pass them with every network switch:

// WiFi SSID and BSSID of the network the device switched to; empty strings on cellular
kapeSDKManager.networkChanged(change: .switched(networkName: "Starbucks", networkId: "aa:bb:cc:dd:ee:ff"))
warning

networkName and networkId are different values. Passing the BSSID as the name leaves the network id empty, and the cache then matches at the ASN level for every network on that ISP — so a result measured on one WiFi is served for the next.

The configuration setters setCurrentNetworkName / setCurrentNetworkId still exist and write the same values, e.g. to set them at start-up before the first network switch is reported.

In a Non-WiFi environment, the network name could the Windows Network Profile Name and the network id could be the mac address of the router. These parameters are optional and just help to further segment the cache results.

Matching cached records ​

The internal Network Cache Manager uses a multi-step logic to match a cached result with the current network of the user.

  1. Try to match Country, Region, ISP, ASN, SSID, BSSID if no results returned continue with 2.
  2. Try to match Country, Region, ISP, ASN, SSID, if no results returned continue with 3.
  3. Try to match Country, Region, ISP, if no results returned continue with 4.
  4. Try to match Country, Region, if not results returned continue with 5.
  5. Try to match Country, if no results returned RETURN NO ENTRY else continue with 6.
  6. If results are returned, use the most recent result, determined by the date and time they’ve been added

has_smart_locations​

Returns whether there are cached, non-expired Smart Location results for the current network, measured under the current plan. This uses exact matching only — no fuzzy fallback. If networkName/networkId are not set in the configuration, matching stops at the ASN level.

This allows the host app to determine whether a Smart Location test needs to be run before showing the server list.

Parameters​

NameTypeDescription
subscription_idStringID of the subscription which has a VPN entitlement, the same one passed to get_smart_locations. Network parameters are read from the configuration.

Returns​

Bool — true if valid cached results exist, false otherwise.


has_preferred_protocol​

Returns whether there are cached, non-expired Preferred Protocol results for the current network.

Parameters​

NameTypeDescription
for_locationString?If provided, checks for that specific location. If nil/null, checks for any cached entry.

Returns​

Bool — true if valid cached results exist, false otherwise.


Measuring and ranking Smart Locations​

Finding the fastest locations is split into two independent steps:

  1. Measure — get_smart_locations(...) serves the raw measurements from the cache, or runs the measurement when nothing is cached, and returns a Vec<ClusterMetrics>. These are pure measurements, with no rating. update_smart_locations(...) runs the measurement unconditionally, for when the user asks for fresh results.
  2. Evaluate — rank_smart_location_measurements(...) scores those measurements and returns a best-first Ranking per use case.
info

All three methods live on the InstanceDiscovery facade (reached via SdkManager.instanceDiscovery()). Evaluation is pure (no network), so you can re-rank cached measurements at any time without running a new measurement.

get_smart_locations​

Returns the Smart Location measurements for the user's current network. This is the method to call whenever your app needs Smart Locations — on launch, before showing the server list, before a "connect to the fastest location".

  • If measurements are cached for the current network, they are returned directly. Nothing is measured and no network request is made.
  • If nothing is cached, the SDK runs the measurement, waits for it, caches the result and returns it. This is the same measurement update_smart_locations runs.

You do not need to check the cache yourself first, and you never need update_smart_locations to "fill" it.

warning

If the user is currently connected to the VPN, a Smart Location measurement cannot be performed. The method then returns what is cached for the user's last known real location, and with nothing cached the nearest locations by distance from that location.

warning

You can't run a Smart Locations measurement multiple times simultaneously, or alongside a speed test, as the measurements would interfere with each other. The SDK prevents this and returns an exception.

Parameters​

NameTypeDescription
subscription_idStringID of the subscription which has a VPN entitlement. You can obtain that subscription id as described in Obtaining a VPN Subscription ID
configSmartLocationSearchConfigHow to measure, should a measurement have to run. An empty record is the fast search; see Choosing the search. Ignored when the result comes from the cache.

The config decides what the measurement is for. There are three ways to fill it:

ConfigThe search stops when…Throughput testTypical durationUse it for
Fast — an empty SmartLocationSearchConfig(). The default.one good location has answerednone1–2 seconds"Connect me to the fastest location"
Recommended — recommendedSmartLocationSearchConfig()five good locations have answereda short download per locationnoticeably longer, up to 30 secondsA list of locations the user picks from
Custom — either of the above with fields changedwhat you setwhat you set—Tuning and diagnostics
// "Connect me now": the fast search, the default.
let metrics = try instanceDiscovery.getSmartLocations(
subscriptionId: id,
config: SmartLocationSearchConfig()
)

// A server list to pick from: the recommended full search.
let metrics = try instanceDiscovery.getSmartLocations(
subscriptionId: id,
config: instanceDiscovery.recommendedSmartLocationSearchConfig()
)

// Custom: start from one of the two and change what you need.
var config = instanceDiscovery.recommendedSmartLocationSearchConfig()
config.searchBudgetMs = 10_000

The fast and the recommended search differ in exactly two fields — minNumberGoodCandidates (1 against 5) and performHttpDownloadTest (off against on). Every field means what it says: a field you leave unset takes the fast value, and nothing overrides a field you set. fastSmartLocationSearchConfig() returns the fast search with every field filled in, if you want to read the values; it behaves exactly like the empty config.

info

The cache holds one entry per network and does not remember which search produced it. A result cached by a fast search is returned to a later get_smart_locations call as it is, whatever config that call passes. If your app shows a location picker, let the user refresh it — a pull-to-refresh that calls update_smart_locations with the recommended config replaces the entry with the full result.

When nothing can be measured​

info

Neither method ever returns an empty list. You get measurements, cached measurements, the nearest locations by distance, or an exception.

A measurement can fail outright — on a network that drops every probe, for example — and while the VPN is connected none can run at all. What you get back then depends on what the SDK already knows:

  1. Something is cached → the cached measurements, unchanged.
  2. Nothing is cached → the locations physically nearest to the user — while connected to the VPN, to the user's last known real location — closest first, at most maxLocationsToReturn of them. Each entry carries a distance and no candidate_phase_ping, download or upload, which is how you tell it apart from a measurement. These entries are not cached, so the next call measures again.
  3. Nothing is cached and the user's position is unknown → the method fails with an InstanceDiscoveryException, so it can be retried.

The same applies when the user's location itself cannot be looked up: nothing is measured, and the method answers from the last known real location — the measurements cached for it, otherwise the locations nearest to it. It fails only when the SDK has never seen a real location.

A measurement you cancel with cancel_ongoing_operation is not a failed one: the call then fails with the abort error (OperationRunnerProcessAborted or InstanceDiscoveryProcessAborted) and returns neither the cached measurements nor the nearest locations.

A distance-only list is safe to pass to rank_smart_location_measurements: the entries come back nearest first, with category Unknown and suitable: false.

When the list looks wrong​

In rare cases Smart Locations are measured or looked up under a location that is not the device's: right after a network switch when the location lookup is slow, when the app has not reported a VPN connect, or when the device moved while connected. The SDK accepts this rather than make the user wait for a location lookup before every result.

The remedy is always the same: a pull-to-refresh that calls update_smart_locations measures again and replaces the cached entry. Offer one wherever you show Smart Locations, and keep reporting network and VPN changes through networkChanged(change) — that is what keeps these cases rare.

update_smart_locations​

Measures Smart Locations afresh and replaces the cached entry for the current network.

warning

Call this only when the user explicitly asks for the cached results to be updated — a pull-to-refresh in the server list, a "test again" button. For everything else use get_smart_locations, which measures by itself when it has to. Calling update_smart_locations on every launch re-measures a network whose results are already known.

  • The cache is only written by a measurement that succeeded. If the measurement fails, the cached entry stays exactly as it was and is what the method returns. With nothing cached, the same fallback as above applies.
  • It does not refresh the user's network parameters by itself. The SDK relies on your app reporting every network change and every VPN connect or disconnect through networkChanged(change); what it was told last is what the measurement is filed under.
  • While the user is connected to the VPN nothing is measured and nothing is written; the cached results are returned, or with nothing cached the nearest locations by distance.

Parameters​

The same as get_smart_locations: subscription_id and config. The fast search is the default here too — pass the recommended config when the refresh feeds a location list.

// Pull-to-refresh in the server list.
let metrics = try instanceDiscovery.updateSmartLocations(
subscriptionId: id,
config: instanceDiscovery.recommendedSmartLocationSearchConfig()
)

Returns​

Both methods return an array of ClusterMetrics upon success or InstanceDiscoveryException otherwise. The cache stores this raw Vec<ClusterMetrics> (not the ranking), so a ranking always reflects the current scoring.

ClusterMetrics​

This is the raw measurement for a single location, with no rating attached. The metrics are gathered from up to 4 different measurements:

  • Physical Distance
  • Ping Measurement (Latency, Jitter, Packet Loss)
  • HTTP Up- and Download Measurements (Bandwidth) — not run by the fast search, which is the default
info

Note: Not all metrics might be available in a result. E.g. if the ping failed, the HTTP metrics will not be provided as well, as the HTTP measurement is based on the endpoint of a location which provided the best ping result. A ping-only run (see ping_locations) only populates ping.

NameTypeDescription
location_idStringUnique ID of the location
distanceDistanceResult (optional)Physical distance between user location and server location (distance_km, same_country)
candidate_phase_pingPingMetrics (optional)Latency, jitter and packet loss from the candidate phase, the selection sweep that probes every candidate endpoint at once. A selection measurement taken under the sweep's own load, aggregated per cluster; idle_ping is the headline latency of the chosen endpoint and is what the scoring reads when present.
downloadHttpTestResultDetails (optional)HTTP download measurement (avg_mbit_per_sec)
uploadHttpTestResultDetails (optional)HTTP upload measurement (avg_mbit_per_sec)
dns_resultBool (optional)Whether DNS resolution succeeded for the location
packet_lossPacketLossMetrics (optional)Result of the dedicated packet-loss phase. Unset when the phase did not run — which is the default for Smart Locations.
loaded_latencyLoadedLatencyMetrics (optional)Latency measured idle versus under load. Unset for a ping-only measurement.

PacketLossMetrics​

Produced by the dedicated packet-loss phase, which sends its own probe train rather than deriving loss from the latency window. It is off by default for Smart Locations and on for the speed test.

NameTypeDescription
loss_percentFloatlost / sent as a percent, matching the unit used elsewhere. The headline figure.
sentUInt32Probes dispatched — the loss denominator.
receivedUInt32Probes that came back.
lostUInt32Probes still outstanding when the drain window closed.
refusedUInt32Probes the host actively rejected (refused / unreachable). That is reachability, not loss, so it is excluded from both numerator and denominator.
local_failuresUInt32Probes that never reached the network because this device ran out of file descriptors, ephemeral ports or socket buffers. Also excluded from the ratio, and reported so that a figure distorted by the probe rate itself is visible rather than silent.
longest_loss_runUInt32Longest run of consecutively lost probes.
burst_run_thresholdUInt32The run length at which bursting stops being explained by chance, at this loss rate. See the hint below.
loss_histogramArray<UInt32>Losses per equal-width slice of the probe sequence, oldest first.
rtt_samples_msArray<UInt32>Round-trip times observed during this phase. Not an idle-latency measurement — the train sends its probes in about a second, so the tail reflects the probe pressure rather than the path. Idle latency is measured separately.
probe_deadline_msUInt32The per-probe deadline used. Over TCP it is a fixed, ping-like 1000 ms; a SYN the kernel retransmitted and the peer answered inside it counts as delivered, so the TCP figure is the effective loss TCP applications see. Over UDP there is no per-probe deadline — a probe is lost when the drain closes — and this reports the phase ceiling that bounds the wait.
info

Read longest_loss_run against burst_run_threshold, and do not substitute a constant of your own. longest_loss_run >= burst_run_threshold means the losses were bursty — they had a common cause, most likely a queue overflowing. Below it they were spread, each probe dropped independently.

The threshold is supplied precisely because it is not a constant. Under independent loss with probability p, the expected number of runs of length r in n probes is about n · pʳ, so the shortest run worth pointing at depends strongly on p: at 0.3 % loss three drops in a row are essentially impossible by chance, while at 12.7 % the expected longest run is already over three and runs of five happen routinely. A fixed threshold reports the same link as bursty on one run and spread on the next.

The distinction is worth showing a user: the same 3 % arriving as thirty scattered drops and as one run of thirty are the same number and different faults. A voice call survives the first and stutters through the second.

LoadedLatencyMetrics​

Latency measured with the link idle versus saturated, and the difference between them. Every field is null when that series produced too few samples to report — never 0, which would be indistinguishable from a perfect measurement.

NameTypeDescription
unloaded_msFloat (optional)Median RTT with the link idle.
during_download_msFloat (optional)Median RTT sampled while the download saturated the link.
during_upload_msFloat (optional)Median RTT sampled while the upload saturated the link.
unloaded_jitter_msFloat (optional)Mean absolute difference between consecutive idle RTTs.
during_download_jitter_msFloat (optional)The same, sampled during the download.
during_upload_jitter_msFloat (optional)The same, sampled during the upload.
download_increase_msFloat (optional)during_download_ms - unloaded_ms. Positive means the download adds delay.
upload_increase_msFloat (optional)during_upload_ms - unloaded_ms.
info

The increase is the field that matters for interactive use, more than the absolute latency: a link whose latency triples while a download runs will stutter in a call regardless of how much bandwidth it has. This is bufferbloat, and it is why a connection that benchmarks well can still feel bad.

rank_smart_location_measurements​

This method scores a set of measurements and returns a best-first Ranking per use case. It is a pure, offline operation.

Parameters​

NameTypeDescription
measurementsArray<MeasuredCandidate>The measurements to rank. For Smart Locations the protocol is irrelevant (the ranking is cross-location), so tag every candidate with PreferredProtocol::None.

Wrap each ClusterMetrics returned by get_smart_locations or update_smart_locations into a MeasuredCandidate:

let metrics = try instanceDiscovery.getSmartLocations(subscriptionId: id, config: SmartLocationSearchConfig())
let candidates = metrics.map {
MeasuredCandidate(protocol: PreferredProtocol.none, metrics: $0, label: $0.locationId)
}
let ranking = try instanceDiscovery.rankSmartLocationMeasurements(measurements: candidates)
let bestServer = ranking.recommended.first // RankedCandidate
let category = bestServer?.category // derived from the continuous score
let score = bestServer?.score // 0–5 float

Returns​

A Ranking.

MeasuredCandidate​

One measurement to rank, tagged with the protocol it was produced with.

NameTypeDescription
protocolPreferredProtocolThe protocol the measurement was produced with. Use PreferredProtocol::None for Smart Locations (cross-location ranking); use the real protocol when comparing protocols at one location (see Speed Test).
metricsClusterMetricsThe raw measurement to be ranked.
labelStringApp-given display name of this measurement, propagated verbatim to the corresponding RankedCandidate and the AI explanation. Purely informational — it never affects scoring.

Ranking​

A best-first ranking per use case. Each field is a Vec<RankedCandidate> sorted best-first.

Each list answers two questions. Its first entry is the winner of that use case. The entries flagged suitable are the candidates fit for that activity — rated Good or better on it. The two are independent: a winner in a weak field is not suitable, and in a strong field several candidates are.

warning

Display each list in the order the SDK returns it — do not re-sort it by score. Candidates whose scores are close are deliberately treated as tied so that the domestic-server preference and the throughput comparison can decide the order, which means the entry at rank 1 may carry a slightly lower score than the one at rank 2. Sorting by score yourself throws all of that away and lets a rounding-level difference pick the winner.

NameTypeDescription
recommendedArray<RankedCandidate>The recommendation order: the best all-rounders first (mean of the five use-case scores). For a protocol comparison one rule applies on top — a Lightway protocol scoring at least 4.5 here and rated Good or better in every use case is placed first; see The recommended protocol. Smart Locations candidates carry no protocol, so the rule never affects them.
gamingArray<RankedCandidate>Best candidates for gaming
streamingArray<RankedCandidate>Best candidates for streaming
browsingArray<RankedCandidate>Best candidates for browsing
downloadsArray<RankedCandidate>Best candidates for downloads
video_confArray<RankedCandidate>Best candidates for video conferencing

RankedCandidate​

One ranked entry. It carries the raw metrics it was ranked from, so you can show the measurement behind a score without a separate lookup.

NameTypeDescription
protocolPreferredProtocolThe protocol this entry was measured with (None for Smart Locations)
location_idStringUnique ID of the ranked location
scoreFloatContinuous quality score, 0–5. This is the measured score; same_country is a separate ranking criterion (see below) and does not inflate the score.
categoryRatingCategoryThe displayed category, derived by rounding the score (and clamped to Excellent).
Excellent = 5,
Good = 4,
Ok = 3,
Degraded = 2,
Poor = 1,
Unknown = 0,
suitableBoolWhether this candidate is fit for the list's use case at all: its category in this list is Good or better. Independent of rank — the first entry of a weak field is the best available choice but not suitable, and in a strong field every entry is. Use it to offer every fit candidate for an activity, not only the winner.
measurement_idUInt32Index of the originating measurement in the input list passed to rank_speedtest_measurements / rank_smart_location_measurements. Identifies this entry in the ranking and in the AI explanation.
labelStringApp-given display name of the originating measurement, propagated verbatim from the input MeasuredCandidate. Purely informational — it never affects scoring.
metricsClusterMetricsThe raw measurement this entry was ranked from
info

Scoring & ranking: scores are continuous 0–5 floats and the category is simply the rounded score. Candidates are then ordered by:

  1. a score band 0.5 wide, so scores within 0.5 are treated as tied;
  2. same_country — within that band, servers in the user's own country rank first;
  3. a narrower score band, 0.25 wide — only scores this close count as noise;
  4. a download band — within 5 % of the fastest download measured in the same call counts as tied;
  5. an upload band — the same, against the fastest upload;
  6. the overall score (the mean across the five use cases);
  7. location_id, then the protocol, for a stable order.

So same_country is a tie-break preference, not a score boost. There are two score bands because they gate different things: the wide one decides how much quality is worth giving up to stay in the user's country, the narrow one decides when raw speed may break a tie at all — speed is unrelated to some scores (gaming does not score bandwidth), so it must not override a real quality difference.

The throughput bands matter most when you compare protocols at one location: there same_country and location_id are identical for every candidate, so download and upload speed are what actually decide. They are banded at 5 % because throughput measurement noise scales with the measured speed — a 0.6 Mbit/s difference on a 272 Mbit/s link is noise and must not outweigh a 120 Mbit/s difference in upload.

Because keys 2 and 4–7 do not depend on the use case, the same candidate wins every list unless the scores genuinely differ by more than 0.25 — you will not see one protocol win each category while another wins recommended. The one deliberate exception is the speed test's Lightway rule (see The recommended protocol), which may place Lightway first in recommended while another protocol wins the use cases.

(For the downloads use case, the score is relative to the fastest link measured in the same call, so a faster server ranks above a slower one instead of both maxing out.)