Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

fix: the Connect relay asks the network a quarter as often and stops logging every answer

My-Name-Is-Jeff6afe5ca5c3b0
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 54b3c7c0..af4c21d0 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -370,7 +370,9 @@ Shared:
pipeline.sh) and the Local Network permission, finds and resolves _spotify-connect._tcp; Spotify's
imports of sendto, sendmsg and recvfrom are rebound, a Connect query the system refused is sent by
unicast to each receiver's port 5353 instead, and each answer is passed to Spotify's socket over
- loopback with recvfrom reporting the receiver as its source. No setting. Tested on the Mac against
+ loopback with recvfrom reporting the receiver as its source. Spotify asks the same question from four
+ sockets, two for each address family, every two seconds or so; it is relayed once every 3 s for each
+ family (on a busy Wi-Fi with 21 receivers, 124 rounds a minute became 31), and a new question goes at once. No setting. Tested on the Mac against
harness/connect/
Native:
diff --git a/tweak/Sources/Shared/Connect/SGConnectRelay.m b/tweak/Sources/Shared/Connect/SGConnectRelay.m
index 0db3c329..4418b5ea 100644
--- a/tweak/Sources/Shared/Connect/SGConnectRelay.m
+++ b/tweak/Sources/Shared/Connect/SGConnectRelay.m
@@ -30,6 +30,11 @@ ssize_t (*SGConnectRealRecvfrom)(int, void *, size_t, int, struct sockaddr *, so
static const uint64_t kMillisecond = NSEC_PER_MSEC;
static const uint64_t kRoundLength = 1200 * kMillisecond, kQuietRoundLength = 700 * kMillisecond, kSendGap = 450 * kMillisecond;
static const uint64_t kRecordLife = 15 * NSEC_PER_SEC;
+// The same question is relayed for an address family at most once in this long. Spotify asks it from four sockets
+// every two seconds or so while it plays, and on a busy Wi-Fi each round brings dozens of answers: with no limit
+// the relay passed about fifty a second, keeping the radio awake for a list that changes rarely. A different
+// question (a device's own record) is never held back.
+static const uint64_t kRepeatGap = 3 * NSEC_PER_SEC;
enum { kMaxRounds = 4, kMaxAddresses = 64, kMaxReplies = 32, kMaxRecords = 64 };
static uint64_t now(void) { return clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW); }
@@ -144,7 +149,9 @@ static void handOver(int spotify, const uint8_t *reply, size_t length, const str
os_unfair_lock_unlock(&sg_lock);
if (sendto(sender, reply, length, 0, (const struct sockaddr *)loopback, loopback->ss_len) >= 0) {
- SGLog(@"connect: relayed %zu bytes from %@", length, SGConnectAddressText((const struct sockaddr *)from));
+ // The first few of a launch only: answers come about fifty a second, and a line each filled the log.
+ static _Atomic int logged;
+ if (atomic_fetch_add(&logged, 1) < 5) SGLog(@"connect: relayed %zu bytes from %@", length, SGConnectAddressText((const struct sockaddr *)from));
return;
}
os_unfair_lock_lock(&sg_lock);
@@ -296,6 +303,39 @@ BOOL SGConnectRelayQuery(int socket, const void *query, size_t length) {
if (!start) return YES;
int family = address.ss_family;
+ // Counted, and said once a minute, so the limit's effect shows in the log.
+ static uint64_t ran, skipped, countedFrom;
+ static NSMutableDictionary<NSData *, NSNumber *> *lastRun;
+ uint64_t time = now();
+ os_unfair_lock_lock(&sg_lock);
+ if (!lastRun) lastRun = [NSMutableDictionary dictionary];
+ // The question for its address family, whatever socket asked it: Spotify sends the same query from two sockets
+ // of each family every two seconds or so, and each started a round of its own to every receiver.
+ NSMutableData *question = [NSMutableData dataWithBytes:&family length:sizeof family];
+ if (length > 2) [question appendBytes:(const uint8_t *)query + 2 length:length - 2];
+ uint64_t was = [lastRun[question] unsignedLongLongValue];
+ BOOL allowed = !was || time - was >= kRepeatGap;
+ if (allowed) {
+ lastRun[question] = @(time);
+ ran++;
+ } else {
+ skipped++;
+ [sg_rounds removeObject:key];
+ }
+ uint64_t saidRan = 0, saidSkipped = 0;
+ BOOL say = NO;
+ if (!countedFrom) countedFrom = time;
+ else if (time - countedFrom >= 60 * NSEC_PER_SEC) {
+ say = YES;
+ saidRan = ran, saidSkipped = skipped;
+ ran = skipped = 0;
+ countedFrom = time;
+ // Questions not asked in the last minute are forgotten.
+ for (NSData *old in lastRun.allKeys) if (time - lastRun[old].unsignedLongLongValue >= 60 * NSEC_PER_SEC) [lastRun removeObjectForKey:old];
+ }
+ os_unfair_lock_unlock(&sg_lock);
+ if (say) SGLog(@"connect: %llu rounds and %llu repeats held back in the last minute", saidRan, saidSkipped);
+ if (!allowed) return YES;
dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{
runRound(socket, family, port, bytes);
os_unfair_lock_lock(&sg_lock);