Skip to content

Instantly share code, notes, and snippets.

View rponte's full-sized avatar
🏠
Working from home

Rafael Ponte rponte

🏠
Working from home
View GitHub Profile
@rponte
rponte / CreateNewUserController.java
Last active January 9, 2024 14:37
Designing fault-tolerant and idempotent APIs with HTTP requests mapped to database's transactions at 1:1 model
/**
* This Spring Boot controller was implemented as an example of a simple but robust idempotent REST API that
* leverages the ACID properties of a relational database.
*/
@RestController
public class CreateNewUserController {
@Autowired
private UserRepository repository;
@Autowired
@rponte
rponte / README.md
Last active January 5, 2024 19:19
StackSpot Action: action responsible for creating a Gitlab repository written in Python

Create Repository Gitlab

Creates a new repository (aka project) in a specific Gitlab group.

How to set up this project

This project was developed with Python v3.9, and it also uses Python's Virtualenv tool. So before working on this project we need to set up it.

Inside this project's folder, execute those commands to configure your environment:

@rponte
rponte / HowToUseIt.java
Last active December 30, 2023 14:37
THEORY: Example of a simple Single Thread Pool implementation in Java
public class HowToUseIt {
/**
* Usually we'll have a single instance per client
*/
private static final SingleThreadPool THREAD_POOL = new SingleThreadPool();
public void executeAsync() {
try {
@rponte
rponte / taskqueues.sql
Created April 26, 2020 00:40 — forked from purcell/taskqueues.sql
Easy task queues using PostgreSQL
-- Let's say you have a table full of work:
CREATE TABLE tasks (
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
status TEXT NOT NULL DEFAULT 'pending',
payload JSON NOT NULL, -- or just have meaningful columns!
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
@rponte
rponte / NaiveJavaSynchronizedATMService.java
Last active December 4, 2023 15:07
Spring: You should not mix @transactional with synchronized
@Service
public class NaiveJavaSynchronizedATMService {
@Autowired
private AccountRepository repository;
@Transactional
public synchronized void withdraw(Long accountId, double amount) {
Account account = repository.findById(accountId).orElseThrow(() -> {
@rponte
rponte / cmd_as_windowsservice.bat
Created January 3, 2012 17:56
running cmd.exe as a windows service
-- install cmd.exe as a windows service
sc create CmdAsService binpath= "cmd /K start" type= own type= interact
-- now execute this to run cmd.exe service
sc start CmdAsService
-- you can delete cmd.exe service with this command
sc delete CmdAsService
@rponte
rponte / grpc-load-balancing.md
Last active October 20, 2023 07:29
gRPC Load Balancing may not be that easy

Why gRPC load balancing is so tricky?

At a high-level, we need to understand two points:

  • gRPC is built on HTTP/2, and HTTP/2 is designed to have a single long-lived TCP connection (a sticky and persistent connection);
  • To do gRPC load balancing, we need to shift from connection balancing to request balancing;

Some interesting articles about this subject

@rponte
rponte / CustomExceptionHandler.java
Created October 7, 2021 12:35
Spring Boot: custom and simple exception handler (using the default payload generated by Spring)
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import java.time.LocalDateTime;
import java.util.Map;
@rponte
rponte / ssh_tunnel.sh
Last active September 26, 2023 23:04
Opening a SSH tunnel
# ssh -L <local_port>:<remote_ip>:<remote_port> <user>@<gateway_ip>
ssh -L 1433:192.168.1.4:1433 owner@google.com
# or if you want to run in background without tty
ssh -f -N -T -L 8080:localhost:80 owner@google.com