All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.core.impl.SharedClientHolder Maven / Gradle / Ivy

There is a newer version: 5.0.0.CR1
Show newest version
/*
 * Copyright (c) 2011-2021 Contributors to the Eclipse Foundation
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
 * which is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
 */

package io.vertx.core.impl;

import io.vertx.core.Closeable;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.shareddata.LocalMap;
import io.vertx.core.shareddata.Shareable;

import java.util.function.Function;

class SharedClientHolder implements Shareable {

  static  C createSharedClient(Vertx vertx, String clientKey, String clientName, CloseFuture closeFuture, Function supplier) {
    LocalMap> localMap = vertx.sharedData().getLocalMap(clientKey);
    SharedClientHolder v = localMap.compute(clientName, (key, value) -> {
      if (value == null) {
        Hook hook = new Hook<>(vertx, clientKey, clientName);
        C client = supplier.apply(hook.closeFuture);
        return new SharedClientHolder<>(hook, 1, client);
      } else {
        return new SharedClientHolder<>(value.hook, value.count + 1, value.client);
      }
    });
    C client = v.client;
    closeFuture.add(v.hook);
    return client;
  }

  final Hook hook;
  final int count;
  final C client;

  SharedClientHolder(Hook hook, int count, C client) {
    this.hook = hook;
    this.count = count;
    this.client = client;
  }

  private static class Hook implements Closeable {

    private final Vertx vertx;
    private final CloseFuture closeFuture;
    private final String clientKey;
    private final String clientName;

    private Hook(Vertx vertx, String clientKey, String clientName) {
      this.vertx = vertx;
      this.closeFuture = new CloseFuture();
      this.clientKey = clientKey;
      this.clientName = clientName;
    }

    @Override
    public void close(Promise completion) {
      LocalMap> localMap1 = vertx.sharedData().getLocalMap(clientKey);
      SharedClientHolder res = localMap1.compute(clientName, (key, value) -> {
        if (value == null) {
          return null; // Should never happen unless bug
        } else if (value.count == 1) {
          return null;
        } else {
          return new SharedClientHolder<>(this, value.count - 1, value.client);
        }
      });
      if (res == null) {
        closeFuture.close(completion);
      } else {
        completion.complete();
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy