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

Commit e704ed5

Browse files
authored
Merge pull request #1084 from vmarkovtsev/master
Increase diffmatchcpatch timeout
2 parents 948b0c9 + a80a9e5 commit e704ed5

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

utils/diff/diff.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,30 @@ package diff
88

99
import (
1010
"bytes"
11+
"time"
1112

1213
"github.com/sergi/go-diff/diffmatchpatch"
1314
)
1415

1516
// Do computes the (line oriented) modifications needed to turn the src
16-
// 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.
1720
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) {
1833
dmp := diffmatchpatch.New()
34+
dmp.DiffTimeout = timeout
1935
wSrc, wDst, warray := dmp.DiffLinesToRunes(src, dst)
2036
diffs = dmp.DiffMainRunes(wSrc, wDst, false)
2137
diffs = dmp.DiffCharsToLines(diffs, warray)

0 commit comments

Comments
 (0)