From 45a47eafb5672bace632300e626fcaa191cf1998 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Tue, 18 Oct 2022 13:30:50 +0200 Subject: [PATCH] allow multiple event consumers per search instance Signed-off-by: jkoberg --- services/search/pkg/config/search.go | 1 + services/search/pkg/search/events.go | 76 +++++++++++++++------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/services/search/pkg/config/search.go b/services/search/pkg/config/search.go index 25845a253a1..3bf65304270 100644 --- a/services/search/pkg/config/search.go +++ b/services/search/pkg/config/search.go @@ -5,6 +5,7 @@ type Events struct { Endpoint string `yaml:"endpoint" env:"SEARCH_EVENTS_ENDPOINT" desc:"The address of the event system. The event system is the message queuing service. It is used as message broker for the microservice architecture."` Cluster string `yaml:"cluster" env:"SEARCH_EVENTS_CLUSTER" desc:"The clusterID of the event system. The event system is the message queuing service. It is used as message broker for the microservice architecture. Mandatory when using NATS as event system."` AsyncUploads bool `yaml:"async_uploads" env:"STORAGE_USERS_OCIS_ASYNC_UPLOADS;SEARCH_EVENTS_ASYNC_UPLOADS" desc:"Enable asynchronous file uploads."` + NumConsumers int `yaml:"num_consumers" env:"SEARCH_EVENTS_NUM_CONSUMERS" desc:"number of event consumers per service instance"` TLSInsecure bool `yaml:"tls_insecure" env:"OCIS_INSECURE;SEARCH_EVENTS_TLS_INSECURE" desc:"Whether to verify the server TLS certificates."` TLSRootCACertificate string `yaml:"tls_root_ca_certificate" env:"SEARCH_EVENTS_TLS_ROOT_CA_CERTIFICATE" desc:"The root CA certificate used to validate the server's TLS certificate. If provided SEARCH_EVENTS_TLS_INSECURE will be seen as false."` diff --git a/services/search/pkg/search/events.go b/services/search/pkg/search/events.go index b376d6ba36d..1717657aa71 100644 --- a/services/search/pkg/search/events.go +++ b/services/search/pkg/search/events.go @@ -48,43 +48,49 @@ func HandleEvents(eng engine.Engine, extractor content.Extractor, gw gateway.Gat return err } - go func(eh *eventHandler, ch <-chan interface{}) { - for e := range ch { - eh.logger.Debug().Interface("event", e).Msg("updating index") + if cfg.Events.NumConsumers == 0 { + cfg.Events.NumConsumers = 1 + } - switch ev := e.(type) { - case events.ItemTrashed: - eh.trashItem(ev.ID) - case events.ItemMoved: - eh.moveItem(ev.Ref, ev.Executant) - case events.ItemRestored: - eh.restoreItem(ev.Ref, ev.Executant) - case events.ContainerCreated: - eh.upsertItem(ev.Ref, ev.Executant) - case events.FileTouched: - eh.upsertItem(ev.Ref, ev.Executant) - case events.FileVersionRestored: - eh.upsertItem(ev.Ref, ev.Executant) - case events.FileUploaded: - eh.upsertItem(ev.Ref, ev.Executant) - case events.UploadReady: - eh.upsertItem(ev.FileRef, ev.ExecutingUser.Id) - case events.TagsAdded: - eh.upsertItem(ev.Ref, ev.Executant) - case events.TagsRemoved: - eh.upsertItem(ev.Ref, ev.Executant) + for i := 0; i < cfg.Events.NumConsumers; i++ { + go func(eh *eventHandler, ch <-chan interface{}) { + for e := range ch { + eh.logger.Debug().Interface("event", e).Msg("updating index") + + switch ev := e.(type) { + case events.ItemTrashed: + eh.trashItem(ev.ID) + case events.ItemMoved: + eh.moveItem(ev.Ref, ev.Executant) + case events.ItemRestored: + eh.restoreItem(ev.Ref, ev.Executant) + case events.ContainerCreated: + eh.upsertItem(ev.Ref, ev.Executant) + case events.FileTouched: + eh.upsertItem(ev.Ref, ev.Executant) + case events.FileVersionRestored: + eh.upsertItem(ev.Ref, ev.Executant) + case events.FileUploaded: + eh.upsertItem(ev.Ref, ev.Executant) + case events.UploadReady: + eh.upsertItem(ev.FileRef, ev.ExecutingUser.Id) + case events.TagsAdded: + eh.upsertItem(ev.Ref, ev.Executant) + case events.TagsRemoved: + eh.upsertItem(ev.Ref, ev.Executant) + } } - } - }( - &eventHandler{ - logger: logger, - engine: eng, - secret: cfg.MachineAuthAPIKey, - gateway: gw, - extractor: extractor, - }, - ch, - ) + }( + &eventHandler{ + logger: logger, + engine: eng, + secret: cfg.MachineAuthAPIKey, + gateway: gw, + extractor: extractor, + }, + ch, + ) + } return nil }