Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Synchronet
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
Container 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
Main
Synchronet
Commits
fb3415e6
Commit
fb3415e6
authored
6 years ago
by
echicken
Browse files
Options
Downloads
Patches
Plain Diff
File-based cache and rate-limiting.
parent
1285a9e7
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
exec/load/openweathermap.js
+39
-12
39 additions, 12 deletions
exec/load/openweathermap.js
with
39 additions
and
12 deletions
exec/load/openweathermap.js
+
39
−
12
View file @
fb3415e6
...
...
@@ -14,34 +14,61 @@ function OpenWeatherMap() {
if
(
!
this
.
settings
.
rate_window
)
this
.
settings
.
rate_window
=
60
;
// Seconds
if
(
!
this
.
settings
.
rate_limit
)
this
.
settings
.
rate_limit
=
60
;
// Requests per window
if
(
!
this
.
settings
.
data_refresh
)
this
.
settings
.
data_refresh
=
7200
;
// Seconds
this
.
cache
=
{}
;
this
.
r
equests
=
[]
;
this
.
cache
_file
=
new
File
(
system
.
data_dir
+
'
openweathermap_cache.json
'
)
;
this
.
r
ate_file
=
new
File
(
system
.
data_dir
+
'
openweathermap_rate.json
'
)
;
}
OpenWeatherMap
.
prototype
.
write_cache
=
function
(
endpoint
,
params
,
response
)
{
const
hash
=
base64_encode
(
endpoint
+
JSON
.
stringify
(
params
));
this
.
cache
[
hash
]
=
{
time
:
time
(),
data
:
response
};
if
(
!
this
.
cache_file
.
exists
)
{
var
cache
=
{};
cache
[
hash
]
=
{
time
:
time
(),
data
:
response
};
this
.
cache_file
.
open
(
'
w
'
);
this
.
cache_file
.
write
(
JSON
.
stringify
(
cache
));
this
.
cache_file
.
close
();
}
else
{
this
.
cache_file
.
open
(
'
r+
'
);
var
cache
=
JSON
.
parse
(
this
.
cache_file
.
read
());
cache
[
hash
]
=
{
time
:
time
(),
data
:
response
};
this
.
cache_file
.
truncate
();
this
.
cache_file
.
write
(
JSON
.
stringify
(
cache
));
this
.
cache_file
.
close
();
}
}
OpenWeatherMap
.
prototype
.
read_cache
=
function
(
endpoint
,
params
)
{
if
(
!
this
.
cache_file
.
exists
)
return
;
this
.
cache_file
.
open
(
'
r
'
);
const
cache
=
JSON
.
parse
(
this
.
cache_file
.
read
());
this
.
cache_file
.
close
();
const
hash
=
base64_encode
(
endpoint
+
JSON
.
stringify
(
params
));
if
(
!
this
.
cache
[
hash
])
return
;
if
(
!
cache
[
hash
])
return
;
// This is probably not the right way, but it'll do
if
(
time
()
-
this
.
cache
[
hash
].
time
>
this
.
settings
.
data_refresh
)
return
;
return
this
.
cache
[
hash
].
data
;
if
(
time
()
-
cache
[
hash
].
time
>
this
.
settings
.
data_refresh
)
return
;
return
cache
[
hash
].
data
;
}
OpenWeatherMap
.
prototype
.
rate_limit
=
function
()
{
const
now
=
time
();
this
.
requests
=
this
.
requests
.
filter
(
function
(
e
)
{
if
(
!
this
.
rate_file
.
exists
)
{
this
.
rate_file
.
open
(
'
w
'
);
var
rate
=
[];
}
else
{
this
.
rate_file
.
open
(
'
r+
'
);
var
rate
=
JSON
.
parse
(
this
.
rate_file
.
read
());
}
rate
=
rate
.
filter
(
function
(
e
)
{
return
now
-
e
<
this
.
settings
.
rate_window
;
},
this
);
if
(
this
.
requests
.
length
<
this
.
settings
.
rate_limit
)
{
this
.
requests
.
push
(
now
);
return
true
;
}
else
{
return
false
;
var
ret
=
false
;
if
(
rate
.
length
<
this
.
settings
.
rate_limit
)
{
rate
.
push
(
now
);
ret
=
true
;
}
this
.
rate_file
.
truncate
();
this
.
rate_file
.
write
(
JSON
.
stringify
(
rate
));
this
.
rate_file
.
close
();
return
ret
;
}
OpenWeatherMap
.
prototype
.
call_api
=
function
(
endpoint
,
params
)
{
...
...
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