Сборка ответа из двух параллельных запросов в хранилище
Код-ревьюMiddle20 минпроверка — Спринт10-t038
Метод Get параллельно (двумя горутинами) запрашивает Storage.Info и Storage.Price и должен склеить результат в список записей. Что здесь не так и как исправить? Обратите внимание на синхронизацию, передачу контекста, обработку ошибок, типы и логику сборки.
Как на собесе: 20 минут, ищи всё — гонки, утечки, ошибки, безопасность, стиль. Ссылайся на строки.
review.gogo
1packagedemo23import(4"context"5)67typeStorageinterface{8Info(ids[]int)(map[int]*InfoRow,error)9Price(ctxcontext.Context,ids[]int)(map[int]*PriceRow,error)10}1112typeInfoRowstruct{13IDint14Createduint815}1617typePriceRowstruct{18IDint19Valuefloat6420}2122typeRecordstruct{23IDint24Info*InfoRow25Price*PriceRow26}2728typeGetReqstruct{29IDs[]int3230}3132typeGetRespstruct{33Records[]*Record34}3536typeServerstruct{37storageStorage38}3940func(sServer)Get(ctxcontext.Context,reqGetReq)(*GetResp,error){41varfailureerror4243varinfoByIDmap[int]*InfoRow44gofunc(){45m,e:=s.storage.Info(toIntIDs(req.IDs))46ife!=nil{47failure=e48return49}50infoByID=m51}()5253varpriceByIDmap[int]*PriceRow54gofunc(){55m,e:=s.storage.Price(context.Background(),toIntIDs(req.IDs))56ife!=nil{57failure=e58return59}60priceByID=m61}()6263iffailure!=nil{64returnnil,failure65}6667out:=[]*Record{}68for_,id:=rangereq.IDs{69ifinfo,ok:=infoByID[int(id)];ok{70out=append(out,&Record{ID:int(id),Info:info})71}7273ifprice,ok:=priceByID[int(id)];ok{74out=append(out,&Record{ID:int(id),Price:price})75}76}7778return&GetResp{Records:out},nil79}8081functoIntIDs(ids[]int32)[]int{82// переводит []int32 в []int83returnnil84}