Skip to content

Commit 35aa214

Browse files
committed
refactor(host): 移除多余本地接口
1 parent c909ff9 commit 35aa214

3 files changed

Lines changed: 68 additions & 26 deletions

File tree

backend/biz/host/handler/v1/internal.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ type InternalHostHandler struct {
3535
teamRepo domain.TeamHostRepo
3636
redis *redis.Client
3737
getAgentToken agentTokenGetter
38-
limiter vmDeleteLimiter
39-
vmDeleter vmDeleter
38+
limiter *redis.Client
39+
vmDeleter taskflow.VirtualMachiner
4040
skipSoftDelete func(context.Context) context.Context
4141
cache *cache.Cache
4242
taskLifecycle *lifecycle.Manager[uuid.UUID, consts.TaskStatus, lifecycle.TaskMetadata]

backend/biz/host/handler/v1/internal_auth.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ const (
1919

2020
var errAgentVMRecycled = errors.New("agent vm is recycled")
2121

22-
type vmDeleteLimiter interface {
23-
SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.BoolCmd
24-
}
25-
26-
type vmDeleter interface {
27-
Delete(ctx context.Context, req *taskflow.DeleteVirtualMachineReq) error
28-
}
29-
3022
type agentTokenGetter func(ctx context.Context, key string) (string, error)
3123

3224
func defaultAgentTokenGetter(rdb *redis.Client) agentTokenGetter {

backend/biz/host/handler/v1/internal_auth_test.go

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99
"time"
1010

11+
"github.com/alicebob/miniredis/v2"
1112
"github.com/google/uuid"
1213
"github.com/redis/go-redis/v9"
1314

@@ -17,6 +18,7 @@ import (
1718
)
1819

1920
func TestAgentAuthRecycledVMTriggersDeleteOnce(t *testing.T) {
21+
rdb := newTestRedis(t)
2022
vmClient := &vmDeleterStub{ch: make(chan struct{}, 1)}
2123
handler := &InternalHostHandler{
2224
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
@@ -32,7 +34,7 @@ func TestAgentAuthRecycledVMTriggersDeleteOnce(t *testing.T) {
3234
},
3335
},
3436
vmDeleter: vmClient,
35-
limiter: &setNXLimiterStub{result: true},
37+
limiter: rdb,
3638
skipSoftDelete: func(ctx context.Context) context.Context { return ctx },
3739
}
3840

@@ -50,6 +52,10 @@ func TestAgentAuthRecycledVMTriggersDeleteOnce(t *testing.T) {
5052
}
5153

5254
func TestAgentAuthRecycledVMLimitedSkipsDelete(t *testing.T) {
55+
rdb := newTestRedis(t)
56+
if ok, err := rdb.SetNX(context.Background(), "vm:recycle:retry:agent_2", "1", time.Minute).Result(); err != nil || !ok {
57+
t.Fatalf("seed redis limiter failed, ok=%v err=%v", ok, err)
58+
}
5359
vmClient := &vmDeleterStub{ch: make(chan struct{}, 1)}
5460
handler := &InternalHostHandler{
5561
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
@@ -65,7 +71,7 @@ func TestAgentAuthRecycledVMLimitedSkipsDelete(t *testing.T) {
6571
},
6672
},
6773
vmDeleter: vmClient,
68-
limiter: &setNXLimiterStub{result: false},
74+
limiter: rdb,
6975
skipSoftDelete: func(ctx context.Context) context.Context { return ctx },
7076
}
7177

@@ -79,6 +85,7 @@ func TestAgentAuthRecycledVMLimitedSkipsDelete(t *testing.T) {
7985
}
8086

8187
func TestAgentAuthSoftDeletedRecycledVMStillTriggersDelete(t *testing.T) {
88+
rdb := newTestRedis(t)
8289
vmClient := &vmDeleterStub{ch: make(chan struct{}, 1)}
8390
skipCalled := false
8491
type testSkipMarkerKey struct{}
@@ -101,7 +108,7 @@ func TestAgentAuthSoftDeletedRecycledVMStillTriggersDelete(t *testing.T) {
101108
getAgentToken: func(context.Context, string) (string, error) { return "", redis.Nil },
102109
repo: repo,
103110
vmDeleter: vmClient,
104-
limiter: &setNXLimiterStub{result: true},
111+
limiter: rdb,
105112
skipSoftDelete: func(ctx context.Context) context.Context {
106113
skipCalled = true
107114
return context.WithValue(ctx, markerKey, markerValue)
@@ -204,19 +211,6 @@ func (s *internalHostRepoStub) GetGitCredentialByTask(context.Context, string) (
204211
return nil, errors.New("task not found")
205212
}
206213

207-
type setNXLimiterStub struct {
208-
result bool
209-
err error
210-
keys []string
211-
ttl time.Duration
212-
}
213-
214-
func (s *setNXLimiterStub) SetNX(_ context.Context, key string, _ interface{}, ttl time.Duration) *redis.BoolCmd {
215-
s.keys = append(s.keys, key)
216-
s.ttl = ttl
217-
return redis.NewBoolResult(s.result, s.err)
218-
}
219-
220214
type vmDeleterStub struct {
221215
reqs []*taskflow.DeleteVirtualMachineReq
222216
err error
@@ -235,6 +229,46 @@ func (s *vmDeleterStub) Delete(_ context.Context, req *taskflow.DeleteVirtualMac
235229
return s.err
236230
}
237231

232+
func (s *vmDeleterStub) Create(context.Context, *taskflow.CreateVirtualMachineReq) (*taskflow.VirtualMachine, error) {
233+
return nil, errors.New("not implemented")
234+
}
235+
236+
func (s *vmDeleterStub) Hibernate(context.Context, *taskflow.HibernateVirtualMachineReq) error {
237+
return errors.New("not implemented")
238+
}
239+
240+
func (s *vmDeleterStub) Resume(context.Context, *taskflow.ResumeVirtualMachineReq) error {
241+
return errors.New("not implemented")
242+
}
243+
244+
func (s *vmDeleterStub) List(context.Context, string) ([]*taskflow.VirtualMachine, error) {
245+
return nil, errors.New("not implemented")
246+
}
247+
248+
func (s *vmDeleterStub) Info(context.Context, taskflow.VirtualMachineInfoReq) (*taskflow.VirtualMachine, error) {
249+
return nil, errors.New("not implemented")
250+
}
251+
252+
func (s *vmDeleterStub) Terminal(context.Context, *taskflow.TerminalReq) (taskflow.Sheller, error) {
253+
return nil, errors.New("not implemented")
254+
}
255+
256+
func (s *vmDeleterStub) Reports(context.Context, taskflow.ReportSubscribeReq) (taskflow.Reporter, error) {
257+
return nil, errors.New("not implemented")
258+
}
259+
260+
func (s *vmDeleterStub) TerminalList(context.Context, string) ([]*taskflow.Terminal, error) {
261+
return nil, errors.New("not implemented")
262+
}
263+
264+
func (s *vmDeleterStub) CloseTerminal(context.Context, *taskflow.CloseTerminalReq) error {
265+
return errors.New("not implemented")
266+
}
267+
268+
func (s *vmDeleterStub) IsOnline(context.Context, *taskflow.IsOnlineReq[string]) (*taskflow.IsOnlineResp, error) {
269+
return nil, errors.New("not implemented")
270+
}
271+
238272
func (s *vmDeleterStub) waitReqs(t *testing.T, timeout time.Duration) []*taskflow.DeleteVirtualMachineReq {
239273
t.Helper()
240274
select {
@@ -254,3 +288,19 @@ func (s *vmDeleterStub) hasReqWithin(timeout time.Duration) bool {
254288
return false
255289
}
256290
}
291+
292+
func newTestRedis(t *testing.T) *redis.Client {
293+
t.Helper()
294+
295+
mr, err := miniredis.Run()
296+
if err != nil {
297+
t.Fatalf("miniredis.Run() error = %v", err)
298+
}
299+
t.Cleanup(mr.Close)
300+
301+
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
302+
t.Cleanup(func() {
303+
_ = rdb.Close()
304+
})
305+
return rdb
306+
}

0 commit comments

Comments
 (0)