@@ -22,7 +22,6 @@ package multithreading
2222import (
2323 "context"
2424 "fmt"
25- "runtime"
2625 "sync"
2726
2827 mpl "github.com/aws/aws-cryptographic-material-providers-library/mpl/awscryptographymaterialproviderssmithygenerated"
@@ -33,29 +32,19 @@ import (
3332 "github.com/aws/aws-sdk-go-v2/service/kms"
3433)
3534
36- // Structure to hold operation results
37- type OperationResult struct {
38- EncryptOutput * esdktypes.EncryptOutput
39- DecryptOutput * esdktypes.DecryptOutput
40- Error error
41- }
42-
4335// Function to handle encryption
4436func encryptData (
4537 ctx context.Context ,
4638 encryptionClient * client.Client ,
4739 plaintext string ,
4840 encryptionContext map [string ]string ,
49- keyring mpltypes.IKeyring ) OperationResult {
41+ keyring mpltypes.IKeyring ) ( * esdktypes. EncryptOutput , error ) {
5042 res , err := encryptionClient .Encrypt (ctx , esdktypes.EncryptInput {
5143 Plaintext : []byte (plaintext ),
5244 EncryptionContext : encryptionContext ,
5345 Keyring : keyring ,
5446 })
55- return OperationResult {
56- EncryptOutput : res ,
57- Error : err ,
58- }
47+ return res , err
5948}
6049
6150// Function to handle decryption
@@ -64,19 +53,17 @@ func decryptData(
6453 encryptionClient * client.Client ,
6554 ciphertext []byte ,
6655 encryptionContext map [string ]string ,
67- keyring mpltypes.IKeyring ) OperationResult {
56+ keyring mpltypes.IKeyring ) ( * esdktypes. DecryptOutput , error ) {
6857 res , err := encryptionClient .Decrypt (ctx , esdktypes.DecryptInput {
6958 EncryptionContext : encryptionContext ,
7059 Keyring : keyring ,
7160 Ciphertext : ciphertext ,
7261 })
73- return OperationResult {
74- DecryptOutput : res ,
75- Error : err ,
76- }
62+ return res , err
7763}
7864
7965func processEncryptionWorker (
66+ ctx context.Context ,
8067 wg * sync.WaitGroup ,
8168 jobs <- chan string ,
8269 encryptionClient * client.Client ,
@@ -85,36 +72,40 @@ func processEncryptionWorker(
8572) {
8673 defer wg .Done ()
8774 for plaintext := range jobs {
88- ctx := context .Background ()
8975 // Perform encryption
90- encryptResult := encryptData (ctx , encryptionClient , plaintext , encryptionContext , awsKmsKeyring )
91- if encryptResult .Error != nil {
92- panic (encryptResult .Error )
76+ encryptResult , err := encryptData (
77+ ctx ,
78+ encryptionClient ,
79+ plaintext ,
80+ encryptionContext ,
81+ awsKmsKeyring )
82+ if err != nil {
83+ panic (err )
9384 }
9485 // Verify ciphertext is different from plaintext
95- if string (encryptResult .EncryptOutput . Ciphertext ) == plaintext {
86+ if string (encryptResult .Ciphertext ) == plaintext {
9687 panic ("Ciphertext and Plaintext before encryption are the same" )
9788 }
9889 // Perform decryption
99- decryptResult := decryptData (
90+ decryptResult , err := decryptData (
10091 ctx ,
10192 encryptionClient ,
102- encryptResult .EncryptOutput . Ciphertext ,
93+ encryptResult .Ciphertext ,
10394 encryptionContext ,
10495 awsKmsKeyring ,
10596 )
106- if decryptResult . Error != nil {
107- panic (decryptResult . Error )
97+ if err != nil {
98+ panic (err )
10899 }
109100 // If you do not specify the encryption context on Decrypt, it's recommended to check if the resulting encryption context matches.
110101 // The encryption context was specified on decrypt; we are validating the encryption context for demonstration only.
111102 // Before your application uses plaintext data, verify that the encryption context that
112103 // you used to encrypt the message is included in the encryption context that was used to
113104 // decrypt the message. The AWS Encryption SDK can add pairs, so don't require an exact match.
114- if err := validateEncryptionContext (encryptionContext , decryptResult .DecryptOutput . EncryptionContext ); err != nil {
105+ if err := validateEncryptionContext (encryptionContext , decryptResult .EncryptionContext ); err != nil {
115106 panic (err )
116107 }
117- if string (decryptResult .DecryptOutput . Plaintext ) != plaintext {
108+ if string (decryptResult .Plaintext ) != plaintext {
118109 panic ("Plaintext after decryption and Plaintext before encryption are NOT the same" )
119110 }
120111 }
@@ -135,11 +126,12 @@ func AWSKMSMultiThreadTest(texts []string, defaultKmsKeyID, defaultKmsKeyRegion
135126 panic (err )
136127 }
137128 // Create the keyring
129+ ctx := context .Background ()
138130 awsKmsKeyringInput := mpltypes.CreateAwsKmsKeyringInput {
139131 KmsClient : kmsClient ,
140132 KmsKeyId : defaultKmsKeyID ,
141133 }
142- awsKmsKeyring , err := matProv .CreateAwsKmsKeyring (context . Background () , awsKmsKeyringInput )
134+ awsKmsKeyring , err := matProv .CreateAwsKmsKeyring (ctx , awsKmsKeyringInput )
143135 if err != nil {
144136 panic (err )
145137 }
@@ -163,7 +155,7 @@ func AWSKMSMultiThreadTest(texts []string, defaultKmsKeyID, defaultKmsKeyRegion
163155 "the data you are handling" : "is what you think it is" ,
164156 }
165157 // Create buffered channels to handle multiple operations
166- numWorkers := runtime . NumCPU () - 1 // Leave one CPU free for system tasks
158+ numWorkers := 10
167159
168160 // Create a wait group to track all goroutines
169161 var wg sync.WaitGroup
@@ -174,7 +166,7 @@ func AWSKMSMultiThreadTest(texts []string, defaultKmsKeyID, defaultKmsKeyRegion
174166 // Start worker pool
175167 for range numWorkers {
176168 wg .Add (1 )
177- go processEncryptionWorker (& wg , jobs , encryptionClient , awsKmsKeyring , encryptionContext )
169+ go processEncryptionWorker (ctx , & wg , jobs , encryptionClient , awsKmsKeyring , encryptionContext )
178170 }
179171
180172 // Send jobs to workers
0 commit comments