FetchModules(ctx context.Context, ids []string) ([]*Module, error) { // Timeout timeoutCtx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() // Fetch modules := make([]*Module, 0, len(ids)) wg, mutex := &sync.WaitGroup{}, &sync.Mutex{} wg.Add(len(ids)) for i := range ids { id := ids[i] go func() { defer wg.Done() module, err := a.service.FetchModule(timeoutCtx, id) if err != nil { log.Println(err) return } mutex.Lock() modules = append(modules, module) mutex.Unlock() }() } wg.Wait() // Cache a.inmemory.BatchSet(ctx, modules) a.redis.BatchSet(ctx, modules) return modules, nil }