Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
nss-pam-webapi
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
magicfelix
nss-pam-webapi
Commits
933870cd
Verified
Commit
933870cd
authored
3 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Patches
Plain Diff
[Cache] Use RwLock instead of Mutex
parent
56728236
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
src/cache.rs
+10
-10
10 additions, 10 deletions
src/cache.rs
with
10 additions
and
10 deletions
src/cache.rs
+
10
−
10
View file @
933870cd
...
...
@@ -17,7 +17,7 @@ use crate::BASE_NAME;
use
lazy_static
::
lazy_static
;
use
std
::
collections
::
HashMap
;
use
std
::
sync
::{
Mutex
,
Mutex
Guard
};
use
std
::
sync
::{
RwLock
,
RwLockRead
Guard
};
use
libc
::{
geteuid
,
seteuid
,
getpwnam
,
uid_t
};
use
std
::
ffi
::{
CStr
,
CString
};
...
...
@@ -144,7 +144,7 @@ impl Cache {
}
}
pub
fn
load_user_token
(
&
mut
self
,
owner
:
&
String
)
->
Option
<&
BasicTokenResponse
>
{
pub
fn
load_user_token
(
&
self
,
owner
:
&
String
)
->
Option
<&
BasicTokenResponse
>
{
if
!
self
.user_tokens
.contains_key
(
owner
)
{
debug!
(
"No token for {} in memory, trying to load from file"
,
owner
);
...
...
@@ -160,7 +160,7 @@ impl Cache {
match
new_token
{
Some
(
t
)
=>
{
self
.user_tokens
.insert
(
owner
.to_string
(),
t
);
CACHE
.write
()
.unwrap
()
.user_tokens
.insert
(
owner
.to_string
(),
t
);
self
.user_tokens
.get
(
owner
)
},
None
=>
None
...
...
@@ -171,8 +171,8 @@ impl Cache {
}
}
pub
fn
save_user_token
(
&
mut
self
,
owner
:
&
String
,
token
:
BasicTokenResponse
)
->
Result
<
(),
io
::
Error
>
{
self
.user_tokens
.insert
(
owner
.to_string
(),
token
.clone
());
pub
fn
save_user_token
(
&
self
,
owner
:
&
String
,
token
:
BasicTokenResponse
)
->
Result
<
(),
io
::
Error
>
{
CACHE
.write
()
.unwrap
()
.user_tokens
.insert
(
owner
.to_string
(),
token
.clone
());
debug!
(
"Saved token for {} in memory"
,
owner
);
// Try to write user's token cache file
...
...
@@ -190,8 +190,8 @@ impl Cache {
return
res
;
}
pub
fn
delete_user_token
(
&
mut
self
,
owner
:
&
String
)
{
self
.user_tokens
.remove
(
owner
);
pub
fn
delete_user_token
(
&
self
,
owner
:
&
String
)
{
CACHE
.write
()
.unwrap
()
.user_tokens
.remove
(
owner
);
debug!
(
"Token for {} removed from memory"
,
owner
);
// Try to remove user's token cache file
...
...
@@ -227,9 +227,9 @@ fn save_json<O: Serialize>(path: PathBuf, obj: O) -> Result<(), io::Error> {
}
lazy_static!
{
static
ref
CACHE
:
Mutex
<
Cache
>
=
Mutex
::
new
(
Cache
::
new
());
static
ref
CACHE
:
RwLock
<
Cache
>
=
RwLock
::
new
(
Cache
::
new
());
}
pub
fn
get_cache
()
->
Mutex
Guard
<
'static
,
Cache
>
{
CACHE
.
lock
()
.unwrap
()
pub
fn
get_cache
()
->
RwLockRead
Guard
<
'static
,
Cache
>
{
CACHE
.
read
()
.unwrap
()
}
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