Skip to content

Commit e1fd74c

Browse files
committed
refactor: rename Handler to HTTPHandler and update related methods
1 parent 81194b0 commit e1fd74c

File tree

5 files changed

+54
-54
lines changed

5 files changed

+54
-54
lines changed

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func main() {
8989
Logger: log.WithFields(logrus.Fields{"component": "model-manager"}),
9090
Transport: baseTransport,
9191
}
92-
modelHandler := models.NewHandler(
92+
modelHandler := models.NewHTTPHandler(
9393
log,
9494
clientConfig,
9595
nil,
@@ -183,7 +183,7 @@ func main() {
183183
router.Handle("/score", aliasHandler)
184184

185185
// Add Ollama API compatibility layer (only register with trailing slash to catch sub-paths)
186-
ollamaHandler := ollama.NewHandler(log, scheduler, schedulerHTTP, nil, modelManager)
186+
ollamaHandler := ollama.NewHTTPHandler(log, scheduler, schedulerHTTP, nil, modelManager)
187187
router.Handle(ollama.APIPrefix+"/", ollamaHandler)
188188

189189
// Register root handler LAST - it will only catch exact "/" requests that don't match other patterns

pkg/inference/models/handler_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func TestPullModel(t *testing.T) {
124124
t.Run(tt.name, func(t *testing.T) {
125125
log := logrus.NewEntry(logrus.StandardLogger())
126126
memEstimator := &mockMemoryEstimator{}
127-
handler := NewHandler(log, ClientConfig{
127+
handler := NewHTTPHandler(log, ClientConfig{
128128
StoreRootPath: tempDir,
129129
Logger: log.WithFields(logrus.Fields{"component": "model-manager"}),
130130
}, nil, memEstimator)
@@ -235,7 +235,7 @@ func TestHandleGetModel(t *testing.T) {
235235
t.Run(tt.name, func(t *testing.T) {
236236
log := logrus.NewEntry(logrus.StandardLogger())
237237
memEstimator := &mockMemoryEstimator{}
238-
handler := NewHandler(log, ClientConfig{
238+
handler := NewHTTPHandler(log, ClientConfig{
239239
StoreRootPath: tempDir,
240240
Logger: log.WithFields(logrus.Fields{"component": "model-manager"}),
241241
Transport: http.DefaultTransport,
@@ -319,7 +319,7 @@ func TestCors(t *testing.T) {
319319
discard := logrus.New()
320320
discard.SetOutput(io.Discard)
321321
log := logrus.NewEntry(discard)
322-
m := NewHandler(log, ClientConfig{}, []string{"*"}, memEstimator)
322+
m := NewHTTPHandler(log, ClientConfig{}, []string{"*"}, memEstimator)
323323
req := httptest.NewRequest(http.MethodOptions, "http://model-runner.docker.internal"+tt.path, http.NoBody)
324324
req.Header.Set("Origin", "docker.com")
325325
w := httptest.NewRecorder()
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ const (
2727
defaultTag = "latest"
2828
)
2929

30-
// Handler manages inference model pulls and storage.
31-
type Handler struct {
30+
// HTTPHandler manages inference model pulls and storage.
31+
type HTTPHandler struct {
3232
// log is the associated logger.
3333
log logging.Logger
3434
// router is the HTTP request router.
@@ -55,10 +55,10 @@ type ClientConfig struct {
5555
UserAgent string
5656
}
5757

58-
// NewHandler creates a new model's handler.
59-
func NewHandler(log logging.Logger, c ClientConfig, allowedOrigins []string, memoryEstimator memory.MemoryEstimator) *Handler {
58+
// NewHTTPHandler creates a new model's handler.
59+
func NewHTTPHandler(log logging.Logger, c ClientConfig, allowedOrigins []string, memoryEstimator memory.MemoryEstimator) *HTTPHandler {
6060
// Create the manager.
61-
m := &Handler{
61+
m := &HTTPHandler{
6262
log: log,
6363
router: http.NewServeMux(),
6464
memoryEstimator: memoryEstimator,
@@ -76,11 +76,11 @@ func NewHandler(log logging.Logger, c ClientConfig, allowedOrigins []string, mem
7676

7777
m.RebuildRoutes(allowedOrigins)
7878

79-
// Handler successfully initialized.
79+
// HTTPHandler successfully initialized.
8080
return m
8181
}
8282

83-
func (h *Handler) RebuildRoutes(allowedOrigins []string) {
83+
func (h *HTTPHandler) RebuildRoutes(allowedOrigins []string) {
8484
h.lock.Lock()
8585
defer h.lock.Unlock()
8686
// Update handlers that depend on the allowed origins.
@@ -134,7 +134,7 @@ func NormalizeModelName(model string) string {
134134
return nameWithOrg + ":" + tag
135135
}
136136

137-
func (h *Handler) routeHandlers() map[string]http.HandlerFunc {
137+
func (h *HTTPHandler) routeHandlers() map[string]http.HandlerFunc {
138138
return map[string]http.HandlerFunc{
139139
"POST " + inference.ModelsPrefix + "/create": h.handleCreateModel,
140140
"POST " + inference.ModelsPrefix + "/load": h.handleLoadModel,
@@ -152,7 +152,7 @@ func (h *Handler) routeHandlers() map[string]http.HandlerFunc {
152152
}
153153

154154
// handleCreateModel handles POST <inference-prefix>/models/create requests.
155-
func (h *Handler) handleCreateModel(w http.ResponseWriter, r *http.Request) {
155+
func (h *HTTPHandler) handleCreateModel(w http.ResponseWriter, r *http.Request) {
156156
// Decode the request.
157157
var request ModelCreateRequest
158158
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
@@ -208,7 +208,7 @@ func (h *Handler) handleCreateModel(w http.ResponseWriter, r *http.Request) {
208208
}
209209

210210
// handleLoadModel handles POST <inference-prefix>/models/load requests.
211-
func (h *Handler) handleLoadModel(w http.ResponseWriter, r *http.Request) {
211+
func (h *HTTPHandler) handleLoadModel(w http.ResponseWriter, r *http.Request) {
212212
err := h.manager.Load(r.Body, w)
213213
if err != nil {
214214
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -217,7 +217,7 @@ func (h *Handler) handleLoadModel(w http.ResponseWriter, r *http.Request) {
217217
}
218218

219219
// handleGetModels handles GET <inference-prefix>/models requests.
220-
func (h *Handler) handleGetModels(w http.ResponseWriter, r *http.Request) {
220+
func (h *HTTPHandler) handleGetModels(w http.ResponseWriter, r *http.Request) {
221221
apiModels, err := h.manager.List()
222222
if err != nil {
223223
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -232,7 +232,7 @@ func (h *Handler) handleGetModels(w http.ResponseWriter, r *http.Request) {
232232
}
233233

234234
// handleGetModel handles GET <inference-prefix>/models/{name} requests.
235-
func (h *Handler) handleGetModel(w http.ResponseWriter, r *http.Request) {
235+
func (h *HTTPHandler) handleGetModel(w http.ResponseWriter, r *http.Request) {
236236
modelRef := r.PathValue("name")
237237

238238
// Parse remote query parameter
@@ -269,15 +269,15 @@ func (h *Handler) handleGetModel(w http.ResponseWriter, r *http.Request) {
269269
}
270270
}
271271

272-
func (h *Handler) getRemoteAPIModel(ctx context.Context, modelRef string) (*Model, error) {
272+
func (h *HTTPHandler) getRemoteAPIModel(ctx context.Context, modelRef string) (*Model, error) {
273273
model, err := h.manager.GetRemote(ctx, modelRef)
274274
if err != nil {
275275
return nil, err
276276
}
277277
return ToModelFromArtifact(model)
278278
}
279279

280-
func (h *Handler) getLocalAPIModel(modelRef string) (*Model, error) {
280+
func (h *HTTPHandler) getLocalAPIModel(modelRef string) (*Model, error) {
281281
model, err := h.manager.GetLocal(modelRef)
282282
if err != nil {
283283
// If not found locally, try partial name matching
@@ -291,7 +291,7 @@ func (h *Handler) getLocalAPIModel(modelRef string) (*Model, error) {
291291
return ToModel(model)
292292
}
293293

294-
func (h *Handler) writeModelError(w http.ResponseWriter, err error) {
294+
func (h *HTTPHandler) writeModelError(w http.ResponseWriter, err error) {
295295
if errors.Is(err, distribution.ErrModelNotFound) || errors.Is(err, registry.ErrModelNotFound) {
296296
http.Error(w, err.Error(), http.StatusNotFound)
297297
return
@@ -302,7 +302,7 @@ func (h *Handler) writeModelError(w http.ResponseWriter, err error) {
302302

303303
// findModelByPartialName looks for a model by matching the provided reference
304304
// against model tags using partial name matching (e.g., "smollm2" matches "ai/smollm2:latest")
305-
func findModelByPartialName(h *Handler, modelRef string) (*Model, error) {
305+
func findModelByPartialName(h *HTTPHandler, modelRef string) (*Model, error) {
306306
// Get all models to search through their tags
307307
models, err := h.manager.RawList()
308308
if err != nil {
@@ -337,7 +337,7 @@ func findModelByPartialName(h *Handler, modelRef string) (*Model, error) {
337337
// handleDeleteModel handles DELETE <inference-prefix>/models/{name} requests.
338338
// query params:
339339
// - force: if true, delete the model even if it has multiple tags
340-
func (h *Handler) handleDeleteModel(w http.ResponseWriter, r *http.Request) {
340+
func (h *HTTPHandler) handleDeleteModel(w http.ResponseWriter, r *http.Request) {
341341
// TODO: We probably want the manager to have a lock / unlock mechanism for
342342
// models so that active runners can retain / release a model, analogous to
343343
// a container blocking the release of an image. However, unlike containers,
@@ -390,7 +390,7 @@ func (h *Handler) handleDeleteModel(w http.ResponseWriter, r *http.Request) {
390390

391391
// handleOpenAIGetModels handles GET <inference-prefix>/<backend>/v1/models and
392392
// GET /<inference-prefix>/v1/models requests.
393-
func (h *Handler) handleOpenAIGetModels(w http.ResponseWriter, r *http.Request) {
393+
func (h *HTTPHandler) handleOpenAIGetModels(w http.ResponseWriter, r *http.Request) {
394394
// Query models.
395395
available, err := h.manager.RawList()
396396
if err != nil {
@@ -413,7 +413,7 @@ func (h *Handler) handleOpenAIGetModels(w http.ResponseWriter, r *http.Request)
413413

414414
// handleOpenAIGetModel handles GET <inference-prefix>/<backend>/v1/models/{name}
415415
// and GET <inference-prefix>/v1/models/{name} requests.
416-
func (h *Handler) handleOpenAIGetModel(w http.ResponseWriter, r *http.Request) {
416+
func (h *HTTPHandler) handleOpenAIGetModel(w http.ResponseWriter, r *http.Request) {
417417
modelRef := r.PathValue("name")
418418
model, err := h.manager.GetLocal(modelRef)
419419
if err != nil {
@@ -441,7 +441,7 @@ func (h *Handler) handleOpenAIGetModel(w http.ResponseWriter, r *http.Request) {
441441
// Action is one of:
442442
// - tag: tag the model with a repository and tag (e.g. POST <inference-prefix>/models/my-org/my-repo:latest/tag})
443443
// - push: pushes a tagged model to the registry
444-
func (h *Handler) handleModelAction(w http.ResponseWriter, r *http.Request) {
444+
func (h *HTTPHandler) handleModelAction(w http.ResponseWriter, r *http.Request) {
445445
model, action := path.Split(r.PathValue("nameAndAction"))
446446
model = strings.TrimRight(model, "/")
447447

@@ -461,7 +461,7 @@ func (h *Handler) handleModelAction(w http.ResponseWriter, r *http.Request) {
461461
// The query parameters are:
462462
// - repo: the repository to tag the model with (required)
463463
// - tag: the tag to apply to the model (required)
464-
func (h *Handler) handleTagModel(w http.ResponseWriter, r *http.Request, model string) {
464+
func (h *HTTPHandler) handleTagModel(w http.ResponseWriter, r *http.Request, model string) {
465465
// Extract query parameters.
466466
repo := r.URL.Query().Get("repo")
467467
tag := r.URL.Query().Get("tag")
@@ -500,7 +500,7 @@ func (h *Handler) handleTagModel(w http.ResponseWriter, r *http.Request, model s
500500
}
501501

502502
// handlePushModel handles POST <inference-prefix>/models/{name}/push requests.
503-
func (h *Handler) handlePushModel(w http.ResponseWriter, r *http.Request, model string) {
503+
func (h *HTTPHandler) handlePushModel(w http.ResponseWriter, r *http.Request, model string) {
504504
if err := h.manager.Push(model, r, w); err != nil {
505505
if errors.Is(err, distribution.ErrInvalidReference) {
506506
h.log.Warnf("Invalid model reference %q: %v", model, err)
@@ -523,7 +523,7 @@ func (h *Handler) handlePushModel(w http.ResponseWriter, r *http.Request, model
523523
}
524524

525525
// handlePackageModel handles POST <inference-prefix>/models/package requests.
526-
func (h *Handler) handlePackageModel(w http.ResponseWriter, r *http.Request) {
526+
func (h *HTTPHandler) handlePackageModel(w http.ResponseWriter, r *http.Request) {
527527

528528
// Decode the request
529529
var request ModelPackageRequest
@@ -564,7 +564,7 @@ func (h *Handler) handlePackageModel(w http.ResponseWriter, r *http.Request) {
564564
}
565565

566566
// handlePurge handles DELETE <inference-prefix>/models/purge requests.
567-
func (h *Handler) handlePurge(w http.ResponseWriter, _ *http.Request) {
567+
func (h *HTTPHandler) handlePurge(w http.ResponseWriter, _ *http.Request) {
568568
err := h.manager.Purge()
569569
if err != nil {
570570
h.log.Warnf("Failed to purge models: %v", err)
@@ -573,8 +573,8 @@ func (h *Handler) handlePurge(w http.ResponseWriter, _ *http.Request) {
573573
}
574574
}
575575

576-
// ServeHTTP implement net/http.Handler.ServeHTTP.
577-
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
576+
// ServeHTTP implement net/http.HTTPHandler.ServeHTTP.
577+
func (h *HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
578578
h.lock.RLock()
579579
defer h.lock.RUnlock()
580580
h.httpHandler.ServeHTTP(w, r)

pkg/inference/scheduling/scheduler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type Scheduler struct {
3131
// defaultBackend is the default inference backend. It may be nil.
3232
defaultBackend inference.Backend
3333
// modelHandler is the shared model handler.
34-
modelHandler *models.Handler
34+
modelHandler *models.HTTPHandler
3535
// modelManager is the shared model manager.
3636
modelManager *models.Manager
3737
// installer is the backend installer.
@@ -49,7 +49,7 @@ func NewScheduler(
4949
log logging.Logger,
5050
backends map[string]inference.Backend,
5151
defaultBackend inference.Backend,
52-
handler *models.Handler,
52+
handler *models.HTTPHandler,
5353
modelManager *models.Manager,
5454
httpClient *http.Client,
5555
allowedOrigins []string,

0 commit comments

Comments
 (0)