Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MyAktion Go-ss2023
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
go-exercises
MyAktion Go-ss2023
Commits
00febf8b
Commit
00febf8b
authored
1 year ago
by
Martin Schmollinger
Browse files
Options
Downloads
Patches
Plain Diff
Banktransfer Service Part 2: Bank Transaction Processing
parent
999c1eb0
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/banktransfer/service/banktransfer.go
+25
-9
25 additions, 9 deletions
src/banktransfer/service/banktransfer.go
src/myaktion/monitor.go
+8
-1
8 additions, 1 deletion
src/myaktion/monitor.go
src/myaktion/service/donation.go
+20
-0
20 additions, 0 deletions
src/myaktion/service/donation.go
with
53 additions
and
10 deletions
src/banktransfer/service/banktransfer.go
+
25
−
9
View file @
00febf8b
...
...
@@ -2,6 +2,8 @@ package service
import
(
"context"
"math/rand"
"sync/atomic"
"time"
log
"github.com/sirupsen/logrus"
...
...
@@ -12,46 +14,60 @@ import (
type
BankTransferService
struct
{
banktransfer
.
BankTransferServer
counter
int32
queue
chan
banktransfer
.
Transaction
}
func
NewBankTransferService
()
*
BankTransferService
{
return
&
BankTransferService
{
counter
:
1
}
rand
.
Seed
(
time
.
Now
()
.
UnixNano
())
return
&
BankTransferService
{
counter
:
1
,
queue
:
make
(
chan
banktransfer
.
Transaction
)}
}
func
(
s
*
BankTransferService
)
TransferMoney
(
_
context
.
Context
,
transaction
*
banktransfer
.
Transaction
)
(
*
emptypb
.
Empty
,
error
)
{
log
.
Infof
(
"-------> Received transaction <-------: %v"
,
transaction
)
entry
:=
log
.
WithField
(
"transaction"
,
transaction
)
entry
.
Info
((
"Received transaction"
))
s
.
processTransaction
(
transaction
)
return
&
emptypb
.
Empty
{},
nil
}
func
(
s
*
BankTransferService
)
processTransaction
(
transaction
*
banktransfer
.
Transaction
)
{
entry
:=
log
.
WithField
(
"transaction"
,
transaction
)
go
func
(
transaction
banktransfer
.
Transaction
)
{
entry
.
Info
(
"Start processing transaction"
)
transaction
.
Id
=
atomic
.
AddInt32
(
&
s
.
counter
,
1
)
time
.
Sleep
(
time
.
Duration
(
rand
.
Intn
(
9
)
+
1
)
*
time
.
Second
)
s
.
queue
<-
transaction
entry
.
Info
(
"Processing transaction finished"
)
}(
*
transaction
)
}
func
(
s
*
BankTransferService
)
ProcessTransactions
(
stream
banktransfer
.
BankTransfer_ProcessTransactionsServer
)
error
{
ticker
:=
time
.
NewTicker
(
2
*
time
.
Second
)
defer
ticker
.
Stop
()
return
func
()
error
{
for
{
select
{
case
<-
stream
.
Context
()
.
Done
()
:
log
.
Info
(
"Watching transactions cancelled from the client side"
)
return
nil
case
<-
ticker
.
C
:
transaction
:=
&
banktransfer
.
T
ransaction
{
Id
:
s
.
counter
,
Amount
:
20
}
case
transaction
:=
<-
s
.
queue
:
id
:=
t
ransaction
.
Id
entry
:=
log
.
WithField
(
"transaction"
,
transaction
)
entry
.
Info
(
"Sending transaction"
)
if
err
:=
stream
.
Send
(
transaction
);
err
!=
nil
{
if
err
:=
stream
.
Send
(
&
transaction
);
err
!=
nil
{
// TODO: we need to recover from this if communication is not up
entry
.
WithError
(
err
)
.
Error
(
"Error sending transaction"
)
return
err
}
entry
.
Info
(
"Transaction sent. Waiting for processing response"
)
response
,
err
:=
stream
.
Recv
()
if
err
!=
nil
{
// TODO: we need to recover from this if communication is not up
entry
.
WithError
(
err
)
.
Error
(
"Error receiving processing response"
)
return
err
}
if
response
.
Id
!=
s
.
counter
{
if
response
.
Id
!=
id
{
// NOTE: this is just a guard and not happening as transaction is local per connection
entry
.
Error
(
"Received processing response of a different transaction"
)
}
else
{
entry
.
Info
(
"Processing response received"
)
s
.
counter
++
}
}
}
...
...
This diff is collapsed.
Click to expand it.
src/myaktion/monitor.go
+
8
−
1
View file @
00febf8b
...
...
@@ -7,6 +7,7 @@ import (
log
"github.com/sirupsen/logrus"
"gitlab.reutlingen-university.de/go-exercises/myaktion-go-ss2023/src/myaktion/client"
"gitlab.reutlingen-university.de/go-exercises/myaktion-go-ss2023/src/myaktion/client/banktransfer"
"gitlab.reutlingen-university.de/go-exercises/myaktion-go-ss2023/src/myaktion/service"
)
func
monitortransactions
()
{
...
...
@@ -42,7 +43,13 @@ func connectandmonitor() {
continue
}
entry
:=
log
.
WithField
(
"transaction"
,
transaction
)
entry
.
Info
(
"Received transaction. Sending processing response"
)
entry
.
Info
(
"Received transaction"
)
err
=
service
.
MarkDonation
(
uint
(
transaction
.
DonationId
))
if
err
!=
nil
{
entry
.
WithError
(
err
)
.
Error
(
"error changing donation status"
)
continue
}
entry
.
Info
(
"Sending processing response"
)
err
=
watcher
.
Send
(
&
banktransfer
.
ProcessingResponse
{
Id
:
transaction
.
Id
})
if
err
!=
nil
{
entry
.
WithError
(
err
)
.
Error
(
"error sending processing response"
)
...
...
This diff is collapsed.
Click to expand it.
src/myaktion/service/donation.go
+
20
−
0
View file @
00febf8b
...
...
@@ -74,3 +74,23 @@ func deleteDonation(donation *model.Donation) error {
entry
.
Info
(
"Successfully deleted campaign."
)
return
nil
}
func
MarkDonation
(
id
uint
)
error
{
entry
:=
log
.
WithField
(
"donationId"
,
id
)
donation
:=
new
(
model
.
Donation
)
result
:=
db
.
DB
.
First
(
donation
,
id
)
if
result
.
Error
!=
nil
{
entry
.
WithError
(
result
.
Error
)
.
Error
(
"Can't retrieve donation"
)
return
result
.
Error
}
entry
=
entry
.
WithField
(
"donation"
,
donation
)
entry
.
Trace
(
"Retrieved donation"
)
donation
.
Status
=
model
.
TRANSFERRED
result
=
db
.
DB
.
Save
(
donation
)
if
result
.
Error
!=
nil
{
entry
.
WithError
(
result
.
Error
)
.
Error
(
"Can't update donation"
)
return
result
.
Error
}
entry
.
Info
(
"Successfully updated status of donation."
)
return
nil
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment