Skip to content

Commit

Permalink
fix: use request headers for controller throtlling (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiendan committed Sep 18, 2023
1 parent 348c94c commit 3297f91
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
4 changes: 3 additions & 1 deletion packages/backend/src/faucet/faucet.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Body, Controller, Headers, Post } from '@nestjs/common'
import { Body, Controller, Headers, Post, UseGuards } from '@nestjs/common'

import { GetSubnetAssetsDto } from './faucet.dto'
import { FaucetService } from './faucet.service'
import { ThrottlerBehindProxyGuard } from './throttler.guard'

@Controller('faucet')
export class FaucetController {
constructor(private faucetService: FaucetService) {}

@Post('getSubnetAssets')
@UseGuards(ThrottlerBehindProxyGuard)
async getSubnetAssets(
@Body() getSubnetAssetsDto: GetSubnetAssetsDto,
@Headers('traceparent') traceparent: string
Expand Down
11 changes: 11 additions & 0 deletions packages/backend/src/faucet/throttler.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ThrottlerGuard } from '@nestjs/throttler'
import { Injectable } from '@nestjs/common'

@Injectable()
export class ThrottlerBehindProxyGuard extends ThrottlerGuard {
protected getTracker(req: Record<string, any>) {
console.log('IPs from request headers', req.ips)
console.log('IP from request', req.ip)
return req.ips.length ? req.ips[0] : req.ip
}
}
20 changes: 5 additions & 15 deletions packages/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import 'antd/dist/reset.css'
import useTheme from './hooks/useTheme'
import FaucetForm from './components/FaucetForm'
import Content from './components/Content'
// import { SERVICE_NAME } from './tracing'
// import { trace } from '@opentelemetry/api'
import { TracingContext } from './contexts/tracing'
import { SubnetWithId } from './types'
import useRegisteredSubnets from './hooks/useRegisteredSubnets'
Expand Down Expand Up @@ -79,22 +77,14 @@ const App = () => {
[registeredSubnets]
)

const transaction = useMemo(() => {
return apm.startTransaction('root', 'app', { managed: true })
// const tracer = trace.getTracer(SERVICE_NAME)
// return tracer.startSpan('root')
}, [])

// useEffect(function init() {
// return function onPageClosed() {
// console.log('end root')
// transaction?.end()
// }
// }, [])
const apmTransaction = useMemo(
() => apm.startTransaction('root', 'app', { managed: true }),
[]
)

return (
<ThemeProvider theme={theme}>
<TracingContext.Provider value={{ transaction }}>
<TracingContext.Provider value={{ transaction: apmTransaction }}>
<ErrorsContext.Provider value={{ setErrors }}>
<SubnetsContext.Provider
value={{
Expand Down

0 comments on commit 3297f91

Please sign in to comment.