<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Christopher J. Campo</title>
    <description>Christopher J. Campo is a software engineer with a background in physics and interests in astronomy, piano, and the outdoors.
</description>
    <link>https://www.ccampo.me/</link>
    <atom:link href="https://www.ccampo.me/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sat, 17 Oct 2026 20:08:19 +0000</pubDate>
    <lastBuildDate>Sat, 17 Oct 2026 20:08:19 +0000</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>gRPC-Gateway servers on AWS Lambda using Unix domain sockets</title>
        <description>&lt;p&gt;I’ve really enjoyed using &lt;a href=&quot;https://github.com/grpc-ecosystem/grpc-gateway&quot;&gt;gRPC-Gateway&lt;/a&gt; to build gRPC/JSON-over-HTTP 
(RESTful) APIs in Go for quite a few years now. However, at first glance, it
appears to be incompatible with one of my favorite ways to deploy small
serverless apps - using &lt;a href=&quot;https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway-tutorial.html&quot;&gt;AWS API Gateway and AWS Lambda&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;According to the &lt;a href=&quot;https://github.com/grpc-ecosystem/grpc-gateway/blob/main/README.md&quot;&gt;documentation&lt;/a&gt;, gRPC-Gateway is architected as
follows:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/grpc_gw_arch.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This means generally that gRPC-Gateway apps usually run two servers - one for 
gRPC and one for HTTP. The HTTP server is a reverse proxy that translates HTTP
requests to gRPC calls, and actually makes local gRPC calls to the gRPC server.
The servers typically listen on different ports, although you can configure them
to listen on the same port using something like &lt;a href=&quot;https://github.com/soheilhy/cmux&quot;&gt;cmux&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Immediately you can see that this architecture is not directly compatible with
AWS Lambda. Lambda is stateless by definition, and does not outwardly support
the idea of running servers and listening on any port(s). Lambda provides an
&lt;a href=&quot;https://github.com/awslabs/aws-lambda-go-api-proxy&quot;&gt;adapter library&lt;/a&gt; to support HTTP APIs via standard Go
&lt;a href=&quot;https://pkg.go.dev/net/http#Handler&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http.Handler&lt;/code&gt;&lt;/a&gt;s, but it doesn’t support running a server. In fact, if
you try to listen with a &lt;a href=&quot;https://pkg.go.dev/net#Listener&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;net.Listener&lt;/code&gt;&lt;/a&gt; in a Lambda function, you will
get an error.&lt;/p&gt;

&lt;p&gt;Luckily, an &lt;a href=&quot;https://pkg.go.dev/net/http#Handler&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http.Handler&lt;/code&gt;&lt;/a&gt; is all we need to run the gRPC-Gateway
reverse proxy component on Lambda using the &lt;a href=&quot;https://github.com/awslabs/aws-lambda-go-api-proxy&quot;&gt;adapter&lt;/a&gt;, and we can use
gRPC-Gateway’s &lt;a href=&quot;https://pkg.go.dev/github.com/grpc-ecosystem/grpc-gateway/runtime#ServeMux&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;runtime.ServeMux&lt;/code&gt;&lt;/a&gt; for that.&lt;/p&gt;

&lt;p&gt;That covers the HTTP portion. However, we’re still left with the problem of
running the gRPC server, which the HTTP proxy makes outbound requests to. As I
just mentioned, Lambda forbids running servers. So how then, can gRPC-Gateway
work?&lt;/p&gt;

&lt;p&gt;This is where the trick comes in. We actually &lt;em&gt;can&lt;/em&gt; run the gRPC server on
Lambda, but we can’t listen on a port. Instead, we can create a Unix domain
socket and listen on that&lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; (you may have guessed the punchline from this
post’s title). You might point out that Lambda’s filesystem is &lt;a href=&quot;https://stackoverflow.com/q/39383465&quot;&gt;readonly&lt;/a&gt;,
and you would generally be right… but luckily, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp&lt;/code&gt; directory is
writable (up to &lt;a href=&quot;https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html&quot;&gt;512 MB&lt;/a&gt; by default).&lt;/p&gt;

&lt;p&gt;The plan of attack is then:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Create a Unix domain socket in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp&lt;/code&gt; and create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;net.Listener&lt;/code&gt; to listen
on it.&lt;/li&gt;
  &lt;li&gt;Run the gRPC server using this listener.&lt;/li&gt;
  &lt;li&gt;Create the gRPC-Gateway HTTP reverse proxy, and configure its gRPC
entrypoint to be the gRPC server’s Unix domain socket (see &lt;a href=&quot;https://github.com/grpc-ecosystem/grpc-gateway?tab=readme-ov-file#5-write-an-entrypoint-for-the-http-reverse-proxy-server&quot;&gt;docs&lt;/a&gt;).&lt;/li&gt;
  &lt;li&gt;Pass the HTTP reverse proxy’s &lt;a href=&quot;https://pkg.go.dev/github.com/grpc-ecosystem/grpc-gateway/runtime#ServeMux&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;runtime.ServeMux&lt;/code&gt;&lt;/a&gt; to the Lambda
&lt;a href=&quot;https://github.com/awslabs/aws-lambda-go-api-proxy&quot;&gt;adapter library&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Run the Lambda function with the handler function.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/grpc_gw_lambda_arch_c4.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This ends up working surprisingly well, and I haven’t encountered any issues
yet. All the interceptors and other gRPC server features work as expected. The
main downside is that the gRPC server is not exposed via Lambda and API Gateway,
but this is unfortunately a limitation of those platforms.&lt;/p&gt;

&lt;h2 id=&quot;faq&quot;&gt;FAQ&lt;/h2&gt;

&lt;h3 id=&quot;can-i-make-grpc-requests-to-the-lambda-function&quot;&gt;Can I make gRPC requests to the Lambda function?&lt;/h3&gt;

&lt;p&gt;Nope.&lt;/p&gt;

&lt;h3 id=&quot;cant-i-just-use-the-grpc-gateway-mux-without-the-grpc-server&quot;&gt;Can’t I just use the gRPC-Gateway mux without the gRPC server?&lt;/h3&gt;

&lt;p&gt;Yes and no. If you register your services directly to the mux using the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Register...Handler&lt;/code&gt; methods generated by gRPC-Gateway, then you can use the
mux without a gRPC server. You could pass this mux directly to the Lambda
adapter, and it would work as a plain HTTP server.&lt;/p&gt;

&lt;p&gt;However, if you want to use gRPC specific features like interceptors (commonly
used for auth for example), which are registered directly to the gRPC
&lt;a href=&quot;https://pkg.go.dev/google.golang.org/grpc#Server&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Server&lt;/code&gt;&lt;/a&gt;, you’ll be out of
luck. Being able to use (auth) interceptors was actually my main motivation for
writing this.&lt;/p&gt;

&lt;h3 id=&quot;just-why&quot;&gt;Just… why?&lt;/h3&gt;

&lt;p&gt;Good question. As I mentioned above, you still can’t make gRPC requests to the
Lambda function. So why even bother, and instead why not just avoid gRPC
altogether and use a regular JSON-over-HTTP API (e.g. via a 
&lt;a href=&quot;https://pkg.go.dev/net/http#Handler&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http.Handler&lt;/code&gt;&lt;/a&gt;)?&lt;/p&gt;

&lt;p&gt;If that’s all you need, then you’re right - you don’t need to go through all
this trouble. However, if you already have an &lt;em&gt;existing&lt;/em&gt; gRPC service that you
want to run on Lambda, and you’re OK with exposing it as JSON-over-HTTP API,
then this is a good way to do it.&lt;/p&gt;

&lt;p&gt;If you’re starting from scratch, I do not recommend this approach. Instead, I
recommend using &lt;a href=&quot;connect&quot;&gt;Connect&lt;/a&gt;. You should be able to pass Connect’s regular
HTTP multiplexer to the Lambda adapter and be done with it.&lt;/p&gt;

&lt;h2 id=&quot;sample-project&quot;&gt;Sample Project&lt;/h2&gt;

&lt;p&gt;I’ve created a &lt;a href=&quot;https://github.com/ccampo133/lambda-grpc-gateway&quot;&gt;sample project&lt;/a&gt; that demonstrates this setup. It’s a
simple gRPC service that echoes a message back to the client. The gRPC server
and gRPC-Gateway reverse proxy are both run in the same Lambda function. The
project contains a Terraform configuration to deploy the Lambda function and API
Gateway. Please check it out for more details!&lt;/p&gt;

&lt;hr /&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Apparently this trick was pointed out years ago in an &lt;a href=&quot;https://medium.com/@tjholowaychuk/dos-and-don-ts-of-aws-lambda-7dfcab7ad115&quot;&gt;article&lt;/a&gt; by 
  TJ Holowaychuk. See the “Running any web server in Lambda” heading. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Mon, 26 Aug 2024 17:21:00 +0000</pubDate>
        <link>https://www.ccampo.me/aws/lambda/go/grpc/grpg-gateway/2024/08/26/lambda-grpc-gateway.html</link>
        <guid isPermaLink="true">https://www.ccampo.me/aws/lambda/go/grpc/grpg-gateway/2024/08/26/lambda-grpc-gateway.html</guid>
        
        
        <category>aws</category>
        
        <category>lambda</category>
        
        <category>go</category>
        
        <category>grpc</category>
        
        <category>grpg-gateway</category>
        
      </item>
    
      <item>
        <title>Duck Typing (horror) in Go</title>
        <description>&lt;p&gt;If you say Go supports &lt;a href=&quot;https://github.com/ccampo133/go-docker-alpine-remote-debug&quot;&gt;“duck typing”&lt;/a&gt; online, you’ll probably get some
pushback. The Go community often points out that Go doesn’t support duck typing
because it’s statically typed, and duck typing is a feature of dynamically typed
languages. Rather, Go has “structural typing” where interfaces are still
implicitly implemented, but still checked at compile time.&lt;/p&gt;

&lt;p&gt;Here’s an example of structural typing in Go:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-go&quot; data-lang=&quot;go&quot;&gt;&lt;span class=&quot;k&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fmt&quot;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Duck&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Swim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Quack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mallard&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Mallard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Swim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;mallard swimming&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Mallard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Quack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;mallard quacking&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Goose&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Goose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Swim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;goose swimming&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Goose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Quack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;goose quacking&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;quack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ducks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Duck&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duck&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;range&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ducks&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;duck&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Swim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;duck&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Quack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;quack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Mallard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Goose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Give a &lt;a href=&quot;https://go.dev/play/p/fYrgjjEgxXi&quot;&gt;try&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mallard&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Goose&lt;/code&gt; implicitly implement the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Duck&lt;/code&gt; interface, so they
can be assumed to be a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Duck&lt;/code&gt; at compile time, and in this example, in the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quack&lt;/code&gt; function. A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Goose&lt;/code&gt; swims like a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Duck&lt;/code&gt; and quacks like a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Duck&lt;/code&gt;, so it
must be a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Duck&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;So how isn’t this duck typing? Well, it’s a bit of a pedantic argument, but
duck typing is typically associated with dynamically typed languages, where the
types are determined at runtime rather than compile time like here.&lt;/p&gt;

&lt;p&gt;From &lt;a href=&quot;https://en.wikipedia.org/wiki/Duck_typing#Structural_type_systems&quot;&gt;Wikipedia&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Duck typing is similar to, but distinct from, structural typing. Structural
typing is a &lt;em&gt;static typing system&lt;/em&gt; that determines type compatibility and
equivalence by a type’s structure, whereas duck typing is &lt;em&gt;dynamic&lt;/em&gt; and
determines type compatibility by only that part of a type’s structure that is
accessed during &lt;em&gt;runtime&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With that definition in hand, can we do duck typing in Go? Well, when using the
language as intended, &lt;strong&gt;no&lt;/strong&gt;. But if you’re willing to hack around the type
system a bit, you can still write this horror show:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-go&quot; data-lang=&quot;go&quot;&gt;&lt;span class=&quot;k&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fmt&quot;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Duck&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Swim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Quack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mallard&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Mallard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Swim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;mallard swimming&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Mallard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Quack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;mallard quacking&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dog&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dog&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Swim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;dog swimming&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dog&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Bark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;woof&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;quack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ducks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duck&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;range&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ducks&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;duck&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Swim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Swim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;duck&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Quack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Quack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;quack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Mallard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dog&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{})&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Go ahead… &lt;a href=&quot;https://go.dev/play/p/FNg_xqIfbwy&quot;&gt;run it&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;I would say that Go can duck type with the best of em!&lt;/p&gt;

&lt;p&gt;PS: please don’t write real code like this! I’m just having some in this post!
It’s really a bad idea. Don’t do it. This example is intended to panic!&lt;/p&gt;

&lt;p&gt;My point is just to show that while it is often said that Go doesn’t support
duck typing, you can write something that looks awfully a lot like duck typing
if you (mis)use some of the more “dynamic” features of the language like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt;
and type assertions and hack around the type system. It doesn’t even require
reflection, as one might expect, such as in Java&lt;sup&gt;&lt;a href=&quot;https://stackoverflow.com/a/1079799&quot;&gt;1&lt;/a&gt;,
&lt;a href=&quot;https://thinking-in-code.blogspot.com/2008/11/duck-typing-in-java-using-dynamic.html&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;. It is essentially the Go analogue of the
&lt;a href=&quot;https://en.wikipedia.org/wiki/Duck_typing#Example&quot;&gt;Wikipedia duck typing example in Python&lt;/a&gt;.
And if the code looks and behaves like duck typing, then it must duck typing ;).&lt;/p&gt;

&lt;p&gt;Please note that there are &lt;em&gt;very valid&lt;/em&gt; use cases for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt; and type assertions,
but duck typing is not one of them!&lt;/p&gt;

&lt;p&gt;Of course, the “correct” implementation of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quack&lt;/code&gt; function would be to
accept &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Duck&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt; and ditch the type assertions, which will result
a &lt;em&gt;compile&lt;/em&gt; time error. This is the first example in this post.&lt;/p&gt;

&lt;p&gt;Even if for whatever reason you were stuck with using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any&lt;/code&gt;, you could still use
the &lt;a href=&quot;https://go.dev/tour/methods/15&quot;&gt;two-valued type assertion&lt;/a&gt; (or a &lt;a href=&quot;https://go.dev/tour/methods/16&quot;&gt;type switch&lt;/a&gt;) to check if the
type implements &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Duck&lt;/code&gt;, which of course is still a runtime check, but at least
it avoids the panic:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-go&quot; data-lang=&quot;go&quot;&gt;&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;quack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ducks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duck&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;range&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ducks&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duck&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Duck&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Swim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Quack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;not a duck&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;See &lt;a href=&quot;https://go.dev/play/p/uK8fmJi560W&quot;&gt;here&lt;/a&gt; for the output.&lt;/p&gt;

&lt;p&gt;Still, always prefer using the type system and the compiler to your advantage
whenever possible!&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
        <pubDate>Tue, 20 Aug 2024 20:19:12 +0000</pubDate>
        <link>https://www.ccampo.me/go/2024/08/20/go-duck-typing.html</link>
        <guid isPermaLink="true">https://www.ccampo.me/go/2024/08/20/go-duck-typing.html</guid>
        
        
        <category>go</category>
        
      </item>
    
      <item>
        <title>Piano</title>
        <description>&lt;p&gt;I started learning to play the piano on Christmas of 2022 and thus I am coming up on my one-year anniversary of starting
to play. It’s been a very rewarding experience and I plan on continuing for the foreseeable future! Occasionally I upload
videos of my playing to a &lt;a href=&quot;https://youtube.com/playlist?list=PLm0HPR2KRrKWWnfgZ_MWag3VBneqHW7Mt&quot;&gt;YouTube playlist&lt;/a&gt;. 
Check it out if you are interested :).&lt;/p&gt;

&lt;div class=&quot;embed-container&quot; style=&quot;text-align: center;&quot;&gt;
    &lt;iframe src=&quot;https://www.youtube.com/embed/videoseries?list=PLm0HPR2KRrKWWnfgZ_MWag3VBneqHW7Mt&quot; width=&quot;560&quot; height=&quot;315&quot; title=&quot;YouTube video player&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; style=&quot;border: none;&quot; allowfullscreen=&quot;&quot;&gt;
    &lt;/iframe&gt;
&lt;/div&gt;

</description>
        <pubDate>Mon, 12 Dec 2022 18:36:12 +0000</pubDate>
        <link>https://www.ccampo.me/general/personal/reflection/2022/12/12/piano.html</link>
        <guid isPermaLink="true">https://www.ccampo.me/general/personal/reflection/2022/12/12/piano.html</guid>
        
        
        <category>general</category>
        
        <category>personal</category>
        
        <category>reflection</category>
        
      </item>
    
      <item>
        <title>Using Remote Debug with Delve with a Dockerized Alpine Linux Go Application</title>
        <description>&lt;p&gt;I recently created a &lt;a href=&quot;https://github.com/ccampo133/go-docker-alpine-remote-debug&quot;&gt;simple example&lt;/a&gt; on how to enable remote debugging
using &lt;a href=&quot;https://github.com/go-delve/delve&quot;&gt;Delve&lt;/a&gt;for a Go application running in
an Alpine Linux Docker container. This may be common knowledge in the Go
community, and your IDE and Delve’s own documentation will show you how to
enable remote debugging for Go programs. However, I figured it would be nice to
see it packaged in a concise example, specifically using an Alpine Linux base. I
use a setup like this at work every day on the various applications I work on.
Hopefully somebody finds this helpful :).&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/ccampo133/go-docker-alpine-remote-debug&quot;&gt;View the full example on GitHub here.&lt;/a&gt;&lt;/p&gt;

</description>
        <pubDate>Mon, 12 Dec 2022 17:48:18 +0000</pubDate>
        <link>https://www.ccampo.me/go/docker/containers/2022/12/12/delve-remote-debug-go-alpine-docker.html</link>
        <guid isPermaLink="true">https://www.ccampo.me/go/docker/containers/2022/12/12/delve-remote-debug-go-alpine-docker.html</guid>
        
        
        <category>go</category>
        
        <category>docker</category>
        
        <category>containers</category>
        
      </item>
    
      <item>
        <title>COVID-19</title>
        <description>&lt;p&gt;The ongoing COVID-19 pandemic is unlike anything I’ve ever experienced before. While I’ve been self-isolating for the 
past ten days or so, I put together this simple, auto-updated website, with a few data visualizations: &lt;a href=&quot;https://covid19.ccampo.me&quot;&gt;https://covid19.ccampo.me&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;…or alternatively &lt;a href=&quot;https://www.ccampo.me/covid19-visualizations&quot;&gt;https://www.ccampo.me/covid19-visualizations&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While not as robust or feature-packed as some of the excellent dashboards out there (e.g. &lt;a href=&quot;https://www.worldometers.info/coronavirus/&quot;&gt;Worldometer&lt;/a&gt;, 
&lt;a href=&quot;https://www.arcgis.com/apps/opsdashboard/index.html#/bda7594740fd40299423467b48e9ecf6&quot;&gt;JHU&lt;/a&gt;, and &lt;a href=&quot;https://ncov2019.live/&quot;&gt;ncov2019.live&lt;/a&gt;), the idea is to offer a quick glance at totals and a few tailored 
visualizations. The website is automatically updated every day at midnight GMT.&lt;/p&gt;

&lt;p&gt;View the &lt;a href=&quot;https://github.com/ccampo133/covid19-visualizations&quot;&gt;GitHub repository&lt;/a&gt; to view the code in more detail. Additionally, all historical data (previous 
updates) are included there. All contributions are welcome. I hope this website can be of help to somebody in these 
trying times.&lt;/p&gt;

&lt;p&gt;We’re all in this together. Stay healthy, stay safe, and we’ll get through this eventually.&lt;/p&gt;

&lt;p&gt;Best,&lt;/p&gt;

&lt;p&gt;/cc&lt;/p&gt;

</description>
        <pubDate>Wed, 25 Mar 2020 00:44:00 +0000</pubDate>
        <link>https://www.ccampo.me/general/covid-19/data-analysis/2020/03/25/covid19.html</link>
        <guid isPermaLink="true">https://www.ccampo.me/general/covid-19/data-analysis/2020/03/25/covid19.html</guid>
        
        
        <category>general</category>
        
        <category>covid-19</category>
        
        <category>data-analysis</category>
        
      </item>
    
      <item>
        <title>Java in a Container: Resource Allocation Guidelines</title>
        <description>&lt;p&gt;Containers have changed the face of the software industry in just a few short years. Perhaps you’ve gotten to the point
where you’re now running Java in a container. That’s great! Unfortunately, there are still some things to be aware of
when it comes to CPU and memory utilization for containerized Java applications, which I’ll outline below.&lt;/p&gt;

&lt;p&gt;This article assumes some familiarity with Java and containers in general. If you need more background, check out some 
(or all) of the &lt;a href=&quot;#references&quot;&gt;references&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;heap-space&quot;&gt;Heap Space&lt;/h2&gt;

&lt;p&gt;If there’s one key statement regarding running Java in a container, it’s the following:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DO NOT SET THE JVM HEAP SPACE MANUALLY FOR ANY JAVA PROCESS RUNNING IN A CONTAINER. INSTEAD, SET THE CONTAINER LIMITS.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;First and foremost, setting container limits accomplishes the very basic objective of containers/cgroups to begin 
with… that is, to isolate resource usage of a collection of processes. When you allocate the heap space manually via 
JVM arguments for example, you disregard that entirely.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;It allows easy tuning of container resource allocation. Need more memory? Bump the container limit. Need less? 
Downsize it. This opens the door for automated processes to scale containers accordingly (such as the &lt;a href=&quot;https://github.com/kubernetes/autoscaler/tree/master/vertical-pod-autoscaler&quot;&gt;k8s vertical pod 
autoscaler&lt;/a&gt;) without having to manually tweak JVM parameters.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If running in a container orchestration environment (such as Kubernetes), the container limits become extremely 
important for both node health and scheduling. The scheduler will use these limits to find an appropriate node to run a 
container, and ensure that equal load is spread across nodes. If you are setting memory usage via JVM arguments, this 
information is not available to the scheduler, and therefore the scheduler has no idea how to effectively spread load 
for your containers.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If you do not set container limits, and Java is running in a container without any JVM memory flags explicitly set, 
the JVM will automatically set the max heap to 25% of the RAM on the &lt;strong&gt;node&lt;/strong&gt; it is running on. For example, if your 
container is running on a 64 GB node, your JVM process heap space can max out at 16 GB. If you’re running 10 containers 
on a node (a common occurrence due to auto-scaling), then all of the sudden you’re on the hook for 160 GB of RAM. It’s a 
disaster waiting to happen.&lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;what-can-you-do-about-it&quot;&gt;What can you do about it?&lt;/h3&gt;

&lt;p&gt;Set the container memory (and CPU) limits.&lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; It is not sufficient to rely on resource requests (soft limits) only. 
Requests are great for helping out the scheduler, but setting hard limits allows Docker (or whatever container runtime 
you’re using) to allocate the specified resources to the container itself, and no more. This will also allow Java 
(which is “container aware” by default as of Java 8u191) to properly allocate the memory based on the resource 
limits placed on the container itself, instead of the node it is running on.&lt;/p&gt;

&lt;h3 id=&quot;regarding-the-minmaxinitialrampercentage-parameters&quot;&gt;Regarding the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Min|Max|Initial]RAMPercentage&lt;/code&gt; Parameters&lt;/h3&gt;

&lt;p&gt;In a &lt;a href=&quot;https://www.oracle.com/technetwork/java/javase/8u191-relnotes-5032181.html#JDK-8146115&quot;&gt;fairly recent version of Java&lt;/a&gt;, the following JVM parameters were introduced (and back-ported to Java
8u191).&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-XX:MinRAMPercentage&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-XX:MaxRAMPercentage&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-XX:InitialRAMPercentage&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I won’t go into detail about how these exactly work,&lt;sup id=&quot;fnref:3&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:3&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; but the key takeaway is that they can be used to fine tune the 
JVM heap size without setting the heap size directly. That is, the container can still rely on the limits imposed on it.&lt;/p&gt;

&lt;p&gt;So what are the correct values to use? The answer is - “it depends”… particularly on the limits imposed on the 
container.&lt;/p&gt;

&lt;p&gt;By default, the JVM heap gets 25% of the container’s memory. You can tune the initial/min/max heap parameters to change 
this… e.g. setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-XX:MaxRAMPercentage=50&lt;/code&gt; will allow the JVM to consume 50% of the container memory for the heap, 
instead of the default 25%. When this is safe depends largely on how much memory the container has to work with, and 
what processes are running in the container.&lt;/p&gt;

&lt;p&gt;For example, if your container is running a single Java process, has 4 GB of RAM allocated to it, and you set 
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-XX:MaxRAMPercentage=50&lt;/code&gt;, the JVM heap will get 2 GB. This is opposed to the 1 GB default it would normally get. In 
this case, 50% is almost certainly perfectly safe, and perhaps optimal, since much of the available RAM was probably 
under-utilized. However, imagine the same container has only 512 MB of RAM allocated to it. Now setting 
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-XX:MaxRAMPercentage=50&lt;/code&gt; gives the heap 256 MB of RAM and only leaves the other 256 MB to rest of the entire container. 
This memory would need to be shared by all other processes running in the container, plus the JVM Metaspace/PermGen, 
etc. allocations. Perhaps 50% is not so safe in this case.&lt;/p&gt;

&lt;p&gt;Therefore, I can offer the following recommendations:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Don’t touch these parameters unless you want to squeeze some extra memory headroom for your Java process. In most 
cases, the 25% default is a safe approach to managing memory. It might not be the most memory efficient, but RAM is 
cheap and it’s always better to err on the side of caution, versus having your JVM process OOM-killed for some unknown 
reason.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If you do want to tweak these, be conservative. 50% is most likely a safe value where you can expect to avoid issues 
(in most cases), however this is still is largely dependent on how big (memory-wise) your container is. I don’t 
recommend going to 75% unless your container has at least 512 MB of RAM (preferably 1 GB), and you have a good 
understanding of the memory usage of the application in question.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If your container is running multiple processes in addition to Java, be extra cautious when tweaking these values.
The container memory is shared across all of these processes, and understanding total container memory usage in these 
cases is more complicated.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Anything over 90% is probably asking for trouble.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;what-about-metaspacepermgenetc&quot;&gt;What about Metaspace/PermGen/etc?&lt;/h2&gt;

&lt;p&gt;That’s beyond the scope of this article but rest assured that this can also be tweaked, but likely shouldn’t. The 
default JVM behavior be fine for majority of use cases. If you find yourself trying to tackle an obscure memory issue, 
then it might be time to look into fiddling with this somewhat esoteric area of JVM memory, but otherwise I would avoid 
doing anything directly.&lt;/p&gt;

&lt;h2 id=&quot;and-cpu&quot;&gt;And CPU?&lt;/h2&gt;

&lt;p&gt;There’s not much to do here. As of Java 8u191, the JVM is pretty “container aware” by default and interprets CPU share 
allocation correctly. There are details around this that are worth understanding though, so instead of me outlining it 
here, I will direct you to &lt;a href=&quot;https://medium.com/@christopher.batey/cpu-considerations-for-java-applications-running-in-docker-and-kubernetes-7925865235b7&quot;&gt;this excellent article&lt;/a&gt; explaining it all in detail.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Modern Java is well suited to run in a container environment, but there are some not-so-obvious details that everybody 
should know to ensure they’re getting the best performance from their applications. I hope that the information 
presented here, combined with the excellent &lt;a href=&quot;#references&quot;&gt;references&lt;/a&gt;, helps you accomplish this goal.&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://jaxenter.com/nobody-puts-java-container-139373.html&quot;&gt;Nobody Puts Java In A Container&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://medium.com/adorsys/jvm-memory-settings-in-a-container-environment-64b0840e1d9e&quot;&gt;JVM Memory Settings in a Container Environment&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.oracle.com/technetwork/java/javase/8u191-relnotes-5032181.html#JDK-8146115&quot;&gt;JDK 8u181 release notes regarding container support&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://blog.softwaremill.com/docker-support-in-new-java-8-finally-fd595df0ca54&quot;&gt;Docker support in Java 8 — finally!&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://banzaicloud.com/blog/java10-container-sizing/&quot;&gt;How to correctly size containers for Java 10 applications&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.javacodegeeks.com/2018/12/docker-jvm.html&quot;&gt;Docker and the JVM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://dzone.com/articles/java-8-permgen-metaspace&quot;&gt;Java 8: From PermGen to Metaspace&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://trustmeiamadeveloper.com/2016/03/18/where-is-my-memory-java/&quot;&gt;Analyzing Java memory usage in a Docker container&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://stackoverflow.com/questions/52596383/kubernetes-throwing-oom-for-pods-running-a-jvm&quot;&gt;Kubernetes throwing OOM for pods running a JVM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://medium.com/@christopher.batey/cpu-considerations-for-java-applications-running-in-docker-and-kubernetes-7925865235b7&quot;&gt;CPU considerations for Java applications running in Docker and Kubernetes&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container&quot;&gt;Kubernetes: Managing Compute Resources for Containers&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.docker.com/config/containers/resource_constraints&quot;&gt;Docker: Runtime options with Memory, CPUs, and GPUs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Note In the 64 GB / 16 GB JVM example, this does not mean that the JVM process automatically consumes 16 GB of RAM for the heap… just that it can potentially grow that large before being considered out of memory. Additionally, there is no downward pressure on the garbage collector. The heap can then easily grow to consume more memory than the container can support before the GC is triggered due to miscalculated max heap space. This would inevitable cause application issues (e.g. OOM errors) or worse (e.g. OOM-killed, crash). &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;For information on setting hard limits in Docker and Kubernetes, check out the following links: &lt;a href=&quot;https://docs.docker.com/config/containers/resource_constraints&quot;&gt;1&lt;/a&gt;, &lt;a href=&quot;https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container&quot;&gt;2&lt;/a&gt; &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:3&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;See this &lt;a href=&quot;https://stackoverflow.com/a/54297753&quot;&gt;StackOverflow&lt;/a&gt; thread where this is discussed. &lt;a href=&quot;#fnref:3&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Thu, 31 Oct 2019 17:09:00 +0000</pubDate>
        <link>https://www.ccampo.me/java/docker/containers/kubernetes/2019/10/31/java-in-a-container.html</link>
        <guid isPermaLink="true">https://www.ccampo.me/java/docker/containers/kubernetes/2019/10/31/java-in-a-container.html</guid>
        
        
        <category>java</category>
        
        <category>docker</category>
        
        <category>containers</category>
        
        <category>kubernetes</category>
        
      </item>
    
      <item>
        <title>Answers To A Management Questionnaire (With Commentary)</title>
        <description>&lt;p&gt;I was given this questionnaire a few years ago as I began my journey into software engineering management, and answered it for my own sake. I don’t think I ever shared my answers with anybody. Recently, I dug it up and found it cathartic, albeit somewhat naive as well. Below are my &lt;em&gt;original&lt;/em&gt; answers, with minimal editing (aside from spelling, etc.), intended for both self-reflection and posterity. Any additions I’ve added are indicated by brackets — used mainly for clarification, but there are some comments peppered in there as well. I might take another stab at this as a follow up, perhaps with lessons learned, but we’ll see. It’s probably too soon for that now.&lt;/p&gt;

&lt;p&gt;In any case, I hope this is useful to somebody somehow. Thanks for reading.&lt;/p&gt;

&lt;h2 id=&quot;engineering-management-questionnaire&quot;&gt;Engineering Management Questionnaire&lt;/h2&gt;

&lt;h3 id=&quot;how-is-leadership-different-from-management-which-one-of-these-comes-more-naturally-to-you-how-do-you-intend-to-perform-your-job-in-both-respects&quot;&gt;How is leadership different from management? Which one of these comes more naturally to you? How do you intend to perform your job in both respects?&lt;/h3&gt;

&lt;p&gt;Management is being a task master. Leadership is being a beacon of stability and success that your team can look up to and trust to follow. Leadership comes more naturally to me than management. I am much more driven to drive my team to success than other aspects of management. In my job, I intend to lead by example.&lt;/p&gt;

&lt;h3 id=&quot;what-have-you-learned-in-your-time-as-a-team-lead-that-is-a-surprise-to-you&quot;&gt;What have you learned in your time as a team lead that is a surprise to you?&lt;/h3&gt;

&lt;p&gt;People are generally more approachable and productive in person than remotely, or via chat/email/calls/whatever. I previously underestimated the power of being in the same room together.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[Another thing I’ve since learned is teammates can and will respect you, regardless of any age/experience difference, if you treat them with mutual respect, elevate them at every opportunity, aren’t afraid to delegate, and exemplify ownership. Thinking inwards, these are qualities I expect of good leaders as well.]&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;what-new-skills-have-you-formed-during-this-time&quot;&gt;What new skills have you formed during this time?&lt;/h3&gt;

&lt;p&gt;Making executive decisions. In a leadership role, people look to you to make the call on things. This is very much a work in progress but it is certainly a newly formed skill, if you can call it that.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[This is still an extremely important skill that I continue to refine. You CANNOT wait to understand every decision fully before you make it. Willingness to take calculated risks is required.]&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;say-a-few-things-about-each-of-your-reports-regarding-their-top-five-strengths-how-do-you-plan-to-take-advantage-of-each-members-individual-strengths-to-achieve-the-goals-of-your-team&quot;&gt;Say a few things about each of your reports regarding their top five strengths. How do you plan to take advantage of each member’s individual strengths to achieve the goals of your team?&lt;/h3&gt;

&lt;p&gt;All of the team members had at least one (and many multiple) strengths in strategic thinking, including myself &lt;em&gt;[At this point, I was leading a team of six developers ranging from entry-level to ~10 years senior. I had been leading the team for about a year]&lt;/em&gt;. That seems like the obvious target to focus on, and much of that will be around learning and problem solving. Our knowledge transfer sessions is one approach &lt;em&gt;[The team conducted bi-weekly knowledge transfer sessions to work on de-siloing]&lt;/em&gt;. Another approach is prompting every team member to commit to full ownership of something they’re passionate about. When finding this “ownership of something”, we’ll focus on each individual’s strength to hone in on something worthwhile to devote energy to.&lt;/p&gt;

&lt;h3 id=&quot;give-an-example-of-an-area-of-weakness-in-yourself-how-you-plan-to-overcome-it-by-focusing-on-a-strength&quot;&gt;Give an example of an area of weakness in yourself how you plan to overcome it by focusing on a strength.&lt;/h3&gt;

&lt;p&gt;I can be blunt, and perhaps sharp at times — however, when I look at all the successful people around me, I notice a similar, more refined style. Nobody becomes truly successful by being a yes man. Obviously, there is a trait here that can be honed into something powerful with the right effort. According to &lt;em&gt;[StrenghtsFinder 2.0]&lt;/em&gt;, my top strength is “achiever”, followed closely by “learner” and “analytical”. I consider myself fairly successful thus far, but if any weakness is holding me back in any way, I need to focus on my achievement strength to propel past this, using introspection from my analytical side to learn what needs to change and how to change it, without completely altering my personality (which would be impossible anyway). Sounds sappy, but I buy into it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[I still generally believe this about myself, and it is a constant area of my attention. It comes with the turf of being opinionated and having strong personal aspirations. Good leaders must never be satisfied. However, as a leader, your personal ego constantly needs to be checked for the benefit of your team, realizing there is a big difference between confidence and cockiness. Understand not everybody will agree with you in the journey, or maybe not even like you for it, but that’s OK. What matters is getting the job done at the end of the day. You are not always right. Your teammates are not always right. Your boss is not always right. Their boss is not always right. And so on. All of these egos can be just as difficult to deal with as your own. However, when the overall goal is the mission at hand, and all egos need to be checked and eyes need to remain on target.]&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;in-what-ways-should-your-team-be-treated-uniformly-and-consistently-and-in-what-ways-should-they-be-treated-individually-and-differently&quot;&gt;In what ways should your team be treated uniformly and consistently, and in what ways should they be treated individually and differently?&lt;/h3&gt;

&lt;p&gt;Everybody should be treated with the same, high level of respect and fairness. Of course, the &lt;a href=&quot;https://en.wikipedia.org/wiki/Golden_Rule&quot;&gt;Golden Rule&lt;/a&gt; always applies. Individually, people should be addressed towards their strengths &lt;em&gt;[e.g. with respect to delegation of tasks]&lt;/em&gt;. For example, &lt;em&gt;[entry-level engineer]&lt;/em&gt; might be more suited to solving a tough technical problem than &lt;em&gt;[mid-level engineer]&lt;/em&gt;, however &lt;em&gt;[mid-level engineer]&lt;/em&gt; might be better suited to solving a complex architectural problem. However, neither would ever be treated as superior to the other.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[A good leader understands their team’s strengths, and plays to them.]&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;what-does-this-mean-to-you-people-dont-change-much-dont-waste-your-time-trying-to-put-in-what-can-be-left-out-try-to-draw-out-what-was-left-in---that-is-hard-enough-do-you-agree-or-disagree-why&quot;&gt;What does this mean to you: “People don’t change much. Don’t waste your time trying to put in what can be left out. Try to draw out what was left in - that is hard enough”. Do you agree or disagree? Why?&lt;/h3&gt;

&lt;p&gt;I agree with this, to an extent. Overall in my experience, people’s beliefs and personalities are largely static. Perhaps this is cynical, but it reflects my personal observations. Of course there are always the occasions where people have experiences that influence dramatic change in them, but those occasions are infrequent on a whole. If your goal is to influence somebody, or help drive them to success, you need to focus on what they &lt;em&gt;are&lt;/em&gt; rather than what they &lt;em&gt;aren’t&lt;/em&gt;. If you have a difference of opinion, while definitely a cliché, it’s much more productive to find common ground rather than to try to bring the person to your side (because no matter how good of a debater you are, you will never persuade somebody on personal opinion alone).&lt;/p&gt;

&lt;h3 id=&quot;where-is-your-team-on-the-progression-of-engagement-starting-with-detached-and-ending-with-fully-engaged-where-are-you-personally-on-this-journey&quot;&gt;Where is your team on the progression of engagement (starting with detached and ending with fully engaged). Where are you personally on this journey?&lt;/h3&gt;

&lt;p&gt;My team is engaged, but not fully. I know some folks don’t quite see a clear mission purpose, from &lt;em&gt;[previous organization]&lt;/em&gt; as a whole.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[This is relatable, even today, in a completely different setting with a completely different team.]&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Personally, I am at the growth stage of the engagement progression scale; perhaps not quite fully engaged, but proactively working on broadening my engagement. I see purpose in our work, and I want to focus on growth so we will win. Growing our team’s skills, growing our company’s skills, and growing our product. We have a unique market opportunity, but it will not be seized without people with this type of attitude.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[Again, even in a completely different setting, this rings true as ever]&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;what-does-it-mean-to-hire-for-talent-as-opposed-to-skill-and-knowledge-do-you-think-we-have-done-a-good-job-at-that&quot;&gt;What does it mean to hire for talent (as opposed to skill and knowledge)? Do you think we have done a good job at that?&lt;/h3&gt;

&lt;p&gt;Skills can be learned, knowledge can be acquired. Talent permits all of that innately.&lt;/p&gt;

&lt;p&gt;We’ve done a good job. The &lt;em&gt;[company I worked for at the time]&lt;/em&gt;’s hiring strategy is dead on. I feel like we’ve personally hired some of the best people in the company onto our team.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[I’m not quite as confident in my recent hiring endeavors, partly because the market is sparse, and partly because I’ve personally lost some focus on hiring talent as opposed to skills. This light bulb recently went off even before reading this, but it is fresh in my mind now as an area of improvement.]&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;what-talents-do-you-believe-are-key-to-having-a-super-successful-team-your-particular-team&quot;&gt;What talents do you believe are key to having a super successful team (your particular team)?&lt;/h3&gt;

&lt;p&gt;Focus, analysis, and input are key. Focus for getting things done, and analysis for getting things done the right way. Input, so we take others’ views into consideration. &lt;em&gt;[My team at the time]&lt;/em&gt; isn’t there yet (“super successful”), but I believe that is key.&lt;/p&gt;

&lt;h3 id=&quot;give-an-example-of-a-talent-you-have-noticed-in-an-individual-of-your-team-and-present-an-idea-about-how-to-fit-that-person-into-a-role-where-that-talent-is-required-what-would-that-look-like-it-can-be-a-real-or-hypothetical-role-that-does-not-yet-exist&quot;&gt;Give an example of a talent you have noticed in an individual of your team and present an idea about how to fit that person into a role where that talent is required. What would that look like? It can be a real or hypothetical role that does not yet exist.&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;[Entry level engineer]&lt;/em&gt; seems to have a talent and innate desire to do meaningful work. He’s shown that he’s willing to jump right into something if he thinks it would be a worthwhile effort. I think an eventual role focusing on this would be somewhat of an innovator. Somebody to come up with ideas and implement them while being open to feedback from others. I’m not sure if I can precisely define such a role at this point, but it would look something like a self-motivated engineer.&lt;/p&gt;

&lt;h3 id=&quot;give-an-example-of-how-you-can-define-an-outcome-for-an-employee-and-let-them-figure-out-how-to-get-there-as-opposed-to-defining-how-to-get-there-are-there-situations-where-defining-methods-is-the-right-approach&quot;&gt;Give an example of how you can define an outcome for an employee and let them figure out how to get there, as opposed to defining how to get there. Are there situations where defining methods is the right approach?&lt;/h3&gt;

&lt;p&gt;Defining an outcome can be as simple as saying “we want to commit to having X done by Y in Z fashion”, and then instructing the employee to take ownership of accomplishing the goal, instead of providing a well-defined set of steps to the outcome. This takes a self-motivated individual. On the other hand, yes, I believe that sometimes it is the correct approach to define methods, especially where there are multiple, discrete ways to solve a problem. Sometimes the best approach is to lay out the paths and pick one organically as a team.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[“When you come to a fork in the road, take it” - Yogi Berra]&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;using-any-advice-youve-absorbed-in-your-career-how-would-you-deal-with-an-under-performing-employee-use-a-real-or-imaginary-example-to-illustrate&quot;&gt;Using any advice you’ve absorbed in your career, how would you deal with an under-performing employee? Use a real or imaginary example to illustrate.&lt;/h3&gt;

&lt;p&gt;I would start with one-on-one meetings to get to the root of the problem. Once we know that, the advice given is to re-focus the employee on their strengths. Example: an employee is constantly working but seemingly not delivering any final products. First we would want to understand “why” they’re getting stuck. Perhaps in this example, they’re constantly context switching, which is killing their productivity but still forcing them to work long hours. And perhaps this individual has a strength of “focus”. We would then come up with a plan together on how to get the employee to focus — perhaps they disconnect for a few hours per day (or week), or maybe they work early in the morning and take the afternoon off. Whatever helps them focus would be the goal here. Build towards their strengths.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[This is one question I continue to ask myself, and have been asked of by others as well. I’ve done some reflecting on it over the years and have shifted my initial reaction to instead START WITH MYSELF (introspection) rather than start with the individual. For example, asking myself, “What am I doing that is causing this individual to under-perform?”. “There are no bad teams, only bad leaders.”]&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;what-do-you-do-when-someone-asks-for-a-promotion&quot;&gt;What do you do when someone asks for a promotion?&lt;/h3&gt;

&lt;p&gt;I would do what I would want my boss to do to me — take the request seriously. If the employee presents a good case why they deserve a promotion, and we both agree that they’ve done good work, then they should certainly be given the opportunity for a promotion. If there is, for whatever reason, no way the employee could get a promotion at that time, I would expect myself to be able to explain clearly why it is not possible.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[I see this is a somewhat naive response in hindsight, which makes sense since I hadn’t handled any promotions at the time of answering this originally. The general premise is fine, and this is how I’ve largely handled promotions, but reading this makes me realize I missed a key point - become an advocate for the promotee. Promotions almost always need to be approved by my boss, or his boss, so recommending the employee is usually not enough. You need to be in their corner until the promotion is complete, and clearly articulate every step of the process to the promotee. If a promotion is not possible, I better have a darn good answer why, especially if the person deserves it. Sometimes this involves ruffling feathers higher up, but it is necessary for maintaining a strong team, where trust and transparency is paramount.]&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;can-you-think-of-an-example-of-when-moving-up-the-ladder-is-actually-not-right-for-a-person-even-when-that-person-is-exceptionally-talented-how-do-you-retain-such-a-person&quot;&gt;Can you think of an example of when moving “up the ladder” is actually not right for a person, even when that person is exceptionally talented? How do you retain such a person?&lt;/h3&gt;

&lt;p&gt;Absolutely. Some “ladders” are structured in such a way that they involve shifting responsibilities. For example, if an engineer wants to move up the ladder and the next role is manager, they might be an exceptionally talented engineer but lack the interpersonal skills to effectively manage a team. I would attempt to retain this employee by offering other potential solutions. Perhaps they simply want more money. Maybe they want to work on a different project. Maybe they want additional responsibilities, which we could find in their areas of expertise, rather than whatever the current corporate ladder defines.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[Being a leader means empowering your team members. Work to their strengths, and provide growth opportunities for them wherever, whenever, and however you can.]&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;what-is-your-approach-to-setting-individual-goals-for-your-employees-how-is-that-working-so-far&quot;&gt;What is your approach to setting individual goals for your employees? How is that working so far?&lt;/h3&gt;

&lt;p&gt;My approach has been to align their goals with the overall team’s goals, as well as focus on ownership at the individual level. Each person sets a goal that they’re confident they can fully commit to owning throughout. So far I’ve gotten positive feedback on this approach, but I haven’t had much opportunity to assess this.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[I’ve now had opportunities to assess this strategy, and I still follow this largely. One big difference is that I’ve found it’s key to also focus on strengths when setting goals. Ownership remains a key part of the process.]&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&quot;what-value-do-you-place-on-trust-in-your-team-what-ideas-do-you-have-to-foster-a-trust-based-environment&quot;&gt;What value do you place on trust in your team? What ideas do you have to foster a trust-based environment?&lt;/h3&gt;

&lt;p&gt;Trust is key. It is perhaps one of, if not THE MOST VALUED traits in a good team. Fostering trust is simple – lead by example, and take ownership. A leader must take full responsibility for their team. Finish the things you say you’re going to. Keep promises and follow up on them. Have individualized conversations with your teammates. Stand up for your teammates when they’re under fire (cover and move). &lt;em&gt;Do&lt;/em&gt; what you say you’re going to do, don’t just &lt;em&gt;say&lt;/em&gt; it. Do not talk behind their backs. Show mutual respect for the entire team. If you can prove you’re trustworthy yourself, it will naturally radiate to the rest of your teammates.&lt;/p&gt;
</description>
        <pubDate>Fri, 04 Oct 2019 14:02:00 +0000</pubDate>
        <link>https://www.ccampo.me/general/management/work/reflection/2019/10/04/management-questionnaire.html</link>
        <guid isPermaLink="true">https://www.ccampo.me/general/management/work/reflection/2019/10/04/management-questionnaire.html</guid>
        
        
        <category>general</category>
        
        <category>management</category>
        
        <category>work</category>
        
        <category>reflection</category>
        
      </item>
    
      <item>
        <title>ccampo.ninja</title>
        <description>&lt;p&gt;I bought this domain a few months ago for $4.06. &lt;a href=&quot;http://ccampo.ninja&quot;&gt;Check it out!&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Fri, 04 Oct 2019 13:21:00 +0000</pubDate>
        <link>https://www.ccampo.me/general/2019/10/04/ccampo-dot-ninja.html</link>
        <guid isPermaLink="true">https://www.ccampo.me/general/2019/10/04/ccampo-dot-ninja.html</guid>
        
        
        <category>general</category>
        
      </item>
    
      <item>
        <title>NBodyJS: An N-Body Simulation Written in Kotlin</title>
        <description>&lt;p&gt;NBodyJS is a simple project I’ve had around for the past few years in various forms. It is a JavaScript 
&lt;a href=&quot;https://en.wikipedia.org/wiki/N-body_simulation&quot;&gt;n-body simulation&lt;/a&gt;. It is currently written in Kotlin. Initially I wrote it in plain JavaScript, then re-wrote
in Scala, and finally, re-wrote in Kotlin. Both the Scala and Kotlin versions transpile to readable JavaScript, using
ScalaJS for Scala and Kotlin’s built-in JavaScript support. The Kotlin implementation is by far my favorite, and the 
language itself is a joy to write in. NBodyJS utilizes the HTML5 canvas element for display, and &lt;a href=&quot;https://kotlinlang.org/docs/tutorials/javascript/kotlin-to-javascript/kotlin-to-javascript.html&quot;&gt;kotlin-js&lt;/a&gt; 
to transpile Kotlin to JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.ccampo.me/NBodyJS/&quot;&gt;See it in action and play around!&lt;/a&gt; You can also &lt;a href=&quot;https://github.com/ccampo133/NBodyJS&quot;&gt;view the code on GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h4 id=&quot;some-background-on-n-body-simulations-warning-math&quot;&gt;Some Background on N-body Simulations (warning: math!)&lt;/h4&gt;

&lt;p&gt;Within a gravity simulation composed of \(n\) bodies, the magnitude of the force \(\mathbf{F}_i\) acting on each body 
\(i\), is determined by Newton’s law of universal gravitation:&lt;/p&gt;

\[\mathbf{F}_i = \sum_{j = 1\atop j\not = i}^n G\frac{m_i m_j}{|\mathbf{r}_{ij}^2|}\hat{\mathbf{r}}_{ij}\]

&lt;p&gt;where \(G\) is the &lt;a href=&quot;https://en.wikipedia.org/wiki/Gravitational_constant&quot;&gt;gravitational constant&lt;/a&gt;, \(m_i\) is the mass of body \(i\), \(m_j\) is the mass of body
\(j\), and \(\mathbf{r}_{ij}\) is the distance vector from \(i\) to \(j\).&lt;/p&gt;

&lt;p&gt;In the case of gravity simulations, the forces due to gravity get exceedingly large as bodies get closer to each other 
(due to the &lt;a href=&quot;https://en.wikipedia.org/wiki/Inverse-square_law&quot;&gt;inverse-square law&lt;/a&gt; nature of the gravitational force), and hence bodies tend to rapidly 
accelerate 
and zoom off when they seemingly “collide”. To compensate for this, a softening length \(R^2\) has been applied in the 
gravity calculation to prevent bodies from zipping off into the abyss when they get near each-other. Also, \(G\) has 
been set to 1 for simplicity in NBodyJS.&lt;/p&gt;

&lt;p&gt;We can then use Newton’s second law \(\mathbf{F} = m\mathbf{a}\) to solve for the acceleration, and then numerically 
integrate it to get the positions and velocities of all bodies. In Kotlin, the code to compute the acceleration due 
to gravity looks like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-kotlin&quot; data-lang=&quot;kotlin&quot;&gt;&lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gravityAcceleration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bodies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;):&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Vector&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gravity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Vector&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;r12&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r12&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;G&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;SOFTENING_LENGTH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bodies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gravity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zero&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h4 id=&quot;the-algorithm&quot;&gt;The Algorithm&lt;/h4&gt;

&lt;p&gt;To compute the positions and velocities of all of the bodies, NBodyJS utilizes the &lt;a href=&quot;http://en.wikipedia.org/wiki/Verlet_integration#Velocity_Verlet&quot;&gt;Velocity Verlet&lt;/a&gt; method
of numerical integration. Verlet integration is a second-order method, which offers more stability than simpler, 
first order solutions such as &lt;a href=&quot;http://en.wikipedia.org/wiki/Euler_method&quot;&gt;Euler’s method&lt;/a&gt;, yet at no significant additional computational cost.&lt;/p&gt;

&lt;p&gt;For the algorithm to work, first you need to specify the initial conditions of the simulation; namely the initial 
positions and velocities of all the bodies at some start time \(t\). Then, the Velocity Verlet algorithm consists of 
the following steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Calculate the acceleration \(\mathbf{a}(t)\) of each body due to gravity (or whatever force(s) you want) at time 
\(t\), using Newton’s laws.&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Calculate the position \(\mathbf{x}(t + dt)\) of each body at time \(t + dt\) (where \(dt\) is a small time step), 
using the approximation&lt;/p&gt;

\[\mathbf{x}(t + dt) = \mathbf{x}(t) + \mathbf{v}(t)dt + \mathbf{a}(t)\frac{dt^2}{2}\]
  &lt;/li&gt;
  &lt;li&gt;Calculate the acceleration \(\mathbf{a}(t + dt)\), again using Newton’s laws and the forces in play.&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Calculate the velocity \(\mathbf{v}(t + dt)\) using the approximation&lt;/p&gt;

\[\mathbf{v}(t + dt) = \mathbf{v}(t) + (\mathbf{a}(t) + \mathbf{a}(t + dt))\frac{dt}{2}\]
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In Kotlin, the integration code is extremely simple:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-kotlin&quot; data-lang=&quot;kotlin&quot;&gt;&lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;verlet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;x1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;v1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dt&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This algorithm is executed every time you take a time step, \(dt\). Since every body exerts a force on every other 
body, computing the accelerations is done in \(O(n^2)\) time.&lt;/p&gt;

&lt;p&gt;There are more efficient, yet similarly accurate algorithms out there, so feel free to implement your own version if 
you feel like experimenting! Also, note that this method can be used to integrate Newton’s laws for nearly any force(s),
not just gravity!&lt;/p&gt;

</description>
        <pubDate>Thu, 01 Mar 2018 07:12:00 +0000</pubDate>
        <link>https://www.ccampo.me/general/javascript/kotlin/scala/java/2018/03/01/nbodyjs.html</link>
        <guid isPermaLink="true">https://www.ccampo.me/general/javascript/kotlin/scala/java/2018/03/01/nbodyjs.html</guid>
        
        
        <category>general</category>
        
        <category>javascript</category>
        
        <category>kotlin</category>
        
        <category>scala</category>
        
        <category>java</category>
        
      </item>
    
      <item>
        <title>Using Spring with AWS Lambda</title>
        <description>&lt;p&gt;I love &lt;a href=&quot;https://aws.amazon.com/lambda/&quot;&gt;AWS Lambda&lt;/a&gt;. Removing the concept of a “server” from your application is huge. I won’t go into all 
the benefits of Lambda, but I can say from first hand experience, it not only eliminates countless hours of worrying 
about servers and infrastructure, but also makes backend application development a breeze. Oh, and for most small 
stuff, &lt;a href=&quot;https://aws.amazon.com/lambda/pricing/&quot;&gt;it’s pretty much free&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Lambda currently supports three languages for application development - Javascript (via NodeJS), Python, and Java. My
preferred language for Lambda is Java &lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; for its static typing, maturity, and open source community… plus it’s 
what I write mostly in my day job. The Lambda API for Java is simple - you write a class that implements one of their
interfaces (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RequestHandler&lt;/code&gt;) or you write a “handler” method with a specific signature and tell Lambda to call it. You 
can &lt;a href=&quot;http://docs.aws.amazon.com/lambda/latest/dg/java-programming-model.html&quot;&gt;read the docs&lt;/a&gt; for the specifics, but suffice to say that it’s simple.&lt;/p&gt;

&lt;p&gt;While the Java API is nice, it’s pretty bare-bones. Also, since AWS Lambda requires your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RequestHandler&lt;/code&gt; 
implementation to have a default, no-arg constructor, writing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RequestHandler&lt;/code&gt;s with dependencies becomes tricky from
a testability standpoint. For example, imagine our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RequestHandler&lt;/code&gt; requires a data-access-object (DAO) class. If we
want to write some unit tests for this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RequestHandler&lt;/code&gt;, we have no way of stubbing this DAO since it has to be 
instantiated either as a field or in the no-arg constructor. Typically the way to solve this problem is using 
Dependency Injection/Inversion of control.&lt;/p&gt;

&lt;p&gt;My library of choice for dependency injection (among with a bunch of other cool features) is &lt;a href=&quot;http://spring.io&quot;&gt;Spring&lt;/a&gt;. There is 
a bit of boilerplate with integrating Spring into AWS Lambda, so I wrote a small library for eliminating some of that, 
as well as providing a pattern for writing testable, Spring-enabled lambda functions.&lt;/p&gt;

&lt;p&gt;Enter &lt;a href=&quot;https://github.com/ccampo133/spring-aws-lambda&quot;&gt;Spring AWS Lambda&lt;/a&gt; (clever title, I know!). The general gist here is that this library 
provides a few abstract classes which build on top of the AWS Lambda Java API’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RequestHandler&lt;/code&gt; interfaces. Instead 
of implementing AWS’s interfaces directly, you extend one of these new abstract classes and voila!, you can now use 
Spring’s dependency injection with your Lambda functions.&lt;/p&gt;

&lt;p&gt;The meat of the &lt;a href=&quot;https://github.com/ccampo133/spring-aws-lambda&quot;&gt;Spring AWS Lambda&lt;/a&gt; library is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SpringRequestHandler&lt;/code&gt; class. Basically, 
instead of implementing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RequestHandler&lt;/code&gt; directly, you extend this class and provide it with a 
Spring &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ApplicationContext&lt;/code&gt;. Then you can implement as many &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RequestHandler&lt;/code&gt;s as you want and provide injectable 
dependencies to them. See the library source code for more details, but examples are worth more than words, so here’s a 
quick one (NOTE: the rest of this post assumes a working knowledge of how Spring dependency injection works):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MainHandler&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SpringRequestHandler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Response&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;cm&quot;&gt;/**
     * Here we create the Spring ApplicationContext that will
     * be used throughout our application.
     */&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ApplicationContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;AnnotationConfigApplicationContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;ApplicationConfiguration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ApplicationContext&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getApplicationContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MainHandler&lt;/code&gt; is the entry point to your application. On the AWS Lambda configuration console, you actually set this 
as your “Handler” (see screenshot below). Note that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MainHandler&lt;/code&gt; will rarely have any logic itself. Instead, you 
simply tell it the request and response types via the generic type parameters (these can be anything you want), and 
implement the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getApplicationContext&lt;/code&gt; abstract method, which should provide a reference to your application’s main 
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ApplicationContext&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/lambda-conf.png&quot; title=&quot;AWS Lambda console&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If you look at the guts of &lt;a href=&quot;https://github.com/ccampo133/spring-aws-lambda/blob/master/spring-aws-lambda/src/main/java/me/ccampo/spring/aws/lambda/SpringRequestHandler.java&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SpringRequestHandler&lt;/code&gt;&lt;/a&gt;, you’ll notice that it actually 
implements the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RequestHandler&lt;/code&gt; class itself and provides a default &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;handleRequest&lt;/code&gt; method implementation, that 
actually retrieves a Spring bean of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RequestHandler&lt;/code&gt; and calls that bean’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;handleRequest&lt;/code&gt; method. You are free 
to override this method yourself if you want to add any additional logic, but that’s the general gist of it.&lt;/p&gt;

&lt;p&gt;More on that - since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SpringRequestHandler&lt;/code&gt; gets a bean of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RequestHandler&lt;/code&gt; and calls that bean’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;handleRequest&lt;/code&gt; 
method, that means that all of your main application logic should go in your own implementation of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RequestHandler&lt;/code&gt;, 
and this class should be declared a bean.&lt;/p&gt;

&lt;p&gt;This might seem not much different than the default AWS programming model. Well you’re right - it’s not. In fact, 
it’s almost exactly the same. The key difference here though is that now any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RequestHandler&lt;/code&gt; you implement yourself 
can take advantage of using Spring’s annotations and dependency injection. All you have to do is register it as a bean.&lt;/p&gt;

&lt;p&gt;Here’s an example of your own Spring-enabled request handler:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nd&quot;&gt;@Component&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ExampleRequestHandler&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RequestHandler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Response&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;cm&quot;&gt;/*
     * These can be anything you want; if they&apos;re registered as 
     * Spring beans, they&apos;ll be injected below via autowiring.
     */&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ExampleServiceA&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exampleServiceA&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ExampleServiceB&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exampleServiceB&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;cm&quot;&gt;/**
     * Dependency injection is handled via autowiring!
     */&lt;/span&gt;
    &lt;span class=&quot;nd&quot;&gt;@Autowired&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ExampleRequestHandler&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ExampleServiceA&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exampleServiceA&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ExampleServiceB&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exampleServiceB&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;exampleServiceA&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Objects&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;requireNonNull&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exampleServiceA&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;exampleServiceA&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;exampleServiceB&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Objects&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;requireNonNull&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exampleServiceB&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;exampleServiceB&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Response&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;handleRequest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Request&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Context&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;responseMessage&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Request message: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;, Service A message: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exampleServiceA&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;, Service B message: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exampleServiceB&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Response&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Response&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;responseMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setStatus&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Response&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Status&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;OK&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Note that we declare this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RequestHandler&lt;/code&gt; as a bean by use of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Component&lt;/code&gt; annotation. When your AWS Lambda 
function is run, it will actually invoke the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;handleRequest&lt;/code&gt; method of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExampleRequestHandler&lt;/code&gt; (via the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MainHandler&lt;/code&gt;
proxy).&lt;/p&gt;

&lt;p&gt;The key to take away here is that in our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExampleRequestHandler&lt;/code&gt; class, we are now able to take advantage of using 
Spring for dependency injection. This means we can inject dependencies, like services, DAOs, etc., via the 
constructor. This is huge for testability! Since AWS Lambda requires that your handler class have a default, no-arg 
constructor, dependencies would be typically hardcoded and tough to mock or stub in a testing environment. Now, we 
have a logic-less &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MainHandler&lt;/code&gt; class which we pass to AWS Lambda’s console, and a fully 
testable/mockable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RequestHandler&lt;/code&gt; implementation!&lt;/p&gt;

&lt;p&gt;For more information, and a more complete example, please see the docs and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;example&lt;/code&gt; module on &lt;a href=&quot;https://github.com/ccampo133/spring-aws-lambda&quot;&gt;Spring AWS Lambda’s 
Github page&lt;/a&gt;. I hope this helps you enjoy using AWS Lambda even more!&lt;/p&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;I really prefer Kotlin, since the Lambda runtime is really just the JVM and Kotlin works great with Lambda! &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Sun, 27 Nov 2016 15:00:00 +0000</pubDate>
        <link>https://www.ccampo.me/java/spring/aws/2016/11/27/spring-aws-lambda.html</link>
        <guid isPermaLink="true">https://www.ccampo.me/java/spring/aws/2016/11/27/spring-aws-lambda.html</guid>
        
        
        <category>java</category>
        
        <category>spring</category>
        
        <category>aws</category>
        
      </item>
    
  </channel>
</rss>
