@@ -10,6 +10,7 @@ import (
1010 "path"
1111 "strconv"
1212 "strings"
13+ "sync"
1314
1415 "github.com/docker/model-distribution/distribution"
1516 "github.com/docker/model-distribution/registry"
@@ -39,6 +40,8 @@ type Manager struct {
3940 distributionClient * distribution.Client
4041 // registryClient is the client for model registry.
4142 registryClient * registry.Client
43+ // lock is used to synchronize access to the models manager's router.
44+ lock sync.Mutex
4245}
4346
4447type ClientConfig struct {
@@ -100,6 +103,20 @@ func NewManager(log logging.Logger, c ClientConfig, allowedOrigins []string) *Ma
100103 return m
101104}
102105
106+ func (m * Manager ) RebuildRoutes (allowedOrigins []string ) {
107+ m .lock .Lock ()
108+ defer m .lock .Unlock ()
109+ // Clear existing routes and re-register them.
110+ m .router = http .NewServeMux ()
111+ // Register routes.
112+ m .router .HandleFunc ("/" , func (w http.ResponseWriter , _ * http.Request ) {
113+ http .Error (w , "not found" , http .StatusNotFound )
114+ })
115+ for route , handler := range m .routeHandlers (allowedOrigins ) {
116+ m .router .HandleFunc (route , handler )
117+ }
118+ }
119+
103120func (m * Manager ) routeHandlers (allowedOrigins []string ) map [string ]http.HandlerFunc {
104121 handlers := map [string ]http.HandlerFunc {
105122 "POST " + inference .ModelsPrefix + "/create" : m .handleCreateModel ,
@@ -494,6 +511,8 @@ func (m *Manager) GetDiskUsage() (int64, error, int) {
494511
495512// ServeHTTP implement net/http.Handler.ServeHTTP.
496513func (m * Manager ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
514+ m .lock .Lock ()
515+ defer m .lock .Unlock ()
497516 m .router .ServeHTTP (w , r )
498517}
499518
0 commit comments