davidradl commented on code in PR #26901: URL: https://github.com/apache/flink/pull/26901#discussion_r2275892365
########## flink-models/flink-model-deepseek/src/main/java/org/apache/flink/model/deepseek/DeepSeekUtils.java: ########## @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.model.deepseek; + +import org.apache.flink.util.Preconditions; + +import okhttp3.OkHttpClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.time.Duration; +import java.util.concurrent.atomic.AtomicInteger; + +/** Utility class for managing DeepSeek clients. */ +public class DeepSeekUtils { + private static final Logger LOG = LoggerFactory.getLogger(DeepSeekUtils.class); + + private static final Object LOCK = new Object(); + + private static ReferenceValue client; + + public static OkHttpClient createClient(String baseUrl, String apiKey) { + synchronized (LOCK) { + if (client != null) { + LOG.debug("Returning an existing DeepSeek client."); + client.referenceCount.incrementAndGet(); + return client.client; + } + return new OkHttpClient.Builder() + .connectTimeout(Duration.ofSeconds(30)) + .readTimeout(Duration.ofSeconds(30)) + .writeTimeout(Duration.ofSeconds(30)) + .addInterceptor(chain -> { + okhttp3.Request original = chain.request(); + okhttp3.Request request = original.newBuilder() + .header("Authorization", "Bearer " + apiKey) + .header("Content-Type", "application/json") + .method(original.method(), original.body()) + .build(); + return chain.proceed(request); + }) + .build(); + } + } + + + + /** + * Releases a DeepSeek client instance. If the reference count reaches zero, the client is closed + * and removed from the cache. + * + */ + public static void releaseClient() { + synchronized (LOCK) { + Preconditions.checkNotNull( + client, "The creation and release of DeepSeek client does not match."); + int count = client.referenceCount.decrementAndGet(); + if (count == 0) { Review Comment: it this counting correct? We create a client with a reference count of 0, the subsequently we add 1 every time the client is referenced. So we will still have a client on count 0. I think this would work if the first client was created `this.referenceCount = new AtomicInteger(1);` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
