Skip to content

Commit 116685c

Browse files
committed
feat: change PersistentCacheTask to value type and ensure empty slice response
Signed-off-by: Gaius <gaius.qi@gmail.com>
1 parent 4265552 commit 116685c

File tree

4 files changed

+29
-25
lines changed

4 files changed

+29
-25
lines changed

manager/service/mocks/service_mock.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manager/service/persistent_cache_task.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,28 @@ func (s *service) DestroyPersistentCacheTask(ctx context.Context, schedulerClust
3838
}
3939

4040
// GetPersistentCacheTask retrieves a persistent cache task from Redis based on query parameters.
41-
func (s *service) GetPersistentCacheTask(ctx context.Context, schedulerClusterID uint, id string) (*types.PersistentCacheTask, error) {
41+
func (s *service) GetPersistentCacheTask(ctx context.Context, schedulerClusterID uint, id string) (types.PersistentCacheTask, error) {
4242
return s.loadTask(ctx, schedulerClusterID, id)
4343
}
4444

4545
// GetPersistentCacheTasks retrieves persistent cache tasks from Redis based on query parameters.
46-
func (s *service) GetPersistentCacheTasks(ctx context.Context, q types.GetPersistentCacheTasksQuery) ([]*types.PersistentCacheTask, int64, error) {
46+
func (s *service) GetPersistentCacheTasks(ctx context.Context, q types.GetPersistentCacheTasksQuery) ([]types.PersistentCacheTask, int64, error) {
4747
tasks, err := s.loadAllTasks(ctx, q.SchedulerClusterID)
4848
if err != nil {
4949
return nil, 0, err
5050
}
5151

52+
if len(tasks) == 0 {
53+
return []types.PersistentCacheTask{}, 0, nil
54+
}
55+
5256
return tasks, int64(len(tasks)), nil
5357
}
5458

5559
// loadAllTasks loads all persistent cache tasks from Redis based on the provided scheduler cluster ID.
56-
func (s *service) loadAllTasks(ctx context.Context, schedulerClusterID uint) ([]*types.PersistentCacheTask, error) {
60+
func (s *service) loadAllTasks(ctx context.Context, schedulerClusterID uint) ([]types.PersistentCacheTask, error) {
5761
var (
58-
tasks []*types.PersistentCacheTask
62+
tasks []types.PersistentCacheTask
5963
cursor uint64
6064
)
6165

@@ -126,18 +130,18 @@ func (s *service) loadAllTasks(ctx context.Context, schedulerClusterID uint) ([]
126130
}
127131

128132
// loadTask loads a task from Redis based on the provided key.
129-
func (s *service) loadTask(ctx context.Context, schedulerClusterID uint, id string) (*types.PersistentCacheTask, error) {
133+
func (s *service) loadTask(ctx context.Context, schedulerClusterID uint, id string) (types.PersistentCacheTask, error) {
130134
taskKey := pkgredis.MakePersistentCacheTaskKeyInScheduler(schedulerClusterID, id)
131135
rawTask, err := s.rdb.HGetAll(ctx, taskKey).Result()
132136
if err != nil {
133-
return nil, err
137+
return types.PersistentCacheTask{}, err
134138
}
135139

136140
if len(rawTask) == 0 {
137-
return nil, errors.New("task not found")
141+
return types.PersistentCacheTask{}, errors.New("task not found")
138142
}
139143

140-
task := &types.PersistentCacheTask{
144+
task := types.PersistentCacheTask{
141145
ID: rawTask["id"],
142146
Tag: rawTask["tag"],
143147
Application: rawTask["application"],
@@ -147,55 +151,55 @@ func (s *service) loadTask(ctx context.Context, schedulerClusterID uint, id stri
147151
// Parse PersistentReplicaCount.
148152
persistentReplicaCount, err := strconv.ParseUint(rawTask["persistent_replica_count"], 10, 64)
149153
if err != nil {
150-
return nil, err
154+
return types.PersistentCacheTask{}, err
151155
}
152156
task.PersistentReplicaCount = persistentReplicaCount
153157

154158
// Parse PieceLength.
155159
pieceLength, err := strconv.ParseUint(rawTask["piece_length"], 10, 64)
156160
if err != nil {
157-
return nil, err
161+
return types.PersistentCacheTask{}, err
158162
}
159163
task.PieceLength = pieceLength
160164

161165
// Parse ContentLength.
162166
contentLength, err := strconv.ParseUint(rawTask["content_length"], 10, 64)
163167
if err != nil {
164-
return nil, err
168+
return types.PersistentCacheTask{}, err
165169
}
166170
task.ContentLength = contentLength
167171

168172
// Parse TotalPieceCount.
169173
totalPieceCount, err := strconv.ParseUint(rawTask["total_piece_count"], 10, 32)
170174
if err != nil {
171-
return nil, err
175+
return types.PersistentCacheTask{}, err
172176
}
173177
task.TotalPieceCount = uint32(totalPieceCount)
174178

175179
// Parse TTL.
176180
ttl, err := strconv.ParseInt(rawTask["ttl"], 10, 64)
177181
if err != nil {
178-
return nil, err
182+
return types.PersistentCacheTask{}, err
179183
}
180184
task.TTL = time.Duration(ttl)
181185

182186
// Parse CreatedAt.
183187
createdAt, err := time.Parse(time.RFC3339, rawTask["created_at"])
184188
if err != nil {
185-
return nil, err
189+
return types.PersistentCacheTask{}, err
186190
}
187191
task.CreatedAt = createdAt
188192

189193
// Parse UpdatedAt.
190194
updatedAt, err := time.Parse(time.RFC3339, rawTask["updated_at"])
191195
if err != nil {
192-
return nil, err
196+
return types.PersistentCacheTask{}, err
193197
}
194198
task.UpdatedAt = updatedAt
195199

196200
peers, err := s.loadAllPeersByTaskID(ctx, schedulerClusterID, task.ID)
197201
if err != nil {
198-
return nil, err
202+
return types.PersistentCacheTask{}, err
199203
}
200204
task.Peers = peers
201205
return task, nil

manager/service/service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ type Service interface {
139139
GetPersonalAccessTokens(context.Context, types.GetPersonalAccessTokensQuery) ([]models.PersonalAccessToken, int64, error)
140140

141141
DestroyPersistentCacheTask(context.Context, uint, string) error
142-
GetPersistentCacheTask(context.Context, uint, string) (*types.PersistentCacheTask, error)
143-
GetPersistentCacheTasks(context.Context, types.GetPersistentCacheTasksQuery) ([]*types.PersistentCacheTask, int64, error)
142+
GetPersistentCacheTask(context.Context, uint, string) (types.PersistentCacheTask, error)
143+
GetPersistentCacheTasks(context.Context, types.GetPersistentCacheTasksQuery) ([]types.PersistentCacheTask, int64, error)
144144
}
145145

146146
type service struct {

manager/types/persistent_cache_task.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ type PersistentCacheTaskParams struct {
2929

3030
type DestroyPersistentCacheTaskQuery struct {
3131
// SchedulerClusterID is the scheduler cluster id of the persistent cache.
32-
SchedulerClusterID uint `json:"scheduler_cluster_id" binding:"required"`
32+
SchedulerClusterID uint `form:"scheduler_cluster_id" binding:"required"`
3333
}
3434

3535
type GetPersistentCacheTaskQuery struct {
3636
// SchedulerClusterID is the scheduler cluster id of the persistent cache.
37-
SchedulerClusterID uint `json:"scheduler_cluster_id" binding:"required"`
37+
SchedulerClusterID uint `form:"scheduler_cluster_id" binding:"required"`
3838
}
3939

4040
type GetPersistentCacheTasksQuery struct {
4141
// SchedulerClusterID is the scheduler cluster id of the persistent cache.
42-
SchedulerClusterID uint `json:"scheduler_cluster_id" binding:"required"`
42+
SchedulerClusterID uint `form:"scheduler_cluster_id" binding:"required"`
4343

4444
// Page is the page number of the persistent cache list.
4545
Page int `form:"page" binding:"omitempty,gte=1"`

0 commit comments

Comments
 (0)