From 543f0b4d7d6babd3659c4f95cd851dea8847bffb Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 4 Nov 2019 10:43:19 +0000 Subject: [PATCH] Sending to followers thread --- daemon.py | 32 ++++++++++++++++++++------------ posts.py | 26 +++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/daemon.py b/daemon.py index b8536138..eb448c23 100644 --- a/daemon.py +++ b/daemon.py @@ -34,7 +34,7 @@ from person import removeAccount from person import canRemovePost from posts import outboxMessageCreateWrap from posts import savePostToBox -from posts import sendToFollowers +from posts import sendToFollowersThread from posts import postIsAddressedToPublic from posts import sendToNamedAddresses from posts import createPublicPost @@ -468,17 +468,23 @@ class PubServer(BaseHTTPRequestHandler): createSession(self.server.domain,self.server.port,self.server.useTor) if self.server.debug: print('DEBUG: sending c2s post to followers') - sendToFollowers(self.server.session,self.server.baseDir, \ - self.postToNickname,self.server.domain, \ - self.server.port, \ - self.server.httpPrefix, \ - self.server.federationList, \ - self.server.sendThreads, \ - self.server.postLog, \ - self.server.cachedWebfingers, \ - self.server.personCache, \ - messageJson,self.server.debug, \ - self.server.projectVersion) + followersThread=sendToFollowersThread(self.server.session, \ + self.server.baseDir, \ + self.postToNickname, \ + self.server.domain, \ + self.server.port, \ + self.server.httpPrefix, \ + self.server.federationList, \ + self.server.sendThreads, \ + self.server.postLog, \ + self.server.cachedWebfingers, \ + self.server.personCache, \ + messageJson,self.server.debug, \ + self.server.projectVersion) + self.server.followersThreads.append(followersThread) + # retain up to 10 threads + if len(self.server.followersThreads)>10: + self.server.followersThreads.pop(0) if self.server.debug: print('DEBUG: handle any unfollow requests') outboxUndoFollow(self.server.baseDir,messageJson,self.server.debug) @@ -4449,6 +4455,8 @@ def runDaemon(projectVersion, \ loadTokens(baseDir,httpd.tokens,httpd.tokensLookup) httpd.instanceOnlySkillsSearch=instanceOnlySkillsSearch httpd.acceptedCaps=["inbox:write","objects:read"] + # contains threads used to send posts to followers + httpd.followersThreads=[] if noreply: httpd.acceptedCaps.append('inbox:noreply') if nolike: diff --git a/posts.py b/posts.py index a70099dd..4be5f289 100644 --- a/posts.py +++ b/posts.py @@ -1620,7 +1620,7 @@ def hasSharedInbox(session,httpPrefix: str,domain: str) -> bool: if not wfRequest.get('errors'): return True return False - + def sendToFollowers(session,baseDir: str, \ nickname: str, domain: str, port: int, \ httpPrefix: str,federationList: [], \ @@ -1654,7 +1654,6 @@ def sendToFollowers(session,baseDir: str, \ # for each instance for followerDomain,followerHandles in grouped.items(): - time.sleep(4) if debug: print('DEBUG: follower handles for '+followerDomain) pprint(followerHandles) @@ -1720,7 +1719,28 @@ def sendToFollowers(session,baseDir: str, \ if debug: print('DEBUG: End of sendToFollowers') - + time.sleep(4) + +def sendToFollowersThread(session,baseDir: str, \ + nickname: str, domain: str, port: int, \ + httpPrefix: str,federationList: [], \ + sendThreads: [],postLog: [], \ + cachedWebfingers: {},personCache: {}, \ + postJsonObject: {},debug: bool, \ + projectVersion: str): + """Returns a thread used to send a post to followers + """ + sendThread= \ + threadWithTrace(target=sendToFollowers, \ + args=(session,baseDir, \ + nickname,domain,port, \ + httpPrefix,federationList, \ + sendThreads,postLog, \ + cachedWebfingers,personCache, \ + postJsonObject.copy(),debug, \ + projectVersion),daemon=True) + sendThread.start() + return sendThread def createInbox(session,baseDir: str,nickname: str,domain: str,port: int,httpPrefix: str, \ itemsPerPage: int,headerOnly: bool,ocapAlways: bool,pageNumber=None) -> {}: