async_request.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package needle
  2. type AsyncRequest struct {
  3. N *Needle
  4. IsWriteRequest bool
  5. ActualSize int64
  6. offset uint64
  7. size uint64
  8. doneChan chan interface{}
  9. isUnchanged bool
  10. err error
  11. }
  12. func NewAsyncRequest(n *Needle, isWriteRequest bool) *AsyncRequest {
  13. return &AsyncRequest{
  14. offset: 0,
  15. size: 0,
  16. ActualSize: 0,
  17. doneChan: make(chan interface{}),
  18. N: n,
  19. isUnchanged: false,
  20. IsWriteRequest: isWriteRequest,
  21. err: nil,
  22. }
  23. }
  24. func (r *AsyncRequest) WaitComplete() (uint64, uint64, bool, error) {
  25. <-r.doneChan
  26. return r.offset, r.size, r.isUnchanged, r.err
  27. }
  28. func (r *AsyncRequest) Complete(offset uint64, size uint64, isUnchanged bool, err error) {
  29. r.offset = offset
  30. r.size = size
  31. r.isUnchanged = isUnchanged
  32. r.err = err
  33. close(r.doneChan)
  34. }
  35. func (r *AsyncRequest) UpdateResult(offset uint64, size uint64, isUnchanged bool, err error) {
  36. r.offset = offset
  37. r.size = size
  38. r.isUnchanged = isUnchanged
  39. r.err = err
  40. }
  41. func (r *AsyncRequest) Submit() {
  42. close(r.doneChan)
  43. }
  44. func (r *AsyncRequest) IsSucceed() bool {
  45. return r.err == nil
  46. }