Skip to content

Commit 54e9daf

Browse files
committed
fix concurrent map read/write in runtime app
1 parent ac47073 commit 54e9daf

36 files changed

+1189
-892
lines changed

acceptance/petstore_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (suite *PetStoreSuite) TestKafka_TopicConfig() {
191191
require.Equal(suite.T(), "petstore.order-event", r.Topics[0].Name)
192192
require.Len(suite.T(), r.Topics[0].Partitions, 2)
193193

194-
require.Len(suite.T(), suite.cmd.App.Http, 1)
194+
require.Len(suite.T(), suite.cmd.App.Http.List(), 1)
195195
}
196196

197197
func (suite *PetStoreSuite) TestKafka_Produce_InvalidFormat() {

api/handler.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (h *handler) getServices(w http.ResponseWriter, _ *http.Request) {
164164
services := make([]interface{}, 0)
165165
services = append(services, getHttpServices(h.app.Http, h.app.Monitor)...)
166166
services = append(services, getKafkaServices(h.app.Kafka, h.app.Monitor)...)
167-
services = append(services, getMailServices(h.app.Smtp, h.app.Monitor)...)
167+
services = append(services, getMailServices(h.app.Mail, h.app.Monitor)...)
168168
services = append(services, getLdapServices(h.app.Ldap, h.app.Monitor)...)
169169
slices.SortFunc(services, func(a interface{}, b interface{}) int {
170170
return compareService(a, b)
@@ -187,16 +187,16 @@ func (h *handler) getInfo(w http.ResponseWriter, _ *http.Request) {
187187
w.Header().Set("Content-Type", "application/json")
188188

189189
i := info{Version: h.app.Version, BuildTime: h.app.BuildTime}
190-
if len(h.app.Http) > 0 {
190+
if len(h.app.Http.List()) > 0 {
191191
i.ActiveServices = append(i.ActiveServices, "http")
192192
}
193-
if len(h.app.Kafka) > 0 {
193+
if len(h.app.Kafka.List()) > 0 {
194194
i.ActiveServices = append(i.ActiveServices, "kafka")
195195
}
196-
if len(h.app.Smtp) > 0 {
196+
if len(h.app.Mail.List()) > 0 {
197197
i.ActiveServices = append(i.ActiveServices, "smtp")
198198
}
199-
if len(h.app.Ldap) > 0 {
199+
if len(h.app.Ldap.List()) > 0 {
200200
i.ActiveServices = append(i.ActiveServices, "ldap")
201201
}
202202

api/handler_fileserver_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
158158
name: "http service",
159159
test: func(t *testing.T) {
160160
app := runtime.New()
161-
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0", openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."))})
161+
app.Http.Add(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0", openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."))})
162162
h := api.New(app, static.Api{Path: "/mokapi", Dashboard: true})
163163
try.Handler(t,
164164
http.MethodGet,
@@ -177,7 +177,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
177177
name: "http service path without summary and description",
178178
test: func(t *testing.T) {
179179
app := runtime.New()
180-
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
180+
app.Http.Add(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
181181
openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."),
182182
openapitest.WithPath("/pet/{petId}", openapitest.NewPath()),
183183
)},
@@ -200,7 +200,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
200200
name: "http service path with summary and description",
201201
test: func(t *testing.T) {
202202
app := runtime.New()
203-
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
203+
app.Http.Add(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
204204
openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."),
205205
openapitest.WithPath("/pet/{petId}", openapitest.NewPath(
206206
openapitest.WithPathInfo("foo", "bar"),
@@ -225,7 +225,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
225225
name: "http service path with no summary but description",
226226
test: func(t *testing.T) {
227227
app := runtime.New()
228-
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
228+
app.Http.Add(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
229229
openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."),
230230
openapitest.WithPath("/pet/{petId}", openapitest.NewPath(
231231
openapitest.WithPathInfo("", "bar"),
@@ -249,7 +249,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
249249
name: "http service endpoint no summary and no description",
250250
test: func(t *testing.T) {
251251
app := runtime.New()
252-
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
252+
app.Http.Add(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
253253
openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."),
254254
openapitest.WithPath("/pet/{petId}", openapitest.NewPath(
255255
openapitest.WithOperation("GET", openapitest.NewOperation()),
@@ -273,7 +273,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
273273
name: "http service endpoint get right path",
274274
test: func(t *testing.T) {
275275
app := runtime.New()
276-
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
276+
app.Http.Add(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
277277
openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."),
278278
openapitest.WithPath("/pet/{petId}", openapitest.NewPath(
279279
openapitest.WithOperation("GET", openapitest.NewOperation()),

api/handler_http.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ type server struct {
8383
Description string `json:"description"`
8484
}
8585

86-
func getHttpServices(services map[string]*runtime.HttpInfo, m *monitor.Monitor) []interface{} {
87-
result := make([]interface{}, 0, len(services))
88-
for _, hs := range services {
86+
func getHttpServices(store *runtime.HttpStore, m *monitor.Monitor) []interface{} {
87+
list := store.List()
88+
result := make([]interface{}, 0, len(list))
89+
for _, hs := range list {
8990
s := service{
9091
Name: hs.Info.Name,
9192
Description: hs.Info.Description,
@@ -115,8 +116,8 @@ func (h *handler) getHttpService(w http.ResponseWriter, r *http.Request, m *moni
115116
segments := strings.Split(r.URL.Path, "/")
116117
name := segments[4]
117118

118-
s, ok := h.app.Http[name]
119-
if !ok {
119+
s := h.app.Http.Get(name)
120+
if s == nil {
120121
w.WriteHeader(404)
121122
return
122123
}

0 commit comments

Comments
 (0)