Skip to content

Commit 506c8e7

Browse files
authored
Add lazy global snippet (GoogleCloudPlatform#1611)
1 parent 159cf1d commit 506c8e7

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START functions_tips_lazy_globals]
18+
// [START run_tips_global_lazy]
19+
20+
import java.io.IOException;
21+
import java.io.PrintWriter;
22+
import java.util.Arrays;
23+
import javax.servlet.http.HttpServletRequest;
24+
import javax.servlet.http.HttpServletResponse;
25+
26+
public class Lazy {
27+
// Always initialized (at cold-start)
28+
// Warning: Class variables used in Servlet classes must be thread-safe,
29+
// or else might introduce race conditions in your code.
30+
private static final int nonLazyGlobal = fileWideComputation();
31+
// Declared at cold-start, but only initialized if/when the function executes
32+
private static Integer lazyGlobal = null;
33+
34+
public void lazyGlobal(HttpServletRequest request, HttpServletResponse response)
35+
throws IOException {
36+
// This value is initialized only if (and when) the function is called
37+
if (lazyGlobal == null) {
38+
lazyGlobal = functionSpecificComputation();
39+
}
40+
41+
PrintWriter writer = response.getWriter();
42+
writer.write(String.format("Lazy global: %s; non-lazy global: %s", lazyGlobal, nonLazyGlobal));
43+
}
44+
45+
private static int functionSpecificComputation() {
46+
int[] numbers = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
47+
return Arrays.stream(numbers).sum();
48+
}
49+
50+
private static int fileWideComputation() {
51+
int[] numbers = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
52+
return Arrays.stream(numbers).reduce((t, x) -> t * x).getAsInt();
53+
}
54+
}
55+
// [END run_tips_global_lazy]
56+
// [END functions_tips_lazy_globals]

functions/snippets/src/main/java/Scopes.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
// [START functions_tips_scopes]
18+
// [START run_tips_global_scope]
1819

1920
import java.io.IOException;
2021
import java.io.PrintWriter;
@@ -26,7 +27,7 @@ public class Scopes {
2627
// Global (instance-wide) scope
2728
// This computation runs at instance cold-start.
2829
// Warning: Class variables used in Servlet classes must be thread-safe,
29-
// or else might introduce race conditions in your code.
30+
// or else might introduce race conditions in your code.
3031
private static final int InstanceVar = heavyComputation();
3132

3233
public void scopeDemo(HttpServletRequest request, HttpServletResponse response)
@@ -38,16 +39,16 @@ public void scopeDemo(HttpServletRequest request, HttpServletResponse response)
3839
PrintWriter writer = response.getWriter();
3940
writer.write(String.format("Instance: %s; function: %s", InstanceVar, functionVar));
4041
}
41-
42+
4243
private static int lightComputation() {
4344
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
4445
return Arrays.stream(numbers).sum();
4546
}
46-
47+
4748
private static int heavyComputation() {
4849
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
4950
return Arrays.stream(numbers).reduce((t, x) -> t * x).getAsInt();
5051
}
5152
}
52-
53+
// [END run_tips_global_scope]
5354
// [END functions_tips_scopes]

functions/snippets/src/test/java/SnippetsTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ public void scopesTest() throws IOException {
163163
assertThat(responseOut.toString(), containsString("Instance:"));
164164
}
165165

166+
@Test
167+
public void lazyTest() throws IOException {
168+
new Lazy().lazyGlobal(request, response);
169+
170+
assertThat(responseOut.toString(), containsString("Lazy global:"));
171+
}
172+
166173
@Test
167174
public void retrieveLogsTest() throws IOException {
168175
new RetrieveLogs().retrieveLogs(request, response);
@@ -177,7 +184,7 @@ public void helloBackgroundTest() throws IOException {
177184
when(request.getReader()).thenReturn(bodyReader);
178185

179186
new HelloBackground().helloBackground(request, response);
180-
assertThat(responseOut.toString(), containsString("Hello John!"));
187+
assertThat(responseOut.toString(), containsString("Hello John!"));
181188
}
182189

183190
@Test
@@ -192,7 +199,7 @@ public void envTest() throws IOException {
192199
new EnvVars().envVar(request, response);
193200
assertThat(responseOut.toString(), containsString("BAR"));
194201
}
195-
202+
196203
@Test
197204
public void helloExecutionCount() throws IOException {
198205
new Concepts().executionCount(request, response);

0 commit comments

Comments
 (0)