aws-samples / serverless-rust-demo

Sample serverless application written in Rust
MIT No Attribution
271 stars 22 forks source link

feat: implement fine-grained traits #21

Closed nmoutschen closed 3 years ago

nmoutschen commented 3 years ago

This pull request implements fine-grained traits for the products::store port.

Current situation

The current implementation uses a single trait for products::store::Store, which is then stored in a Box<dyn Store> in a products::CrudService. Because of that, all Lambda functions behind API Gateway have references to all the functions implemented by the Store trait.

By running make build && grep 'Scanning DynamoDB table' build/*/bootstrap on the current main branch, I get the following results:

Binary file build/delete-product/bootstrap matches
Binary file build/get-product/bootstrap matches
Binary file build/get-products/bootstrap matches
Binary file build/put-product/bootstrap matches

That means that whenever there is a code change for the all() function for DynamoDBStore, all four Lambda functions will be updated.

New implementation

By running the same command, I only get a single match from the Lambda functions, meaning that the other ones no longer have code for functions that will never be called.

Binary file build/get-products/bootstrap matches

I can confirm that functions are the same by comparing the hash of all binaries after a small code change on products::store::dynamodb::DynamoDBStore's implementation of all() (using sha256sum build/*/bootstrap after a make build).

Before:

6aee8a97d7a5a5abffb9e25f115044bbb332106e17b8c3f0b40b50e09d3e9c71  build/delete-product/bootstrap
732583b13f6dfe276cbffdad3ce0d0348652d809b2bb2168d9e8f0eb455aa217  build/dynamodb-streams/bootstrap
0eef074bcbd7aeac62869cea4f244a0743f8b73eb9df1ecafea97d28354712a5  build/get-product/bootstrap
d5fce4d479ff96ed732ca1f1315e5b5d17bb31562b664558bc8970a984f61737  build/get-products/bootstrap
7c13ffb616d4b4d4369f17aec806097afd0ae59f773675f253024adcfca2f7e1  build/put-product/bootstrap

After:

6aee8a97d7a5a5abffb9e25f115044bbb332106e17b8c3f0b40b50e09d3e9c71  build/delete-product/bootstrap
732583b13f6dfe276cbffdad3ce0d0348652d809b2bb2168d9e8f0eb455aa217  build/dynamodb-streams/bootstrap
0eef074bcbd7aeac62869cea4f244a0743f8b73eb9df1ecafea97d28354712a5  build/get-product/bootstrap
30bab1eaae8923f0b4c5732906a6872def7234d11d3f35f7f013393fc586879a  build/get-products/bootstrap
7c13ffb616d4b4d4369f17aec806097afd0ae59f773675f253024adcfca2f7e1  build/put-product/bootstrap

The only hash that changed is for build/get-products/bootstrap, as expected. Meaning that functions won't redeploy unless there is a code change that affected them.

As part of this change, I've also moved the domain logic layer to src/domain.rs. It's currently just functions until I implement a better interface, for example using extension traits.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.