@@ -5,9 +5,11 @@ import (
5
5
"encoding/json"
6
6
"fmt"
7
7
"io"
8
+ "log/slog"
9
+ "math/rand/v2"
10
+ "strconv"
8
11
9
12
"github.com/DataDog/zstd"
10
- "github.com/google/uuid"
11
13
"github.com/langchain-ai/langsmith-collector-proxy/internal/model"
12
14
)
13
15
@@ -29,12 +31,12 @@ type StreamingCompressor struct {
29
31
}
30
32
31
33
func NewStreamingCompressor () * StreamingCompressor {
32
- var buf bytes.Buffer
33
- zw := zstd .NewWriter (& buf )
34
+ buf := & bytes.Buffer {}
35
+ zw := zstd .NewWriter (buf )
34
36
return & StreamingCompressor {
35
- boundary : "BOUNDARY- " + uuid . NewString ( ),
37
+ boundary : "----LangSmithFormBoundary- " + strconv . FormatUint ( rand . Uint64 (), 36 ),
36
38
w : zw ,
37
- buf : & buf ,
39
+ buf : buf ,
38
40
}
39
41
}
40
42
@@ -78,24 +80,32 @@ func (sc *StreamingCompressor) AddRun(r *model.Run) error {
78
80
}
79
81
80
82
func (sc * StreamingCompressor ) Close () ([]byte , string , int , error ) {
81
- // Write the final multipart boundary.
82
- if sc .runCount == 0 || sc .uncompressed == 0 {
83
- return nil , "" , 0 , nil
84
- }
85
- if _ , err := sc .w .Write ([]byte (fmt .Sprintf ("--%s--\r \n " , sc .boundary ))); err != nil {
86
- return nil , "" , 0 , err
87
- }
88
- // Close the zstd writer.
89
- if err := sc .w .Close (); err != nil {
90
- return nil , "" , 0 , err
83
+ // Write the final multipart boundary and reset state.
84
+ hasData := sc .runCount > 0 || sc .uncompressed > 0
85
+ var (
86
+ outBytes []byte
87
+ boundary string
88
+ uncompressed int
89
+ err error
90
+ )
91
+
92
+ if hasData {
93
+ if _ , err = sc .w .Write ([]byte (fmt .Sprintf ("--%s--\r \n " , sc .boundary ))); err == nil {
94
+ err = sc .w .Close ()
95
+ }
96
+ if err == nil {
97
+ outBytes = sc .buf .Bytes ()
98
+ boundary = sc .boundary
99
+ uncompressed = sc .uncompressed
100
+ }
101
+ } else {
102
+ slog .Error ("Failed to write final multipart boundary" , "err" , err )
91
103
}
92
- out := sc .buf .Bytes ()
93
- uncompressed := sc .uncompressed
94
- // reset the buffer and writer for the next upload
95
- sc .buf = bytes .NewBuffer (nil )
104
+ sc .buf = & bytes.Buffer {}
105
+ sc .boundary = "----LangSmithFormBoundary-" + strconv .FormatUint (rand .Uint64 (), 36 )
96
106
sc .w = zstd .NewWriter (sc .buf )
97
- sc .runCount , sc . uncompressed = 0 , 0
98
- return out , sc . boundary , uncompressed , nil
107
+ sc .runCount = 0
108
+ return outBytes , boundary , uncompressed , err
99
109
}
100
110
101
111
func (sc * StreamingCompressor ) Uncompressed () int {
0 commit comments