Skip to content

Conversation

@ComixHe
Copy link
Contributor

@ComixHe ComixHe commented Jan 30, 2026

  • Add cancelReason type to distinguish between timeout, user cancellation, etc.
  • Refactor cancellation channel, use cancelReason instead of bool type
  • Improve transfer progress handling, accurately track user cancellation
  • Optimize session acceptance logic, reduce lock holding time
  • Fix typo recevieChMu -> receiveChMu
  • Adjust notification timeout to 30 seconds
  • Improve notification timeout parameter type, use milliseconds consistently

Pms: BUG-333027
Log: optimize file transfer cancellation mechanism and notification handling

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @ComixHe, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

…ification handling

- Add cancelReason type to distinguish between timeout, user cancellation, etc.
- Refactor cancellation channel, use cancelReason instead of bool type
- Improve transfer progress handling, accurately track user cancellation
- Optimize session acceptance logic, reduce lock holding time
- Fix typo recevieChMu -> receiveChMu
- Adjust notification timeout to 30 seconds
- Improve notification timeout parameter type, use milliseconds consistently

Pms: BUG-333027
Log: optimize file transfer cancellation mechanism and notification handling
Signed-off-by: ComixHe <heyuming@deepin.org>
@deepin-ci-robot
Copy link

deepin pr auto review

这份代码主要是对蓝牙OBEX代理(Agent)功能的重构和改进,涉及文件接收、通知处理、取消操作等逻辑。以下是对代码的详细审查意见:

1. 语法逻辑

  1. cancelReason 类型定义

    • 定义了 cancelReason 类型及其常量,符合 Go 语言规范。
    • String() 方法提供了友好的错误信息,便于调试和日志记录。
  2. obexAgent 结构体修改

    • 移除了 requestNotifyChisCancel 字段,新增了 cancelChcancelChMu,逻辑更清晰。
    • 修复了 recevieChMu 的拼写错误(应为 receiveChMu)。
  3. isSessionAccepted 方法

    • 优化了锁的粒度,避免在持有锁时执行耗时操作(如 requestReceive)。
    • 增加了对 transfernil 的检查,避免空指针异常。
  4. receiveProgress 方法

    • 重构了取消逻辑,使用 isThisTransferCanceled 局部变量替代全局 isCancel,减少竞争条件。
    • 优化了 receiveCh 的发送逻辑,避免阻塞(使用 select + default)。
  5. requestReceive 方法

    • 新增了对 cancelReason 的处理,支持超时、用户取消等场景。
    • 修复了 requestNotifyCh 的初始化逻辑,避免重复创建。

2. 代码质量

  1. 命名规范

    • cancelReason 和其常量命名清晰,符合 Go 语言规范。
    • isThisTransferCanceled 变量名语义明确。
  2. 错误处理

    • 对 D-Bus 调用和文件操作的错误处理较为完善。
    • 日志记录(logger.Warning)有助于调试。
  3. 代码复用

    • cancelReasonString() 方法提高了代码可读性。
    • receiveProgress 中对 notify 的处理逻辑更加统一。
  4. 注释

    • 添加了 cancelReason 的参考链接,便于理解其来源。
    • TODO 注释标记了潜在改进点(监控 session 移除事件)。

3. 代码性能

  1. 锁的优化

    • isSessionAccepted 方法中减少了锁的持有时间,避免阻塞其他操作。
    • receiveProgress 中使用局部变量 notifyMu 替代全局锁,减少竞争。
  2. 通道操作

    • receiveCh 的发送逻辑使用 select + default 避免阻塞。
    • cancelCh 的初始化和清理逻辑更加合理。
  3. 超时处理

    • receiveFileNotifyTimeout 的计算方式改为 int32(30 * time.Second / time.Millisecond),更直观。

4. 代码安全

  1. 并发安全

    • 对共享资源(如 acceptedSessionsreceiveChcancelCh)的访问均通过互斥锁保护。
    • isSessionAccepted 方法中避免在持有锁时调用外部方法。
  2. 空指针检查

    • transfernotify 的空指针检查较为完善。
  3. 资源清理

    • receiveProgress 中移除了 notify.RemoveAllHandlers(),改为只移除当前处理器,避免误删其他处理器。
    • requestReceive 中对 cancelCh 的清理逻辑更加安全。

5. 改进建议

  1. receiveProgress 方法

    • 可以考虑将 isThisTransferCanceled 的检查逻辑提取为单独的方法,提高可读性。
    • TODO 注释中提到的监控 session 移除事件是一个很好的改进方向。
  2. requestReceive 方法

    • 可以将 cancelCh 的初始化和清理逻辑封装为单独的方法,减少重复代码。
  3. 常量定义

    • receiveFileNotifyTimeout 的计算方式可以改为常量表达式(如 const receiveFileNotifyTimeout = 30000),避免运行时计算。
  4. 错误信息

    • 可以统一错误信息的格式(如 fmt.Errorf("failed to cancel transfer: %v", err)),便于日志分析。
  5. 测试覆盖

    • 建议为 cancelReasonString() 方法和 isSessionAccepted 方法添加单元测试。

总结

这份代码在语法逻辑、代码质量、性能和安全性方面均有明显改进,尤其是并发控制和资源清理逻辑更加合理。建议进一步优化部分方法的可读性,并增加单元测试覆盖。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: ComixHe, fly602

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ComixHe ComixHe merged commit e48aaab into linuxdeepin:master Jan 31, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants