SubscriberContext state in case of concurrent HTTP Request in spring reactive

I have an HTTP API written in Spring Reactive which fetches user details from the datastore. I am adding the description for the API below.

@Configuration
class Routes {

    @Bean
    fun router() = router {
        POST("/url") { request ->
            handle(request, ::retrieveUser)
        }
    }

    private inline fun <reified T, reified R> handle(
        request: ServerRequest,
        noinline handler: (T) -> Mono<R>
    ): Mono<ServerResponse> = request.bodyToMono(T::class.java)
        .flatMap { rq ->
            /*this will populate context with authorization header 
    Will in case of concurrent request is it possible for context to overwrite the same Authorization header tag */

            val authHeader = request.headers().header(HttpHeaders.AUTHORIZATION).firstOrNull()
            val context = Mono.subscriberContext().map { it.put("Authorization", authHeader ?: "") }
            context.flatMap { handler(rq).subscriberContext(it) }
        }
        .flatMap { ServerResponse.ok().bodyValue(it) }
}

There is a router defined and a POST API for the URL. Then there is a function named handle which fetches the Authorization token from the HTTP request if present and sets it in the spring reactive context with the same key. After that, it calls the handler with the request body passed as an rq argument.

Now sharing the implementation for retrieveUser

override fun retrieveUser(rq: RetrieveUserRequestType): Mono<RetrieveUserResponseType> {
    return retrieveSingleUser(rq.orderReferenceList?.orderReference?.first(), rq)
        .subscriberContext { it.populateContext() }
        .defaultIfEmpty(UserRetrieveHandle.NO_USERS_RESPONSE)
        .map { response ->
            response.withMessageInfo(rq.messageInfo)
                .withMessageStatus(MessageStatusType())
                .also {
                    it.messageInfo.withCreateDateTime(OffsetDateTime.now(ZoneOffset.UTC).toXMLGregorianCalendar())
                }
        }
}

private fun Context.populateContext(): Context {
    val oocid = UUID.randomUUID().toString()
    val requestId = UUID.randomUUID().toString()
    return this.mergeGlobalContext(mapOf(
        OOCID to oocid
    ))
        .mergeLocalContext(mapOf(REQUEST_ID to requestId))
        .put(KEY_OOCID, oocid)
        .put(REQUEST_ID, requestId)
}

here Context.populateContext() will add some random id’s which will be used for logging purpose and to be used in downstream

And for retrieveSingleUser is

fun retrieveSingleUser(): Mono<RetrieveUserResponseType> {
    return Mono.subscriberContext()
        .map { context ->
           //This will fetch authorization header from context 
            val authHeader = context.getOrDefault(HttpHeaders.AUTHORIZATION, "")
           
            RetrieveUserResponseType()
        }
}

retrieveSingleUser is the function which will access the authorization header token from the context for to be passed to downstream.

I have a question: in case of multiple concurrent requests, will the context be unique for each HTTP Request? Another way to frame this is in case of multiple concurrent requests will there be any chance context values for the “Authorization” key be overwritten?

The authorization header will be unique for each request and in my use case, one request should not overwrite the “Authorization” flag for the other. Will this context be shared among the threads or will it be unique for each incoming request?

Thanks in advance. Feel free to ask any question

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật