Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ImageViewerAPI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
dylan.frei
ImageViewerAPI
Commits
b24b1e07
Commit
b24b1e07
authored
2 years ago
by
dylan.frei
Browse files
Options
Downloads
Patches
Plain Diff
Added mutex for concurrency
parent
6d82d7b1
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
api.go
+18
-3
18 additions, 3 deletions
api.go
with
18 additions
and
3 deletions
api.go
+
18
−
3
View file @
b24b1e07
...
@@ -18,9 +18,13 @@ import (
...
@@ -18,9 +18,13 @@ import (
"path/filepath"
"path/filepath"
"strconv"
"strconv"
"strings"
"strings"
"sync"
"syscall"
"syscall"
"time"
)
)
var
m
sync
.
Mutex
func
main
()
{
func
main
()
{
if
_
,
err
:=
os
.
Stat
(
"/var/lib/image-viewer"
);
err
!=
nil
{
if
_
,
err
:=
os
.
Stat
(
"/var/lib/image-viewer"
);
err
!=
nil
{
...
@@ -86,9 +90,9 @@ func Stop(c echo.Context) error {
...
@@ -86,9 +90,9 @@ func Stop(c echo.Context) error {
}
}
func
Upload
(
c
echo
.
Context
)
error
{
func
Upload
(
c
echo
.
Context
)
error
{
//
-----------
//
protect ressources
// Read file
m
.
Lock
()
//-----------
defer
m
.
Unlock
()
// Source
// Source
file
,
err
:=
c
.
FormFile
(
"file"
)
file
,
err
:=
c
.
FormFile
(
"file"
)
...
@@ -139,6 +143,10 @@ func List(c echo.Context) error {
...
@@ -139,6 +143,10 @@ func List(c echo.Context) error {
}
}
func
Del
(
c
echo
.
Context
)
error
{
func
Del
(
c
echo
.
Context
)
error
{
//protect ressources
m
.
Lock
()
defer
m
.
Unlock
()
// Removing file from the directory
// Removing file from the directory
// Using Remove() function
// Using Remove() function
img
:=
c
.
Param
(
"img"
)
img
:=
c
.
Param
(
"img"
)
...
@@ -150,6 +158,9 @@ func Del(c echo.Context) error {
...
@@ -150,6 +158,9 @@ func Del(c echo.Context) error {
}
}
func
Display
(
c
echo
.
Context
)
error
{
func
Display
(
c
echo
.
Context
)
error
{
//protect ressources
m
.
Lock
()
defer
m
.
Unlock
()
//Check if program is running
//Check if program is running
if
_
,
err
:=
os
.
Stat
(
"/var/run/image-viewer.pid"
);
err
!=
nil
{
if
_
,
err
:=
os
.
Stat
(
"/var/run/image-viewer.pid"
);
err
!=
nil
{
...
@@ -187,6 +198,10 @@ func Display(c echo.Context) error {
...
@@ -187,6 +198,10 @@ func Display(c echo.Context) error {
}
}
syscall
.
Kill
(
pid
,
syscall
.
SIGUSR1
)
syscall
.
Kill
(
pid
,
syscall
.
SIGUSR1
)
//Ensure image has the time to be displayed before mutex release
//In case a subsequent call to display would be made before the signal is processed by the backend,
//the data from current.ppm could be corrupted (or too long) during process.
time
.
Sleep
(
500
*
time
.
Millisecond
)
return
c
.
String
(
http
.
StatusOK
,
img
+
" is being displayed
\n
"
)
return
c
.
String
(
http
.
StatusOK
,
img
+
" is being displayed
\n
"
)
}
}
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