-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f1ea47
commit 707ddfa
Showing
10 changed files
with
135 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Controller, Get, UseGuards } from '@nestjs/common'; | ||
import { HeartbeatService } from './heartbeat.service'; | ||
import { User } from 'src/users/decorator/user.decorator'; | ||
import { AccessTokenGuard } from 'src/auth/guard/bearer-token.guard'; | ||
|
||
@Controller() | ||
export class HeartbeatController { | ||
constructor(private readonly heartbeatsService: HeartbeatService) {} | ||
|
||
@UseGuards(AccessTokenGuard) | ||
@Get('/heartbeat') | ||
heartbeat(@User('id') userId: number) { | ||
this.heartbeatsService.recordHeartbeat(userId); | ||
return { statusCode: 200, message: 'OK' }; | ||
} | ||
} |
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,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { HeartbeatService } from './heartbeat.service'; | ||
import { HeartbeatController } from './heartbeat.controller'; | ||
import { AuthModule } from 'src/auth/auth.module'; | ||
import { UsersModule } from 'src/users/users.module'; | ||
import { RedisService } from 'src/common/redis.service'; | ||
|
||
@Module({ | ||
imports: [AuthModule, UsersModule], | ||
controllers: [HeartbeatController], | ||
providers: [HeartbeatService, RedisService], | ||
}) | ||
export class HeartbeatModule {} |
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,27 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { RedisService } from 'src/common/redis.service'; | ||
|
||
@Injectable() | ||
export class HeartbeatService { | ||
constructor(private redisService: RedisService) {} | ||
|
||
recordHeartbeat(userId: number) { | ||
this.redisService.hset(`${userId}`, 'received_at', `${Date.now()}`); | ||
} | ||
|
||
async startCheckingHeartbeats() { | ||
setInterval(async () => { | ||
const now = Date.now(); | ||
const clients = await this.redisService.getKeys(); | ||
for (const clientId of clients) { | ||
const received_at = await this.redisService.hget( | ||
`${clientId}`, | ||
'received_at', | ||
); | ||
if (now - +received_at > 30000) { | ||
await this.redisService.del(`${clientId}`); | ||
} | ||
} | ||
}, 10000); | ||
} | ||
} |
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