Skip to content

Instantly share code, notes, and snippets.

@cunla
Created April 28, 2021 17:11
Show Gist options
  • Save cunla/b698af423684a7b8f47f0f05fcf616b4 to your computer and use it in GitHub Desktop.
Save cunla/b698af423684a7b8f47f0f05fcf616b4 to your computer and use it in GitHub Desktop.
use for CR
package com.backbase.interview.course.controller;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
private static Map<String, List<Long>> requests = new HashMap<>();
@GetMapping("/myapi")
public ResponseEntity<Object> greeting(@RequestParam(name = "client") String clientName) {
Long currentTimestamp = System.currentTimeMillis();
if (!requests.containsKey(clientName)) {
requests.put(clientName, new LinkedList<>());
}
List<Long> clientRequests = requests.get(clientName);
long numRequests = clientRequests.stream()
.filter(x -> x >= currentTimestamp - 10000)
.count();
if (numRequests >= 3) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
clientRequests.removeIf(x -> x < currentTimestamp - 10000);
return ResponseEntity.ok().build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment