// SPDX-License-Identifier: AGPL-3.0-or-later package ycrdt import ( "context" "fmt" ) // retireAbove is the memory size after which an instance is replaced: // WebAssembly memory never shrinks after a large document. const retireAbove = 64 << 20 // Pool hands out instances to one caller at a time. type Pool struct { rt *Runtime idle chan *Instance } // NewPool keeps up to size instances; they are created on first use. func NewPool(rt *Runtime, size int) *Pool { p := &Pool{rt: rt, idle: make(chan *Instance, size)} for range size { p.idle <- nil } return p } // Do runs fn with an instance. An instance that returned an error is thrown // away, since the module aborts on panics and may be left in a bad state. func (p *Pool) Do(ctx context.Context, fn func(*Instance) error) error { var inst *Instance select { case inst = <-p.idle: case <-ctx.Done(): return ctx.Err() } if inst == nil { var err error if inst, err = p.rt.Instance(ctx); err != nil { p.idle <- nil return err } } err := fn(inst) if err != nil || inst.MemoryBytes() > retireAbove { inst.Close(context.WithoutCancel(ctx)) inst = nil } p.idle <- inst return err } // Merge combines a few updates into one without building a document. It is // quadratic in the number of updates: use it for small batches only. func (p *Pool) Merge(ctx context.Context, updates [][]byte) (out []byte, err error) { if len(updates) == 1 { return updates[0], nil } err = p.Do(ctx, func(i *Instance) error { out, err = i.Merge(ctx, updates); return err }) return out, err } // Compact merges updates into one snapshot update. func (p *Pool) Compact(ctx context.Context, updates [][]byte) (out []byte, err error) { err = p.Do(ctx, func(i *Instance) error { out, err = i.Compact(ctx, updates); return err }) return out, err } // Diff returns what a peer with state vector sv is missing from update. func (p *Pool) Diff(ctx context.Context, update, sv []byte) (out []byte, err error) { err = p.Do(ctx, func(i *Instance) error { out, err = i.Diff(ctx, update, sv); return err }) return out, err } // StateVector returns the state vector of update. func (p *Pool) StateVector(ctx context.Context, update []byte) (out []byte, err error) { err = p.Do(ctx, func(i *Instance) error { out, err = i.StateVector(ctx, update); return err }) return out, err } // Replace returns an update that replaces the document in updates with doc (ProseMirror JSON). func (p *Pool) Replace(ctx context.Context, updates [][]byte, doc []byte) (out []byte, err error) { err = p.Do(ctx, func(i *Instance) error { out, err = i.Replace(ctx, updates, doc); return err }) return out, err } // Read is the derived content of a snapshot. type Read struct { JSON []byte Text string } // Read extracts ProseMirror JSON and plain text from a snapshot in one checkout. func (p *Pool) Read(ctx context.Context, snapshot []byte) (r Read, err error) { err = p.Do(ctx, func(i *Instance) error { u := [][]byte{snapshot} if r.JSON, err = i.JSON(ctx, u); err != nil { return fmt.Errorf("json: %w", err) } if r.Text, err = i.Text(ctx, u); err != nil { return fmt.Errorf("text: %w", err) } return nil }) return r, err }