-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: socket server improvement (#1021)
feat: cleanup invalid files
- Loading branch information
1 parent
0869b22
commit 1e41819
Showing
19 changed files
with
364 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/sdk-socket-server-next/src/protocol/handleChannelRejected.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Server, Socket } from 'socket.io'; | ||
import { pubClient } from '../api-config'; | ||
import { config } from '../config'; | ||
import { getLogger } from '../logger'; | ||
import { ChannelConfig } from './handleJoinChannel'; | ||
|
||
const logger = getLogger(); | ||
|
||
export type ChannelRejectedParams = { | ||
io: Server; | ||
socket: Socket; | ||
channelId: string; | ||
}; | ||
|
||
/** | ||
* Can only be called by the wallet after a connection attempt. | ||
* | ||
* @param params | ||
* @param callback | ||
*/ | ||
export const handleChannelRejected = async ( | ||
params: ChannelRejectedParams, | ||
callback?: (error: string | null, result?: unknown) => void, | ||
) => { | ||
const { channelId, socket } = params; | ||
|
||
const socketId = socket.id; | ||
const clientIp = socket.request.socket.remoteAddress; | ||
|
||
const channelConfigKey = `channel_config:${channelId}`; | ||
const existingConfig = await pubClient.get(channelConfigKey); | ||
let channelConfig: ChannelConfig | null = existingConfig | ||
? (JSON.parse(existingConfig) as ChannelConfig) | ||
: null; | ||
|
||
if (channelConfig) { | ||
logger.debug( | ||
`[handleChannelRejected] Channel already exists: ${channelId}`, | ||
JSON.stringify(channelConfig), | ||
); | ||
|
||
// ignore if already ready | ||
if (channelConfig.ready) { | ||
logger.warn( | ||
`[handleChannelRejected] received rejected for channel that is already ready: ${channelId}`, | ||
{ | ||
channelId, | ||
socketId, | ||
clientIp, | ||
}, | ||
); | ||
return; | ||
} | ||
|
||
// channel config already exists but keyexchange hasn't happened, so we can just update the existing one as rejected with short ttl. | ||
channelConfig.rejected = true; | ||
channelConfig.updatedAt = Date.now(); | ||
} else { | ||
// this condition can occur if the dapp (ios) was disconnected before the channel config was created | ||
channelConfig = { | ||
clients: { | ||
wallet: socketId, | ||
dapp: '', | ||
}, | ||
rejected: true, | ||
createdAt: Date.now(), | ||
updatedAt: Date.now(), | ||
}; | ||
} | ||
|
||
logger.info( | ||
`[handleChannelRejected] updating channel config for channelId=${channelId}`, | ||
{ | ||
channelId, | ||
socketId, | ||
clientIp, | ||
}, | ||
); | ||
|
||
// Update redis channel config to inform dApp of rejection | ||
await pubClient.setex( | ||
channelConfigKey, | ||
config.rejectedChannelExpiry, | ||
JSON.stringify(channelConfig), | ||
); | ||
|
||
// Also broadcast to dapp if it is connected | ||
socket.broadcast.to(channelId).emit(`rejected-${channelId}`, { channelId }); | ||
|
||
// Edit redis channel config to set to terminated for sdk to pick up | ||
callback?.(null, { success: true }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.