Skip to content

Commit

Permalink
Add async lock for websocket broadcast and change deafult host config
Browse files Browse the repository at this point in the history
  • Loading branch information
MouJieQin committed Nov 10, 2024
1 parent 2637932 commit bfaf23f
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 23 deletions.
3 changes: 1 addition & 2 deletions server/src/bookmarkManager/bookmarkManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ def __initData(self):
self.webpsDir = self.ResourceMap.getWebpsPath(self.varchiveDir)
self.detailsPath = self.ResourceMap.getDetailsPath(self.varchiveDir)
self.bookmarkPath = self.ResourceMap.getBookmarkPath(self.varchiveDir)
print("#############bookmarkPath:", self.bookmarkPath)
if os.path.exists(self.bookmarkPath):
if self.bookmarkPath in BookmarksUsing.keys():
self.bookmarkJson = BookmarksUsing[self.bookmarkPath]
Expand Down Expand Up @@ -212,7 +211,7 @@ def _getPngFilePath(self, webpFilename: str):
return webpFilename + ".png"

def _getWebpPath(self, outputDir: str, startTime: float, endTime: float) -> str:
return outputDir + "/" + str(startTime) + "-" + str(endTime) + ".webp"
return outputDir + "/" + str(startTime) + "-" + str(endTime) + videoEditing.VideoEditing.webpFormat

def _createWebpDirIfNotExist(self):
if not os.path.exists(self.webpsDir):
Expand Down
2 changes: 1 addition & 1 deletion server/src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"maxWebsocketConnections":100
},
"iina": {
"path": "/Users/qinmoujie/QMJgithub/spa/varchive/server/fileManager/IINA.app",
"path": "/Applications/IINA.app",
"varchiveDirURL":"Library/Application Support/com.colliderli.iina/varchive",
"varchiveConfig":"config.plist"
},
Expand Down
6 changes: 4 additions & 2 deletions server/src/infoManager/infoManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async def __genCover(self) -> int:
"endTime": endTime,
}
cover = self.infoJson["cover"]
coverPath = self.webpsDir + "/0.webp"
coverPath = self.webpsDir + "/0" + videoEditing.VideoEditing.webpFormat
videoEditor.genWebp(
startTime,
endTime,
Expand Down Expand Up @@ -318,7 +318,9 @@ async def __genPreview(self) -> int:
self._createWebpDirIfNotExist()
timestamps = [percentage * videoDuration for percentage in percentages]
previewWebpOutputPaths = []
previewWebpCoverPath = self.webpsDir + "/0.webp"
previewWebpCoverPath = (
self.webpsDir + "/0" + videoEditing.VideoEditing.webpFormat
)
cover = self.infoJson["cover"]
for timestamp in timestamps:
startTime = timestamp
Expand Down
4 changes: 3 additions & 1 deletion server/src/videoEditing/videoEditing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@


class VideoEditing:
# .webp .gif
webpFormat=".webp"
def __init__(self, videoPath: str, isNetworkResource: bool):
self.videoPath: str = videoPath
self.isNetworkResource: bool = isNetworkResource
Expand Down Expand Up @@ -85,7 +87,7 @@ def getNetworkResourceExt(self) -> str:
return self.networkResourceExt

def getWebpPath(self, outputDir: str, startTime: float, endTime: float) -> str:
return outputDir + "/" + str(startTime) + "-" + str(endTime) + ".webp"
return outputDir + "/" + str(startTime) + "-" + str(endTime) + self.webpFormat

def getPngFilePath(self, webpFilename: str):
return webpFilename + ".png"
Expand Down
2 changes: 1 addition & 1 deletion server/src/videoStatistics/videoStatistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async def merge(self, vs: VideoStatistics):
)

def __insertClip(self, clipStartTime: int, clipEndTime: int, clipCount: int = 1):
print("[{},{}]".format(clipStartTime, clipEndTime))
# print("[{},{}]".format(clipStartTime, clipEndTime))
if clipEndTime - clipStartTime < self.minClip:
return
startTimePreIndex = 0
Expand Down
38 changes: 24 additions & 14 deletions server/src/webSocketManager/webSocketManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

class WebSocketManager:
sendTextLock = asyncio.Lock()
broadcastToIinasLock = asyncio.Lock()
broadcastToVarchivesLock = asyncio.Lock()

def __init__(self, Config, maxConnection, ResourceMap, SystemRunner):
self.ResourceMap = ResourceMap
Expand Down Expand Up @@ -89,22 +91,30 @@ async def sendTextNoWait(websocket: WebSocket, text: str):
await websocket.send_text(text)

async def broadcastToIinasByMetaPath(self, metaPath: str, text: str):
if metaPath not in self.metaFilenameMapIinaID.keys():
return
for id in self.metaFilenameMapIinaID[metaPath]:
if self.isIinaConnected(id):
await self.sendTextNoWait(self.activeIinaConnections[id], text)
# avoid sending too many message simultaneously, which could damage websocket
await asyncio.sleep(0.01)
try:
async with WebSocketManager.broadcastToIinasLock:
if metaPath not in self.metaFilenameMapIinaID.keys():
return
for id in self.metaFilenameMapIinaID[metaPath]:
if self.isIinaConnected(id):
await self.sendTextNoWait(self.activeIinaConnections[id], text)
# avoid sending too many message simultaneously, which could damage websocket
await asyncio.sleep(0.01)
except Exception as e:
print(f"An error occurred when broadcast to iinas: {e}")

async def broadcastToVarchivesByMetaPath(self, metaPath: str, text: str):
if metaPath not in self.metaFilenameMapVarchiveID.keys():
return
for id in self.metaFilenameMapVarchiveID[metaPath]:
if self.isVarchiveConnected(id):
await self.sendTextNoWait(self.activeVarchiveConnections[id], text)
# avoid sending too many message simultaneously, which could damage websocket
await asyncio.sleep(0.01)
try:
async with WebSocketManager.broadcastToVarchivesLock:
if metaPath not in self.metaFilenameMapVarchiveID.keys():
return
for id in self.metaFilenameMapVarchiveID[metaPath]:
if self.isVarchiveConnected(id):
await self.sendTextNoWait(self.activeVarchiveConnections[id], text)
# avoid sending too many message simultaneously, which could damage websocket
await asyncio.sleep(0.01)
except Exception as e:
print(f"An error occurred when broadcast to varchives: {e}")

async def broadcastToIinasByIinaID(self, ID: int, text: str):
metaPath = self.iinaIDmapMetaPath[ID]
Expand Down
3 changes: 1 addition & 2 deletions spa/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import vue from '@vitejs/plugin-vue'
import {fileURLToPath, URL} from 'node:url'
import {defineConfig} from 'vite'

// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: {'@': fileURLToPath(new URL('./src', import.meta.url))},
},
plugins: [vue()],
server: {host: '0.0.0.0', port: '5999'}
server: {host: 'localhost', port: '5999'}
})

0 comments on commit bfaf23f

Please sign in to comment.