2017-12-29 01:29:53 -05:00
|
|
|
# GitLab utilities
|
|
|
|
|
2019-10-20 23:06:30 -04:00
|
|
|
We have developed a number of utilities to help ease development:
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2019-09-18 14:06:14 -04:00
|
|
|
## `MergeHash`
|
|
|
|
|
|
|
|
Refer to: <https://gitlab.com/gitlab-org/gitlab/blob/master/lib/gitlab/utils/merge_hash.rb>:
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2018-11-13 01:07:16 -05:00
|
|
|
- Deep merges an array of hashes:
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2019-07-31 04:28:51 -04:00
|
|
|
``` ruby
|
|
|
|
Gitlab::Utils::MergeHash.merge(
|
|
|
|
[{ hello: ["world"] },
|
|
|
|
{ hello: "Everyone" },
|
|
|
|
{ hello: { greetings: ['Bonjour', 'Hello', 'Hallo', 'Dzien dobry'] } },
|
|
|
|
"Goodbye", "Hallo"]
|
|
|
|
)
|
|
|
|
```
|
|
|
|
|
|
|
|
Gives:
|
|
|
|
|
|
|
|
``` ruby
|
|
|
|
[
|
|
|
|
{
|
|
|
|
hello:
|
|
|
|
[
|
|
|
|
"world",
|
|
|
|
"Everyone",
|
|
|
|
{ greetings: ['Bonjour', 'Hello', 'Hallo', 'Dzien dobry'] }
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"Goodbye"
|
|
|
|
]
|
|
|
|
```
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2018-11-13 01:07:16 -05:00
|
|
|
- Extracts all keys and values from a hash into an array:
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2019-07-31 04:28:51 -04:00
|
|
|
``` ruby
|
|
|
|
Gitlab::Utils::MergeHash.crush(
|
|
|
|
{ hello: "world", this: { crushes: ["an entire", "hash"] } }
|
|
|
|
)
|
|
|
|
```
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2019-07-31 04:28:51 -04:00
|
|
|
Gives:
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2019-07-31 04:28:51 -04:00
|
|
|
``` ruby
|
|
|
|
[:hello, "world", :this, :crushes, "an entire", "hash"]
|
|
|
|
```
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2019-09-18 14:06:14 -04:00
|
|
|
## `Override`
|
|
|
|
|
|
|
|
Refer to <https://gitlab.com/gitlab-org/gitlab/blob/master/lib/gitlab/utils/override.rb>:
|
2018-01-12 06:37:03 -05:00
|
|
|
|
2019-10-20 23:06:30 -04:00
|
|
|
- This utility can help you check if one method would override
|
|
|
|
another or not. It is the same concept as Java's `@Override` annotation
|
|
|
|
or Scala's `override` keyword. However, you should only do this check when
|
2018-01-12 06:37:03 -05:00
|
|
|
`ENV['STATIC_VERIFICATION']` is set to avoid production runtime overhead.
|
2019-10-20 23:06:30 -04:00
|
|
|
This is useful for checking:
|
2018-01-12 06:37:03 -05:00
|
|
|
|
2019-10-20 23:06:30 -04:00
|
|
|
- If you have typos in overriding methods.
|
|
|
|
- If you renamed the overridden methods, which make the original override methods
|
|
|
|
irrelevant.
|
2018-01-12 06:37:03 -05:00
|
|
|
|
|
|
|
Here's a simple example:
|
|
|
|
|
|
|
|
``` ruby
|
|
|
|
class Base
|
|
|
|
def execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Derived < Base
|
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
|
|
|
override :execute # Override check happens here
|
|
|
|
def execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
This also works on modules:
|
|
|
|
|
|
|
|
``` ruby
|
|
|
|
module Extension
|
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
|
|
|
override :execute # Modules do not check this immediately
|
|
|
|
def execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Derived < Base
|
|
|
|
prepend Extension # Override check happens here, not in the module
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2019-09-18 14:06:14 -04:00
|
|
|
## `StrongMemoize`
|
|
|
|
|
|
|
|
Refer to <https://gitlab.com/gitlab-org/gitlab/blob/master/lib/gitlab/utils/strong_memoize.rb>:
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2018-11-13 01:07:16 -05:00
|
|
|
- Memoize the value even if it is `nil` or `false`.
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2019-10-20 23:06:30 -04:00
|
|
|
We often do `@value ||= compute`. However, this doesn't work well if
|
|
|
|
`compute` might eventually give `nil` and you don't want to compute again.
|
|
|
|
Instead you could use `defined?` to check if the value is set or not.
|
|
|
|
It's tedious to write such pattern, and `StrongMemoize` would
|
|
|
|
help you use such pattern.
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2019-07-31 04:28:51 -04:00
|
|
|
Instead of writing patterns like this:
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2019-07-31 04:28:51 -04:00
|
|
|
``` ruby
|
|
|
|
class Find
|
|
|
|
def result
|
|
|
|
return @result if defined?(@result)
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2019-07-31 04:28:51 -04:00
|
|
|
@result = search
|
2017-12-29 01:29:53 -05:00
|
|
|
end
|
2019-07-31 04:28:51 -04:00
|
|
|
end
|
|
|
|
```
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2019-10-20 23:06:30 -04:00
|
|
|
You could write it like:
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2019-07-31 04:28:51 -04:00
|
|
|
``` ruby
|
|
|
|
class Find
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2019-07-31 04:28:51 -04:00
|
|
|
def result
|
|
|
|
strong_memoize(:result) do
|
|
|
|
search
|
2017-12-29 01:29:53 -05:00
|
|
|
end
|
|
|
|
end
|
2019-07-31 04:28:51 -04:00
|
|
|
end
|
|
|
|
```
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2018-11-13 01:07:16 -05:00
|
|
|
- Clear memoization
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2019-07-31 04:28:51 -04:00
|
|
|
``` ruby
|
|
|
|
class Find
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
end
|
2017-12-29 01:29:53 -05:00
|
|
|
|
2019-07-31 04:28:51 -04:00
|
|
|
Find.new.clear_memoization(:result)
|
|
|
|
```
|
2018-06-21 07:46:52 -04:00
|
|
|
|
2019-09-18 14:06:14 -04:00
|
|
|
## `RequestCache`
|
|
|
|
|
|
|
|
Refer to <https://gitlab.com/gitlab-org/gitlab/blob/master/lib/gitlab/cache/request_cache.rb>.
|
2018-06-21 07:46:52 -04:00
|
|
|
|
|
|
|
This module provides a simple way to cache values in RequestStore,
|
|
|
|
and the cache key would be based on the class name, method name,
|
|
|
|
optionally customized instance level values, optionally customized
|
|
|
|
method level values, and optional method arguments.
|
|
|
|
|
2019-10-20 23:06:30 -04:00
|
|
|
A simple example that only uses the instance level customised values is:
|
2018-06-21 07:46:52 -04:00
|
|
|
|
|
|
|
``` ruby
|
|
|
|
class UserAccess
|
|
|
|
extend Gitlab::Cache::RequestCache
|
|
|
|
|
|
|
|
request_cache_key do
|
|
|
|
[user&.id, project&.id]
|
|
|
|
end
|
|
|
|
|
|
|
|
request_cache def can_push_to_branch?(ref)
|
|
|
|
# ...
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
This way, the result of `can_push_to_branch?` would be cached in
|
|
|
|
`RequestStore.store` based on the cache key. If `RequestStore` is not
|
2019-10-20 23:06:30 -04:00
|
|
|
currently active, then it would be stored in a hash, and saved in an
|
|
|
|
instance variable so the cache logic would be the same.
|
2018-06-21 07:46:52 -04:00
|
|
|
|
|
|
|
We can also set different strategies for different methods:
|
|
|
|
|
|
|
|
``` ruby
|
|
|
|
class Commit
|
|
|
|
extend Gitlab::Cache::RequestCache
|
|
|
|
|
|
|
|
def author
|
2018-11-07 06:00:21 -05:00
|
|
|
User.find_by_any_email(author_email)
|
2018-06-21 07:46:52 -04:00
|
|
|
end
|
2018-11-07 06:00:21 -05:00
|
|
|
request_cache(:author) { author_email }
|
2018-06-21 07:46:52 -04:00
|
|
|
end
|
|
|
|
```
|
2019-11-06 13:06:29 -05:00
|
|
|
|
|
|
|
## `ReactiveCaching`
|
|
|
|
|
|
|
|
The `ReactiveCaching` concern is used to fetch some data in the background and
|
|
|
|
store it in the Rails cache, keeping it up-to-date for as long as it is being
|
|
|
|
requested. If the data hasn't been requested for `reactive_cache_lifetime`,
|
|
|
|
it will stop being refreshed, and then be removed.
|
|
|
|
|
|
|
|
Example of use:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
class Foo < ApplicationRecord
|
|
|
|
include ReactiveCaching
|
|
|
|
|
|
|
|
after_save :clear_reactive_cache!
|
|
|
|
|
|
|
|
def calculate_reactive_cache
|
|
|
|
# Expensive operation here. The return value of this method is cached
|
|
|
|
end
|
|
|
|
|
|
|
|
def result
|
|
|
|
with_reactive_cache do |data|
|
|
|
|
# ...
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
In this example, the first time `#result` is called, it will return `nil`.
|
|
|
|
However, it will enqueue a background worker to call `#calculate_reactive_cache`
|
|
|
|
and set an initial cache lifetime of ten minutes.
|
|
|
|
|
|
|
|
The background worker needs to find or generate the object on which
|
|
|
|
`with_reactive_cache` was called.
|
|
|
|
The default behaviour can be overridden by defining a custom
|
|
|
|
`reactive_cache_worker_finder`.
|
|
|
|
Otherwise, the background worker will use the class name and primary key to get
|
|
|
|
the object using the ActiveRecord `find_by` method.
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
class Bar
|
|
|
|
include ReactiveCaching
|
|
|
|
|
|
|
|
self.reactive_cache_key = ->() { ["bar", "thing"] }
|
|
|
|
self.reactive_cache_worker_finder = ->(_id, *args) { from_cache(*args) }
|
|
|
|
|
|
|
|
def self.from_cache(var1, var2)
|
|
|
|
# This method will be called by the background worker with "bar1" and
|
|
|
|
# "bar2" as arguments.
|
|
|
|
new(var1, var2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(var1, var2)
|
|
|
|
# ...
|
|
|
|
end
|
|
|
|
|
|
|
|
def calculate_reactive_cache
|
|
|
|
# Expensive operation here. The return value of this method is cached
|
|
|
|
end
|
|
|
|
|
|
|
|
def result
|
|
|
|
with_reactive_cache("bar1", "bar2") do |data|
|
|
|
|
# ...
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
Each time the background job completes, it stores the return value of
|
|
|
|
`#calculate_reactive_cache`. It is also re-enqueued to run again after
|
|
|
|
`reactive_cache_refresh_interval`, therefore, it will keep the stored value up to date.
|
|
|
|
Calculations are never run concurrently.
|
|
|
|
|
|
|
|
Calling `#result` while a value is cached will call the block given to
|
|
|
|
`#with_reactive_cache`, yielding the cached value. It will also extend the
|
|
|
|
lifetime by the `reactive_cache_lifetime` value.
|
|
|
|
|
|
|
|
Once the lifetime has expired, no more background jobs will be enqueued and
|
|
|
|
calling `#result` will again return `nil` - starting the process all over
|
|
|
|
again.
|