• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

temporalio / sdk-java / #244

10 Apr 2024 08:19PM UTC coverage: 77.465% (-0.08%) from 77.549%
#244

push

github

web-flow
Slot supplier interface & fixed-size implementation (#2014)

https://github.com/temporalio/proposals/blob/master/all-sdk/autotuning.md

286 of 388 new or added lines in 25 files covered. (73.71%)

3 existing lines in 3 files now uncovered.

19116 of 24677 relevant lines covered (77.46%)

0.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.75
/temporal-sdk/src/main/java/io/temporal/worker/tuning/FixedSizeSlotSupplier.java
1
/*
2
 * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3
 *
4
 * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
 *
6
 * Modifications copyright (C) 2017 Uber Technologies, Inc.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this material except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
package io.temporal.worker.tuning;
22

23
import com.google.common.base.Preconditions;
24
import java.util.Optional;
25
import java.util.concurrent.*;
26

27
/**
28
 * This implementation of {@link SlotSupplier} provides a fixed number of slots backed by a
29
 * semaphore, and is the default behavior when a custom supplier is not explicitly specified.
30
 *
31
 * @param <SI> The slot info type for this supplier.
32
 */
33
public class FixedSizeSlotSupplier<SI extends SlotInfo> implements SlotSupplier<SI> {
34
  private final int numSlots;
35
  private final Semaphore executorSlotsSemaphore;
36

37
  public FixedSizeSlotSupplier(int numSlots) {
1✔
38
    Preconditions.checkArgument(numSlots > 0, "FixedSizeSlotSupplier must have at least one slot");
1✔
39
    this.numSlots = numSlots;
1✔
40
    executorSlotsSemaphore = new Semaphore(numSlots);
1✔
41
  }
1✔
42

43
  @Override
44
  public SlotPermit reserveSlot(SlotReserveContext<SI> ctx) throws InterruptedException {
45
    executorSlotsSemaphore.acquire();
1✔
46
    return new SlotPermit();
1✔
47
  }
48

49
  @Override
50
  public Optional<SlotPermit> tryReserveSlot(SlotReserveContext<SI> ctx) {
51
    boolean gotOne = executorSlotsSemaphore.tryAcquire();
1✔
52
    if (gotOne) {
1✔
53
      return Optional.of(new SlotPermit());
1✔
54
    }
55
    return Optional.empty();
1✔
56
  }
57

58
  @Override
59
  public void markSlotUsed(SlotMarkUsedContext<SI> ctx) {}
1✔
60

61
  @Override
62
  public void releaseSlot(SlotReleaseContext<SI> ctx) {
63
    executorSlotsSemaphore.release();
1✔
64
  }
1✔
65

66
  @Override
67
  public int getMaximumSlots() {
68
    return numSlots;
1✔
69
  }
70

71
  @Override
72
  public String toString() {
NEW
73
    return "FixedSizeSlotSupplier{" + "numSlots=" + numSlots + '}';
×
74
  }
75
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc