Conversation
|
@Aleridia can you fix these errors/warnings as reported by the CI? − https://travis-ci.org/github/ocaml-sf/learn-ocaml/jobs/696405269 |
737e5ab to
ed673ff
Compare
src/state/token_index.ml
Outdated
| let create_index (sync_dir : string) = | ||
| let l = get sync_dir () in | ||
| let data = l >|= List.map cast_string >|= cast_list in | ||
| data >>= write_file (sync_dir ^ "/" ^ token_file) mutex_token |
There was a problem hiding this comment.
it seems the four occurrences of
sync_dir ^ "/" ^ token_file
are not portable (under Windows, the directory separator is a \)
but one could replace this with sync_dir / token_file
by taking advantage of the definition above
let ( / ) dir f = if dir = "" then f else Filename.concat dir f in
(which could be promoted to a file-local definition, instead of a function-local definition)
@Aleridia I can push a commit applying this change, unless you prefer to do that yourself.
There was a problem hiding this comment.
I will be thankfull if you can push it @erikmd
I can do it but it won't be before tomorrow.
|
Dear @yurug, can you have a look on the code proposed in this PR? BTW the CI failure − only the macOS job − seems to be spurious: |
learn-ocaml-client.opam
Outdated
| "ppx_tools" | ||
| "ppx_sexp_conv" {= "v0.9.0"} | ||
| "ppx_fields_conv" {= "v0.9.0"} | ||
| "yojson" {>= "1.4.0" } |
There was a problem hiding this comment.
Why add a dependency on yojson instead of using ezjsonm? FWIW yojson is part of learn-ocaml’s dependencies but before this commit it looks like no binaries are even linked to it…
There was a problem hiding this comment.
@agrn good catch! Indeed it seems learn-ocaml currently depends on these two JSON libraries…
because even if this PR added yojson in learn-ocaml-client.opam, it was already part of learn-ocaml.opam:
Line 51 in b3d0e75
but it indeed seems yojson was not actually used.
@yurug do you agree with the proposed choice? (removing yojson from learn-ocaml direct dependencies, and rewrite #354 using ezjsonm)
src/state/token_index.ml
Outdated
| Lwt_mutex.unlock mutex | ||
|
|
||
| let create_index sync_dir = | ||
| (* Note: we may want to write some line in the standard output telling that |
|
So, I pushed two commits: one to use Ezjsonm instead of yojson (and remove the dependency in the *.opam files), and a second one to use modules to decouple the underlying storage from the index operations, so I can easily remove it if we elect not to do that. Feel free to comment on both of them. |
|
The failure is once again osx, failing to build |
erikmd
left a comment
There was a problem hiding this comment.
LGTM, thanks! Two comments:
You could maybe put these two declarations in the .mli (as they are generic, beyond tokens…):
module type IndexRW = sig
type t
val init : unit -> t
val read : string -> (string -> 'a) -> 'a Lwt.t
val write : t -> string -> ('a -> string) -> 'a -> unit Lwt.t
end
module IndexFile: IndexRW
Finally, you could implement that comment as suggested by Yann:
(* Note: we may want to write some line in the standard output telling that
the token index is being generated. *)
|
So, I force pushed a fixed up commit but it doesn't appear here. |
953adb4 to
29ce231
Compare
|
Ah no, it's all good now. |
29ce231 to
a1b8444
Compare
|
I edited the last commit to move edit: Travis once again failed because of macOS: |
a1b8444 to
30f3921
Compare
|
Rebased this branch on master as requested by @erikmd. |
Rename string_to_json : it's now cast_string Delete useless function Delete token_to_string Refactor string_to_token Remove parens on write_file Update way to use sync_dir Delete the 'sync/' from the token_file, change it on functions
We already use Ezjsonm, so there is no need to also depend on Yojson. Signed-off-by: Alban Gruin <alban.gruin@univ-tlse3.fr>
This transforms token_index to use modules, to allow to change the underlying storage (currently they are stored in flat files). Signed-off-by: Alban Gruin <alban.gruin@univ-tlse3.fr>
30f3921 to
fee06ee
Compare
|
@yurug thanks for having merged #356 ! I've just rebased this one to fix the conflict, to sum up:
|
|
Hi, I’ve successfully rebased oauth-moodle onto this new version, and I’m going to push it soon. |
| let serialise = Json_codec.encode ?minify:(Some(false)) enc | ||
|
|
||
| let create_index sync_dir = | ||
| let found_indexes = |
There was a problem hiding this comment.
Do we have a guarantee that new tokens can't be created while this function is running ?
I probably overlooked something, but here is a race-condition to be considered:
- the index is absent
- two clients A and B trigger
add_tokenat the same time - both lwthreads scan existing tokens concurrently, getting the same result
- the thread for A writes the index file, adds its token ta, and writes the index again
- the thread for B overwrites the index file with a version that doesn't contain ta
There may be constraints avoiding this at upper level; otherwise I would advise getting a write-lock (blocking concurrent scanning, reads and writes) before starting the index read/scan for the cases where you intend to update and write it back (i.e. add_token).
| | e -> Lwt.fail e | ||
| in | ||
| aux () | ||
| aux () >>= fun t -> TokenIndex.add_token !sync_dir t >|= fun _ -> t |
There was a problem hiding this comment.
If the index doesn't exist yet, add_token will scan, including the just created token, then add it again, resulting in a duplicate.
|
|
||
| let add_token sync_dir token = | ||
| get_tokens sync_dir >>= fun tokens -> | ||
| RW.write rw (sync_dir / file) serialise (token :: tokens) |
There was a problem hiding this comment.
Possible race condition if called concurrently ?
Hello,
As a result of #348 I changed the way of getting all the tokens.
Now, we have an index structure. It means that all the tokens are stored in a json file (sync/token.json). The function Token.Index.get will return all the tokens stored in the json file.
If a new token is created, it will be automatically added to the index.
If you don't have any index (updating per example), it will create itself by using the old Token.Index.get's function.