This assignment is based on sample code from Eli Bendersky. He has a blog post that...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
This assignment is based on sample code from Eli Bendersky. He has a blog post that discusses it: https://eli.thegreenplace.net/2019/on-concurrency-in-go-http-servers/ and the code is found on github: https://github.com/eliben/code-for-blog/blob/master/2019/gohttpconcurrency/mutex-se rver.co On github, you can click on the Raw link to see the plaintext of the code. I'll mention several of the points he makes in the following. In this assignment, you will: Here are the requirements: Get a deeper understanding of interacting with web services Get practice with synchronizing multiple threads First, build and run the existing code. Note: the default port number used here is 8000. A student had a problem on the previous assignment where some setting on his computer was blocking attempts to use 8081 as a port number and would only accept 8080, so if things don't run, try changing 8000 to 8080. 1. I think the most important takeaway from Bendersky's blog is that even though you do not see any goroutines here, a web server is running things concurrently so that multiple users could interact with the server concurrently and not get bogged down waiting their turn. 2. An interesting thing to note is that the sync.Mutex field of Counter Store is just by itself. This is a feature of Go that lets you do composition conveniently. Ordinarily, you would define a field of type sync.Mutex, but every time you wanted to call Lock() or Unlock(), you would need to write something like "cs.field.Lock()". The existing syntax lets Benderskx write "cs.Lock()", so it's as if Counter Store has inherited the functionality of Mutex but without the potential problems that we discussed for inheritance of implementation. 3. There is also a nice use of the defer command here to ensure that the lock is released in all cases after one of the functions that affects the counter is done. Now modify the code so that it manages Account's for a payment processor (a bit like Paypal). We'll say a bit more about the more likely approach that such a program would take, but this is to motivate the concepts of synchronization. 1. An Account is a struct with an owner(string) and balance(int) 2. Using *Account as a target, add methods to get the balance, set the balance and modify the balance 3. Update the rest of the code so that Counter Store is now Accountstore and maps strings to *Account's. The strings become the account ID's One thing we note is that there is just one lock for the whole map. This is ok for the simple example being presented, but it does block one user from incrementing pointer A when a second user is incrementing pointer B, even though the two pointers are not related. 1. Move the Mutex to the Account structure 2. Note that we cannot use the composition feature anymore. That would imply there is only one Mutex for all accounts and we would not be gaining any greater concurrency. Create a named field of type sync.Mutex Add support for a /pay endpoint, 1. The (required) parameters are: 1. from (string): account ID of account where money will be taken out 2. to (string): account ID of account where money will be deposited amt(int): amount to transfer 1. Protect this transaction by locking the two accounts and unlocking them appropriately. Note that if you forgot to name the Mutex field, you will not be able to execute the transfer since the code will try to lock the same lock twice. 2. As a minimal error handling requirement, make sure that there is enough money in the "from" account to make the payment. Produce an error message saying so if not. 3. Once you get this all working, you can go to a browser and enter a URL like the following to make a payment http://localhost:8000/pay?from=A&to=B&amt=20 Submission: It's just one.go file. This assignment is based on sample code from Eli Bendersky. He has a blog post that discusses it: https://eli.thegreenplace.net/2019/on-concurrency-in-go-http-servers/ and the code is found on github: https://github.com/eliben/code-for-blog/blob/master/2019/gohttpconcurrency/mutex-se rver.co On github, you can click on the Raw link to see the plaintext of the code. I'll mention several of the points he makes in the following. In this assignment, you will: Here are the requirements: Get a deeper understanding of interacting with web services Get practice with synchronizing multiple threads First, build and run the existing code. Note: the default port number used here is 8000. A student had a problem on the previous assignment where some setting on his computer was blocking attempts to use 8081 as a port number and would only accept 8080, so if things don't run, try changing 8000 to 8080. 1. I think the most important takeaway from Bendersky's blog is that even though you do not see any goroutines here, a web server is running things concurrently so that multiple users could interact with the server concurrently and not get bogged down waiting their turn. 2. An interesting thing to note is that the sync.Mutex field of Counter Store is just by itself. This is a feature of Go that lets you do composition conveniently. Ordinarily, you would define a field of type sync.Mutex, but every time you wanted to call Lock() or Unlock(), you would need to write something like "cs.field.Lock()". The existing syntax lets Benderskx write "cs.Lock()", so it's as if Counter Store has inherited the functionality of Mutex but without the potential problems that we discussed for inheritance of implementation. 3. There is also a nice use of the defer command here to ensure that the lock is released in all cases after one of the functions that affects the counter is done. Now modify the code so that it manages Account's for a payment processor (a bit like Paypal). We'll say a bit more about the more likely approach that such a program would take, but this is to motivate the concepts of synchronization. 1. An Account is a struct with an owner(string) and balance(int) 2. Using *Account as a target, add methods to get the balance, set the balance and modify the balance 3. Update the rest of the code so that Counter Store is now Accountstore and maps strings to *Account's. The strings become the account ID's One thing we note is that there is just one lock for the whole map. This is ok for the simple example being presented, but it does block one user from incrementing pointer A when a second user is incrementing pointer B, even though the two pointers are not related. 1. Move the Mutex to the Account structure 2. Note that we cannot use the composition feature anymore. That would imply there is only one Mutex for all accounts and we would not be gaining any greater concurrency. Create a named field of type sync.Mutex Add support for a /pay endpoint, 1. The (required) parameters are: 1. from (string): account ID of account where money will be taken out 2. to (string): account ID of account where money will be deposited amt(int): amount to transfer 1. Protect this transaction by locking the two accounts and unlocking them appropriately. Note that if you forgot to name the Mutex field, you will not be able to execute the transfer since the code will try to lock the same lock twice. 2. As a minimal error handling requirement, make sure that there is enough money in the "from" account to make the payment. Produce an error message saying so if not. 3. Once you get this all working, you can go to a browser and enter a URL like the following to make a payment http://localhost:8000/pay?from=A&to=B&amt=20 Submission: It's just one.go file.
Expert Answer:
Related Book For
Intermediate Accounting Volume 2
ISBN: 9781260881240
8th Edition
Authors: Thomas H. Beechy, Joan E. Conrod, Elizabeth Farrell, Ingrid McLeod-Dick, Kayla Tomulka, Romi-Lee Sevel
Posted Date:
Students also viewed these programming questions
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
A data breach represents a security incident that occurs when "sensitive, protected or confidential data has potentially been viewed, stolen, or used by an individual unauthorized to do so."3...
-
Give the order of magnitude () for the following algorithm. Explain why your answer is correct. GET VALUES for A1, A2, ..., An, and B, B2, ..., Ba Get value of n /* set i equal to 1 */ /* for each of...
-
Suppose that we have identified three important systematic risk factors given by exports, inflation, and industrial production. In the beginning of the year, growth in these three factors is...
-
In the ray diagram of Figure, where the angles are not drawn to scale, the ray is incident at the critical angle on the interface between materials 2 and 3. Angle = 60.0?, and two of the indexes of...
-
Draw an energy diagram for the situation presented in Exercise 9.4, but choose the system that comprises block, spring, surface, and Earth. Data from Exercises 9.4 A block initially at rest is...
-
Ultimate Butter Popcorn issues 7%, 15-year bonds with a face amount of $60,000. The market interest rate for bonds of similar risk and maturity is 8%. Interest is paid semiannually. At what price...
-
15. 16. What is the final product (major) 'A' in the given reaction? CH3 OH CH CH3 HCI + 'A' (major product) CH3 CH3 Cl CH (B) CH3 CH2-CH3 (A) CH3 CH (C) shiks CH=CH2 (D) Which of the following...
-
Southland Industries has $60,000 of 5.2% ?(annual interest) bonds?outstanding, 1,800 shares of preferred stock paying an annual dividend of $3 per?share, and 4,000 shares of common stock outstanding....
-
For the datapath from Figure 4.24, draw the logic diagram for the part of the control unit that implements just the first signal. Assume that we only need to support LW, SW, BEQ, ADD, and J (jump)...
-
Thumbtacks capital structure is shown in table below. If taxes are paid annually and Thumbtacks combined tax rate is 36 percent, determine the weighted average cost of capital. Loans Bonds Common...
-
A souvenir shop makes the following forecast for one years sales and costs. Required Prepare tables showing the sensitivity of the profit forecast to each of the following: (a) a 1 percent change in...
-
The following transactions relate to a dairy, converting milk to cheese, for the month of May. Prepare ledger accounts which record the transactions. 1 May Bought 600 drums of milk from supplier on...
-
Alternatives 1, 2, and 3 have lives of 3, 4, and 6 years, respectively. Their net cash flow (NCF) and salvage value (SV) profiles are as follows: Additional explanation is necessary: the NCF profile...
-
Researchers call a measure valid to the extent that it _________________________. Select one: a. has high face validity, but low content validity. b. is easy to administer. c. has steps that are...
-
F.(3e* -2x 3 sin(2x)) is equal to 2 3 Cos 8. IT 3, t (4+@ 2 3, 1+o 1 4 Cos 4 4 1 3. 1 +4cos V7 (1+o 4 1 4 Cos 4 1+0 4-
-
Moon Ltd. reported the following: Required: 1. What is the amount of the taxable income or loss in each year? 2. How much is the tax refund to be claimed in 20X8? 3. What is the amount of the loss...
-
Halton Corp. reported pre-tax earnings from operations in 20X7 of $120,000 (the first year of operations). In 20X8, the corporation experienced a $70,000 pre-tax loss from operations. Future...
-
The following information has been provided for Relink Corporation for 20X2-20X5: Required: 1. What is the tax basis of the capital assets in each year? 2. What is the tax basis of the development...
-
Given the four criteria necessary for a sale to be complete, which of the following is not one of those conditions? 1. Delivery has occurred or services rendered. 2. Cash has been collected. 3. The...
-
What is the difference between a business and a pure charity? Between a business and a governmental agency?
-
What is an integrated delivery system?
Study smarter with the SolutionInn App