Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit a80a9e5

Browse files
committed
Add diff.DoWithTimeout()
Signed-off-by: Vadim Markovtsev <vadim@sourced.tech>
1 parent 3dc0d50 commit a80a9e5

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

utils/diff/diff.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,24 @@ import (
1414
)
1515

1616
// Do computes the (line oriented) modifications needed to turn the src
17-
// string into the dst string.
17+
// string into the dst string. The underlying algorithm is Meyers,
18+
// its complexity is O(N*d) where N is min(lines(src), lines(dst)) and d
19+
// is the size of the diff.
1820
func Do(src, dst string) (diffs []diffmatchpatch.Diff) {
21+
// the default timeout is time.Second which may be too small under heavy load
22+
return DoWithTimeout(src, dst, time.Hour)
23+
}
24+
25+
// DoWithTimeout computes the (line oriented) modifications needed to turn the src
26+
// string into the dst string. The `timeout` argument specifies the maximum
27+
// amount of time it is allowed to spend in this function. If the timeout
28+
// is exceeded, the parts of the strings which were not considered are turned into
29+
// a bulk delete+insert and the half-baked suboptimal result is returned at once.
30+
// The underlying algorithm is Meyers, its complexity is O(N*d) where N is
31+
// min(lines(src), lines(dst)) and d is the size of the diff.
32+
func DoWithTimeout (src, dst string, timeout time.Duration) (diffs []diffmatchpatch.Diff) {
1933
dmp := diffmatchpatch.New()
20-
dmp.DiffTimeout = time.Hour // the default is time.Second which may be too little under heavy load
34+
dmp.DiffTimeout = timeout
2135
wSrc, wDst, warray := dmp.DiffLinesToRunes(src, dst)
2236
diffs = dmp.DiffMainRunes(wSrc, wDst, false)
2337
diffs = dmp.DiffCharsToLines(diffs, warray)

0 commit comments

Comments
 (0)