Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit dac1c3e

Browse files
authored
Utilizing quesma v2 lifecycle api (#1134)
This PR: - for the first time utilize quesma v2 lifecycle APIs (start/stop) without using ad hoc http server. The same thing we have to do for TCP frontend connector. Finally, we should have one `quesmaV2` object at `constructQuesma` level. - fixes some issues with updating mapping handlers
1 parent b05ce0f commit dac1c3e

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

quesma/quesma/dual_write_proxy_v2.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package quesma
44

55
import (
66
"context"
7-
"errors"
87
"net/http"
98
"quesma/ab_testing"
109
"quesma/clickhouse"
@@ -50,7 +49,7 @@ func (c *simultaneousClientsLimiterV2) ServeHTTP(w http.ResponseWriter, r *http.
5049
}
5150

5251
type dualWriteHttpProxyV2 struct {
53-
routingHttpServer *http.Server
52+
quesmaV2 quesma_api.QuesmaBuilder
5453
indexManagement elasticsearch.IndexManagement
5554
logManager *clickhouse.LogManager
5655
publicPort util.Port
@@ -91,33 +90,27 @@ func newDualWriteProxyV2(dependencies quesma_api.Dependencies, schemaLoader clic
9190

9291
queryPipeline := quesma_api.NewPipeline()
9392
queryPipeline.AddFrontendConnector(elasticHttpQueryFrontendConnector)
94-
quesmaBuilder.AddPipeline(ingestPipeline)
9593
quesmaBuilder.AddPipeline(queryPipeline)
94+
quesmaBuilder.AddPipeline(ingestPipeline)
9695

97-
_, err := quesmaBuilder.Build()
96+
quesmaV2, err := quesmaBuilder.Build()
9897
if err != nil {
9998
logger.Fatal().Msgf("Error building Quesma: %v", err)
10099
}
101-
var limitedHandler http.Handler
102100
if config.DisableAuth {
103101
elasticHttpIngestFrontendConnector.AddMiddleware(newSimultaneousClientsLimiterV2(concurrentClientsLimitV2))
104102
elasticHttpQueryFrontendConnector.AddMiddleware(newSimultaneousClientsLimiterV2(concurrentClientsLimitV2))
105-
limitedHandler = elasticHttpIngestFrontendConnector
106103
} else {
107104
elasticHttpQueryFrontendConnector.AddMiddleware(newSimultaneousClientsLimiterV2(concurrentClientsLimitV2))
108105
elasticHttpQueryFrontendConnector.AddMiddleware(NewAuthMiddlewareV2(config.Elasticsearch))
109106
elasticHttpIngestFrontendConnector.AddMiddleware(newSimultaneousClientsLimiterV2(concurrentClientsLimitV2))
110107
elasticHttpIngestFrontendConnector.AddMiddleware(NewAuthMiddlewareV2(config.Elasticsearch))
111-
limitedHandler = elasticHttpIngestFrontendConnector
112108
}
113109

114110
return &dualWriteHttpProxyV2{
115-
schemaRegistry: registry,
116-
schemaLoader: schemaLoader,
117-
routingHttpServer: &http.Server{
118-
Addr: ":" + strconv.Itoa(int(config.PublicTcpPort)),
119-
Handler: limitedHandler,
120-
},
111+
schemaRegistry: registry,
112+
schemaLoader: schemaLoader,
113+
quesmaV2: quesmaV2,
121114
indexManagement: indexManager,
122115
logManager: logManager,
123116
publicPort: config.PublicTcpPort,
@@ -139,20 +132,13 @@ func (q *dualWriteHttpProxyV2) Close(ctx context.Context) {
139132
if q.asyncQueriesEvictor != nil {
140133
q.asyncQueriesEvictor.Close()
141134
}
142-
if err := q.routingHttpServer.Shutdown(ctx); err != nil {
143-
logger.Fatal().Msgf("Error during server shutdown: %v", err)
144-
}
135+
q.quesmaV2.Stop(ctx)
145136
}
146137

147138
func (q *dualWriteHttpProxyV2) Ingest() {
148139
q.schemaLoader.ReloadTableDefinitions()
149140
q.logManager.Start()
150141
q.indexManagement.Start()
151142
go q.asyncQueriesEvictor.AsyncQueriesGC()
152-
go func() {
153-
if err := q.routingHttpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
154-
logger.Fatal().Msgf("Error starting http server: %v", err)
155-
}
156-
logger.Info().Msgf("Accepting HTTP at :%d", q.publicPort)
157-
}()
143+
q.quesmaV2.Start()
158144
}

quesma/v2/core/mux.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/ucarion/urlpath"
77
"net/http"
88
"net/url"
9+
"sort"
910
"strings"
1011
)
1112

@@ -205,14 +206,20 @@ func (p *PathRouter) GetHandlers() map[string]HandlersPipe {
205206
func (p *PathRouter) SetHandlers(handlers map[string]HandlersPipe) {
206207
newHandlers := make(map[string]HandlersPipe, 0)
207208
for path, handler := range handlers {
208-
for index := range p.mappings {
209+
var index int
210+
var found bool
211+
for index = range p.mappings {
209212
if p.mappings[index].pattern == path {
210-
p.mappings[index].handler.Processors = handler.Processors
211-
p.mappings[index].handler.Predicate = handler.Predicate
212-
} else {
213-
newHandlers[path] = handler
213+
found = true
214+
break
214215
}
215216
}
217+
if found {
218+
p.mappings[index].handler.Processors = handler.Processors
219+
p.mappings[index].handler.Predicate = handler.Predicate
220+
} else {
221+
newHandlers[path] = handler
222+
}
216223
}
217224
for path, handler := range newHandlers {
218225
p.mappings = append(p.mappings, mapping{pattern: path,
@@ -222,4 +229,9 @@ func (p *PathRouter) SetHandlers(handlers map[string]HandlersPipe) {
222229
Predicate: handler.Predicate,
223230
Processors: handler.Processors}})
224231
}
232+
// mappings needs to be sorted as literal paths should be matched first
233+
// for instance /_search should be matched before /:index
234+
sort.Slice(p.mappings, func(i, j int) bool {
235+
return p.mappings[i].pattern > p.mappings[j].pattern
236+
})
225237
}

0 commit comments

Comments
 (0)