From a3ed04247c38defece45d734c06d9ff6c88c804d Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 4 Sep 2024 15:42:35 +0200 Subject: [PATCH] Make structifyStream() channel types more explicit, e.g. input only This enforces which side (caller/function) can read and especially close/write to a particular channel at compile time. That leaves less room for bugs (which had to be detected by tests otherwise). --- pkg/icingadb/runtime_updates.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/icingadb/runtime_updates.go b/pkg/icingadb/runtime_updates.go index fed4591ed..46d7bbe29 100644 --- a/pkg/icingadb/runtime_updates.go +++ b/pkg/icingadb/runtime_updates.go @@ -301,17 +301,24 @@ func (r *RuntimeUpdates) xRead(ctx context.Context, updateMessagesByKey map[stri // those messages into Icinga DB entities (contracts.Entity) using the provided structifier. // Converted entities are inserted into the upsertEntities or deleteIds channel depending on the "runtime_type" message field. func structifyStream( - ctx context.Context, updateMessages <-chan redis.XMessage, upsertEntities, upserted chan database.Entity, - deleteIds, deleted chan interface{}, structifier structify.MapStructifier, + ctx context.Context, + updateMessages <-chan redis.XMessage, + upsertEntities chan<- database.Entity, + upserted <-chan database.Entity, + deleteIds chan<- any, + deleted <-chan any, + structifier structify.MapStructifier, ) func() error { if upserted == nil { - upserted = make(chan database.Entity) - close(upserted) + ch := make(chan database.Entity) + close(ch) + upserted = ch } if deleted == nil { - deleted = make(chan interface{}) - close(deleted) + ch := make(chan any) + close(ch) + deleted = ch } return func() error {