Skip to content

Instantly share code, notes, and snippets.

View OlegDokuka's full-sized avatar
🧑‍💻
Ping me if you wanna sponsor my work

Oleh Dokuka OlegDokuka

🧑‍💻
Ping me if you wanna sponsor my work
View GitHub Profile
@OlegDokuka
OlegDokuka / InstanceOfCheckBenchmark.java
Last active November 28, 2022 12:35
instanceof overhead check
/*
* Copyright (c) 2022 VMware Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
Flux.create(new Consumer<FluxSink<Map<String, Object>>>() {
@Override
public void accept(FluxSink<Map<String, Object>> fluxSink) {
// here we should have our subscription to resource
Socket socket;
try {
socket = IO.socket("https://streamer.cryptocompare.com");
logger.info("[EXTERNAL-SERVICE] Connecting to CryptoCompare.com ...");
@OlegDokuka
OlegDokuka / FluxLimitRate.java
Created August 9, 2019 21:39
Flux Limit Rate Atomic Based impl
/*
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@OlegDokuka
OlegDokuka / FluxLimitRate.java
Last active July 31, 2019 15:18
FluxLimitRate
/*
* Copyright 2015-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@OlegDokuka
OlegDokuka / test
Created February 19, 2019 21:17
test
/*
* Copyright (c) 2017, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
package com.salesforce.rxgrpc;
import com.google.common.collect.Lists;
for (;;) {
long currentDemand = demand.getAcquire(); // (1)
if (currentDemand == Long.MAX_VALUE) { // (2)
return;
}
long adjustedDemand = currentDemand + n;
if (adjustedDemand < 0L) { // (3)
adjustedDemand = Long.MAX_VALUE;
}
if (demand.compareAndSet(currentDemand, adjustedDemand)) { // (4)
new StreamPublisher(...)
.subscribe(new Subscriber() {
public void onSubscribe(Subscription s) {
new Thread(() -> s.request(Long.MAX_VALUE)).start();
new Thread(() -> s.request(Long.MAX_VALUE)).start();
}
...
})
...
@Override
public void request(long n) {
...
if (demand.getAndAdd(n) > 0) {
return;
}
for (...
//---------Thread A---------|-----------------------Thread B--------------------------
if (demand.get() > 0) { // | demand -> 1
// | for(; ... ; executing --> demand.decrementAndGet())
// | demand -> 0
// | for(; executing --> demand.get() > 0 && ...)
// | exiting loop
demand.getAndAdd(n); // | <-- executing
// | demand -> n
return;
}
private class StreamSubscription implements Flow.Subscription {
private final Flow.Subscriber<? super T> subscriber;
private final Iterator<? extends T> iterator;
private final AtomicBoolean isTerminated = new AtomicBoolean(false);
private final AtomicLong demand = new AtomicLong();
private final AtomicReference<Throwable> error = new AtomicReference<>();
StreamSubscription(Flow.Subscriber<? super T> subscriber) {
this.subscriber = subscriber;
Iterator<? extends T> iterator = null;