-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
diffs.go
143 lines (115 loc) 路 3.82 KB
/
diffs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2016-2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package httpstate
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"sync"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
opentracing "github.com/opentracing/opentracing-go"
)
type deploymentDiffState struct {
lastSavedDeployment json.RawMessage
sequenceNumber int
minimalDiffSize int
}
type deploymentDiff struct {
sequenceNumber int
checkpointHash string
deploymentDelta json.RawMessage
}
func newDeploymentDiffState(minimalDiffSize int) *deploymentDiffState {
return &deploymentDiffState{
sequenceNumber: 1,
minimalDiffSize: minimalDiffSize,
}
}
func (dds *deploymentDiffState) SequenceNumber() int {
return dds.sequenceNumber
}
func (dds *deploymentDiffState) CanDiff() bool {
return dds.lastSavedDeployment != nil
}
// Size-based heuristics trying to estimate if the diff method will be
// worth it and take less time than sending the entire deployment.
func (dds *deploymentDiffState) ShouldDiff(new json.RawMessage) bool {
if !dds.CanDiff() {
return false
}
if len(dds.lastSavedDeployment) < dds.minimalDiffSize {
return false
}
if len(new) < dds.minimalDiffSize {
return false
}
return true
}
func (dds *deploymentDiffState) Diff(ctx context.Context, deployment json.RawMessage) (deploymentDiff, error) {
if !dds.CanDiff() {
return deploymentDiff{}, fmt.Errorf("Diff() cannot be called before Saved()")
}
tracingSpan, childCtx := opentracing.StartSpanFromContext(ctx, "Diff")
defer tracingSpan.Finish()
before := dds.lastSavedDeployment
after := deployment
var checkpointHash string
checkpointHashReady := &sync.WaitGroup{}
checkpointHashReady.Add(1)
go func() {
defer checkpointHashReady.Done()
checkpointHash = dds.computeHash(childCtx, after)
}()
delta, err := dds.computeEdits(childCtx, string(before), string(after))
if err != nil {
return deploymentDiff{}, fmt.Errorf("Cannot marshal the edits: %v", err)
}
checkpointHashReady.Wait()
tracingSpan.SetTag("before", len(before))
tracingSpan.SetTag("after", len(after))
tracingSpan.SetTag("diff", len(delta))
tracingSpan.SetTag("compression", 100.0*float64(len(delta))/float64(len(after)))
tracingSpan.SetTag("hash", checkpointHash)
diff := deploymentDiff{
checkpointHash: checkpointHash,
deploymentDelta: delta,
sequenceNumber: dds.sequenceNumber,
}
return diff, nil
}
// Indicates that a deployment was just saved to the service.
func (dds *deploymentDiffState) Saved(ctx context.Context, deployment json.RawMessage) error {
dds.lastSavedDeployment = deployment
dds.sequenceNumber++
return nil
}
func (*deploymentDiffState) computeHash(ctx context.Context, deployment json.RawMessage) string {
tracingSpan, _ := opentracing.StartSpanFromContext(ctx, "computeHash")
defer tracingSpan.Finish()
hash := sha256.Sum256(deployment)
return hex.EncodeToString(hash[:])
}
func (*deploymentDiffState) computeEdits(ctx context.Context, before, after string) (json.RawMessage, error) {
tracingSpan, _ := opentracing.StartSpanFromContext(ctx, "computeEdits")
defer tracingSpan.Finish()
edits := myers.ComputeEdits(span.URIFromURI(""), before, after)
delta, err := json.Marshal(edits)
if err != nil {
return nil, fmt.Errorf("Cannot marshal the edits: %v", err)
}
return delta, nil
}