66 "net/http"
77 "net/url"
88 "sync"
9- "time"
109
1110 "github.com/0x2e/fusion/model"
1211 "github.com/0x2e/fusion/repo"
@@ -40,7 +39,7 @@ func NewFeed(feedRepo FeedRepo, itemRepo ItemInFeedRepo) *Feed {
4039 }
4140}
4241
43- func (f Feed ) All () (* RespFeedAll , error ) {
42+ func (f Feed ) All (ctx context. Context ) (* RespFeedAll , error ) {
4443 data , err := f .feedRepo .All ()
4544 if err != nil {
4645 return nil , err
@@ -63,7 +62,7 @@ func (f Feed) All() (*RespFeedAll, error) {
6362 }, nil
6463}
6564
66- func (f Feed ) Get (req * ReqFeedGet ) (* RespFeedGet , error ) {
65+ func (f Feed ) Get (ctx context. Context , req * ReqFeedGet ) (* RespFeedGet , error ) {
6766 data , err := f .feedRepo .Get (req .ID )
6867 if err != nil {
6968 return nil , err
@@ -79,7 +78,7 @@ func (f Feed) Get(req *ReqFeedGet) (*RespFeedGet, error) {
7978 }, nil
8079}
8180
82- func (f Feed ) Create (req * ReqFeedCreate ) error {
81+ func (f Feed ) Create (ctx context. Context , req * ReqFeedCreate ) error {
8382 feeds := make ([]* model.Feed , 0 , len (req .Feeds ))
8483 for _ , r := range req .Feeds {
8584 feeds = append (feeds , & model.Feed {
@@ -109,7 +108,9 @@ func (f Feed) Create(req *ReqFeedCreate) error {
109108 routinePool <- struct {}{}
110109 wg .Add (1 )
111110 go func () {
112- puller .PullOne (feed .ID )
111+ // NOTE: do not use the incoming ctx, as it will be Done() automatically
112+ // by api timeout middleware
113+ puller .PullOne (context .Background (), feed .ID )
113114 <- routinePool
114115 wg .Done ()
115116 }()
@@ -118,13 +119,10 @@ func (f Feed) Create(req *ReqFeedCreate) error {
118119 }()
119120 return nil
120121 }
121- return puller .PullOne (feeds [0 ].ID )
122+ return puller .PullOne (ctx , feeds [0 ].ID )
122123}
123124
124- func (f Feed ) CheckValidity (req * ReqFeedCheckValidity ) (* RespFeedCheckValidity , error ) {
125- ctx , cancel := context .WithTimeout (context .Background (), 20 * time .Second )
126- defer cancel ()
127-
125+ func (f Feed ) CheckValidity (ctx context.Context , req * ReqFeedCheckValidity ) (* RespFeedCheckValidity , error ) {
128126 validLinks := make ([]ValidityItem , 0 )
129127 parsed , err := pull .Fetch (ctx , req .Link )
130128 if err == nil && parsed != nil {
@@ -153,7 +151,7 @@ func (f Feed) CheckValidity(req *ReqFeedCheckValidity) (*RespFeedCheckValidity,
153151 }, nil
154152}
155153
156- func (f Feed ) Update (req * ReqFeedUpdate ) error {
154+ func (f Feed ) Update (ctx context. Context , req * ReqFeedUpdate ) error {
157155 data := & model.Feed {
158156 Name : req .Name ,
159157 Link : req .Link ,
@@ -169,20 +167,22 @@ func (f Feed) Update(req *ReqFeedUpdate) error {
169167 return err
170168}
171169
172- func (f Feed ) Delete (req * ReqFeedDelete ) error {
170+ func (f Feed ) Delete (ctx context. Context , req * ReqFeedDelete ) error {
173171 // FIX: transaction
174172 if err := f .itemRepo .DeleteByFeed (req .ID ); err != nil {
175173 return err
176174 }
177175 return f .feedRepo .Delete (req .ID )
178176}
179177
180- func (f Feed ) Refresh (req * ReqFeedRefresh ) error {
178+ func (f Feed ) Refresh (ctx context. Context , req * ReqFeedRefresh ) error {
181179 pull := pull .NewPuller (repo .NewFeed (repo .DB ), repo .NewItem (repo .DB ))
182180 if req .ID != nil {
183- return pull .PullOne (* req .ID )
181+ return pull .PullOne (ctx , * req .ID )
184182 }
185183 if req .All != nil && * req .All {
184+ // NOTE: do not use the incoming ctx, as it will be Done() automatically
185+ // by api timeout middleware
186186 go pull .PullAll (context .Background (), true )
187187 }
188188 return nil
0 commit comments