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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
go-exercises
MyAktion Go-ss2023
Commits
6aeff073
Commit
6aeff073
authored
May 10, 2023
by
Martin Schmollinger
Browse files
Options
Downloads
Patches
Plain Diff
Banktransfer Service Part 3: Simple Retry-Mechanism
parent
00febf8b
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/banktransfer/main.go
+3
-0
3 additions, 0 deletions
src/banktransfer/main.go
src/banktransfer/service/banktransfer.go
+47
-6
47 additions, 6 deletions
src/banktransfer/service/banktransfer.go
with
50 additions
and
6 deletions
src/banktransfer/main.go
+
3
−
0
View file @
6aeff073
...
...
@@ -33,6 +33,9 @@ func main() {
log
.
Fatalf
(
"failed to listen on grpc port %d: %v"
,
grpcPort
,
err
)
}
grpcServer
:=
grpc
.
NewServer
()
transferService
:=
service
.
NewBankTransferService
()
transferService
.
Start
()
defer
transferService
.
Stop
()
banktransfer
.
RegisterBankTransferServer
(
grpcServer
,
service
.
NewBankTransferService
())
if
err
:=
grpcServer
.
Serve
(
lis
);
err
!=
nil
{
log
.
Fatalf
(
"failed to serve: %v"
,
err
)
...
...
This diff is collapsed.
Click to expand it.
src/banktransfer/service/banktransfer.go
+
47
−
6
View file @
6aeff073
...
...
@@ -11,15 +11,23 @@ import (
"google.golang.org/protobuf/types/known/emptypb"
)
const
retryTime
=
5
*
time
.
Second
type
BankTransferService
struct
{
banktransfer
.
BankTransferServer
counter
int32
queue
chan
banktransfer
.
Transaction
retryQueue
chan
banktransfer
.
Transaction
stop
chan
struct
{}
}
func
NewBankTransferService
()
*
BankTransferService
{
rand
.
Seed
(
time
.
Now
()
.
UnixNano
())
return
&
BankTransferService
{
counter
:
1
,
queue
:
make
(
chan
banktransfer
.
Transaction
)}
return
&
BankTransferService
{
counter
:
0
,
queue
:
make
(
chan
banktransfer
.
Transaction
),
retryQueue
:
make
(
chan
banktransfer
.
Transaction
),
stop
:
make
(
chan
struct
{}),
}
}
func
(
s
*
BankTransferService
)
TransferMoney
(
_
context
.
Context
,
transaction
*
banktransfer
.
Transaction
)
(
*
emptypb
.
Empty
,
error
)
{
...
...
@@ -33,13 +41,17 @@ func (s *BankTransferService) processTransaction(transaction *banktransfer.Trans
entry
:=
log
.
WithField
(
"transaction"
,
transaction
)
go
func
(
transaction
banktransfer
.
Transaction
)
{
entry
.
Info
(
"Start processing transaction"
)
transaction
.
Id
=
atomic
.
AddInt32
(
&
s
.
counter
,
1
)
transaction
.
Id
=
s
.
getUniqueId
(
)
time
.
Sleep
(
time
.
Duration
(
rand
.
Intn
(
9
)
+
1
)
*
time
.
Second
)
s
.
queue
<-
transaction
entry
.
Info
(
"Processing transaction finished"
)
}(
*
transaction
)
}
func
(
s
*
BankTransferService
)
getUniqueId
()
int32
{
return
atomic
.
AddInt32
(
&
s
.
counter
,
1
)
}
func
(
s
*
BankTransferService
)
ProcessTransactions
(
stream
banktransfer
.
BankTransfer_ProcessTransactionsServer
)
error
{
return
func
()
error
{
for
{
...
...
@@ -52,14 +64,14 @@ func (s *BankTransferService) ProcessTransactions(stream banktransfer.BankTransf
entry
:=
log
.
WithField
(
"transaction"
,
transaction
)
entry
.
Info
(
"Sending transaction"
)
if
err
:=
stream
.
Send
(
&
transaction
);
err
!=
nil
{
// TODO: we need to recover from this if communication is not up
s
.
requeueTransaction
(
&
transaction
)
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
s
.
requeueTransaction
(
&
transaction
)
entry
.
WithError
(
err
)
.
Error
(
"Error receiving processing response"
)
return
err
}
...
...
@@ -73,3 +85,32 @@ func (s *BankTransferService) ProcessTransactions(stream banktransfer.BankTransf
}
}()
}
func
(
s
*
BankTransferService
)
requeueTransaction
(
transaction
*
banktransfer
.
Transaction
)
{
entry
:=
log
.
WithField
(
"transaction"
,
transaction
)
go
func
(
transaction
banktransfer
.
Transaction
)
{
entry
.
Infof
(
"Requeuing transaction. Wait for %f seconds"
,
retryTime
.
Seconds
())
time
.
Sleep
(
retryTime
)
s
.
retryQueue
<-
transaction
entry
.
Info
(
"Requeued transaction"
)
}(
*
transaction
)
}
func
(
s
*
BankTransferService
)
Start
()
{
log
.
Info
(
"Starting banktransfer service"
)
go
func
()
{
for
{
select
{
case
<-
s
.
stop
:
break
case
transaction
:=
<-
s
.
retryQueue
:
s
.
queue
<-
transaction
}
}
}()
}
func
(
s
*
BankTransferService
)
Stop
()
{
log
.
Info
(
"Stopping banktransfer service"
)
close
(
s
.
stop
)
}
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
sign in
to comment