Skip to content

Commit a080a17

Browse files
authored
Create UUID string under a lock (#143)
Signed-off-by: Ira <[email protected]>
1 parent 471fac0 commit a080a17

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

pkg/common/utils.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ func RandomNorm(mean float64, stddev float64) float64 {
244244
return value
245245
}
246246

247+
// GenerateUUIDString generates a UUID string under a lock
248+
func GenerateUUIDString() string {
249+
randMutex.Lock()
250+
defer randMutex.Unlock()
251+
return uuid.NewString()
252+
}
253+
247254
// Regular expression for the response tokenization
248255
var re *regexp.Regexp
249256

pkg/llm-d-inference-sim/simulator.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030

3131
"github.com/buaazp/fasthttprouter"
3232
"github.com/go-logr/logr"
33-
"github.com/google/uuid"
3433
"github.com/prometheus/client_golang/prometheus"
3534
"github.com/prometheus/client_golang/prometheus/promhttp"
3635
"github.com/valyala/fasthttp"
@@ -200,7 +199,7 @@ func (s *VllmSimulator) Printf(format string, args ...interface{}) {
200199

201200
// readRequest reads and parses data from the body of the given request according the type defined by isChatCompletion
202201
func (s *VllmSimulator) readRequest(ctx *fasthttp.RequestCtx, isChatCompletion bool) (openaiserverapi.CompletionRequest, error) {
203-
requestID := uuid.NewString()
202+
requestID := common.GenerateUUIDString()
204203

205204
if isChatCompletion {
206205
var req openaiserverapi.ChatCompletionRequest
@@ -546,7 +545,7 @@ func (s *VllmSimulator) HandleError(_ *fasthttp.RequestCtx, err error) {
546545
func (s *VllmSimulator) createCompletionResponse(isChatCompletion bool, respTokens []string, toolCalls []openaiserverapi.ToolCall,
547546
finishReason *string, usageData *openaiserverapi.Usage, modelName string, doRemoteDecode bool) openaiserverapi.CompletionResponse {
548547
baseResp := openaiserverapi.BaseCompletionResponse{
549-
ID: chatComplIDPrefix + uuid.NewString(),
548+
ID: chatComplIDPrefix + common.GenerateUUIDString(),
550549
Created: time.Now().Unix(),
551550
Model: modelName,
552551
Usage: usageData,

pkg/llm-d-inference-sim/streaming.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"fmt"
2323
"time"
2424

25-
"github.com/google/uuid"
2625
"github.com/llm-d/llm-d-inference-sim/pkg/common"
2726
openaiserverapi "github.com/llm-d/llm-d-inference-sim/pkg/openai-server-api"
2827
"github.com/valyala/fasthttp"
@@ -154,7 +153,7 @@ func (s *VllmSimulator) sendTokenChunks(context *streamingContext, w *bufio.Writ
154153
// supports both modes (text and chat)
155154
func (s *VllmSimulator) createUsageChunk(context *streamingContext, usageData *openaiserverapi.Usage) openaiserverapi.CompletionRespChunk {
156155
baseChunk := openaiserverapi.BaseCompletionResponse{
157-
ID: chatComplIDPrefix + uuid.NewString(),
156+
ID: chatComplIDPrefix + common.GenerateUUIDString(),
158157
Created: context.creationTime,
159158
Model: context.model,
160159
Usage: usageData,
@@ -179,7 +178,7 @@ func (s *VllmSimulator) createUsageChunk(context *streamingContext, usageData *o
179178
func (s *VllmSimulator) createTextCompletionChunk(context *streamingContext, token string, finishReason *string) openaiserverapi.CompletionRespChunk {
180179
return &openaiserverapi.TextCompletionResponse{
181180
BaseCompletionResponse: openaiserverapi.BaseCompletionResponse{
182-
ID: chatComplIDPrefix + uuid.NewString(),
181+
ID: chatComplIDPrefix + common.GenerateUUIDString(),
183182
Created: context.creationTime,
184183
Model: context.model,
185184
Object: textCompletionObject,
@@ -199,7 +198,7 @@ func (s *VllmSimulator) createChatCompletionChunk(context *streamingContext, tok
199198
role string, finishReason *string) openaiserverapi.CompletionRespChunk {
200199
chunk := openaiserverapi.ChatCompletionRespChunk{
201200
BaseCompletionResponse: openaiserverapi.BaseCompletionResponse{
202-
ID: chatComplIDPrefix + uuid.NewString(),
201+
ID: chatComplIDPrefix + common.GenerateUUIDString(),
203202
Created: context.creationTime,
204203
Model: context.model,
205204
Object: chatCompletionChunkObject,

0 commit comments

Comments
 (0)