fix(02): write fixtures exclusively and only on successful flush (WR-01)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Jakub Zych
2026-09-17 14:41:18 +02:00
parent 5994e671b3
commit 7e70afe819
5 changed files with 92 additions and 9 deletions

View File

@@ -48,6 +48,7 @@ type ProxyConfig struct {
VarsPath string
Fixtures string
MaxBody int64
Update bool
}
// Proxy is a fixed-upstream recording reverse proxy.
@@ -282,7 +283,7 @@ func (p *Proxy) recordStep(state *captureState, resp *http.Response, respBody []
return err
}
buf.steps = append(buf.steps, step)
return p.writeSessionLocked(buf)
return nil
}
func (p *Proxy) sessionLocked(name string) (*sessionBuf, error) {
@@ -290,8 +291,10 @@ func (p *Proxy) sessionLocked(name string) (*sessionBuf, error) {
return buf, nil
}
dest := p.sessionPath(name)
if _, err := os.Stat(dest); err == nil {
return nil, fmt.Errorf("tide: duplicate session %q", name)
if !p.cfg.Update {
if _, err := os.Stat(dest); err == nil {
return nil, fmt.Errorf("tide: duplicate session %q", name)
}
}
buf := &sessionBuf{name: name}
p.sessions[name] = buf
@@ -310,7 +313,11 @@ func (p *Proxy) writeSessionLocked(buf *sessionBuf) error {
Name: buf.name,
Steps: append([]Step(nil), buf.steps...),
}
return SaveFlow(p.sessionPath(buf.name), flow)
dest := p.sessionPath(buf.name)
if p.cfg.Update {
return SaveFlow(dest, flow)
}
return SaveFlowExclusive(dest, flow)
}
func (p *Proxy) sessionPath(name string) string {
@@ -323,9 +330,7 @@ func (p *Proxy) failSession(name string, err error) {
if _, ok := p.failed[name]; !ok {
p.failed[name] = err
}
if buf := p.sessions[name]; buf != nil && len(buf.steps) == 0 {
_ = os.Remove(p.sessionPath(name))
}
_ = os.Remove(p.sessionPath(name))
}
func validateSessionName(name string) error {