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 / ValidPixKey.kt
Last active December 8, 2022 19:07
Micronaut Bean Validation does NOT support Custom property paths
package br.com.zup.edu.pix.registra
import io.micronaut.core.annotation.AnnotationValue
import io.micronaut.validation.validator.constraints.ConstraintValidator
import io.micronaut.validation.validator.constraints.ConstraintValidatorContext
import javax.inject.Singleton
import javax.validation.Constraint
import javax.validation.Payload
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.CLASS
@rponte
rponte / HowToUse.java
Last active March 26, 2021 23:11
Decorator simples para cuidar da segurança de forma transparente
public class HowToUse {
public static void main(String[] args) {
MercadoLivreHttpClient client = new MercadoLivreHttpClient();
MercadoLivreHttpClient securedClient = new MercadoLivreSecuredHttpClient(client);
HttpResponse<?> response = securedClient.post(null);
}
}
@rponte
rponte / CreateProposalEndpoint.kt
Last active May 25, 2021 12:34
Micronaut: mixing @transaction with gRPC Endpoint methods
@Singleton
open class CreateProposalEndpoint(@Inject val repository: ProposalRespository) : ProposalGrpcServiceGrpc.ProposalGrpcServiceImplBase() {
@Transactional // starts the current transaction
open override fun create(request: CreateProposalRequest, responseObserver: StreamObserver<CreateProposalResponse>) {
val proposal = repository.save(request.toModel()) // with Hibernate, the INSERT may not be flushed here
// returns to the client without waiting for the commit
responseObserver.onNext(CreateProposalResponse.newBuilder().build())
@rponte
rponte / the-grpc-ecosystem.md
Created March 9, 2021 13:09
gRPC Ecosystem
@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 / BeanValidationConfig.java
Last active December 27, 2021 20:33
Spring Boot: integrating Bean Validation between Spring and Hibernate (supporting DI on custom validations with Hibernate)
@Configuration
public class BeanValidationConfig {
/**
* The idea here is to configure Hibernate to use the same ValidatorFactory used by Spring,
* so that Hibernate will be able to handle custom validations that need to use dependency injection (DI)
*/
@Bean
public HibernatePropertiesCustomizer hibernatePropertiesCustomizer(final Validator validator) {
return new HibernatePropertiesCustomizer() {
@rponte
rponte / a01_ExceptionHandlerInterceptor.kt
Last active January 26, 2023 11:21
Micronaut: Implementing a Micronaut AOP Interceptor for exception handling in gRPC endpoints
package br.com.zup.edu.shared.grpc
import io.grpc.BindableService
import io.grpc.stub.StreamObserver
import io.micronaut.aop.MethodInterceptor
import io.micronaut.aop.MethodInvocationContext
import org.slf4j.LoggerFactory
import javax.inject.Inject
import javax.inject.Singleton
@rponte
rponte / a01_ExceptionHandlerInterceptor.kt
Last active June 29, 2022 00:21
Micronaut: Implementing a gRPC Server Interceptor for exception handling
package br.com.zup.edu.shared.grpc
import io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener
import io.grpc.Metadata
import io.grpc.ServerCall
import io.grpc.ServerCallHandler
import io.grpc.ServerInterceptor
import org.slf4j.LoggerFactory
import javax.inject.Inject
import javax.inject.Singleton
@rponte
rponte / ClientHostResolver.java
Last active April 27, 2021 18:03
SpringBoot: how to obtain the user IP address?
package br.com.zup.edu.nossocartao.propostas.shared.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.stream.Stream;
@Component