message.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package snap
  15. import (
  16. "io"
  17. "github.com/coreos/etcd/raft/raftpb"
  18. )
  19. // Message is a struct that contains a raft Message and a ReadCloser. The type
  20. // of raft message MUST be MsgSnap, which contains the raft meta-data and an
  21. // additional data []byte field that contains the snapshot of the actual state
  22. // machine.
  23. // Message contains the ReadCloser field for handling large snapshot. This avoid
  24. // copying the entire snapshot into a byte array, which consumes a lot of memory.
  25. //
  26. // User of Message should close the Message after sending it.
  27. type Message struct {
  28. raftpb.Message
  29. ReadCloser io.ReadCloser
  30. closeC chan bool
  31. }
  32. func NewMessage(rs raftpb.Message, rc io.ReadCloser) *Message {
  33. return &Message{
  34. Message: rs,
  35. ReadCloser: rc,
  36. closeC: make(chan bool, 1),
  37. }
  38. }
  39. // CloseNotify returns a channel that receives a single value
  40. // when the message sent is finished. true indicates the sent
  41. // is successful.
  42. func (m Message) CloseNotify() <-chan bool {
  43. return m.closeC
  44. }
  45. func (m Message) CloseWithError(err error) {
  46. m.ReadCloser.Close()
  47. if err == nil {
  48. m.closeC <- true
  49. } else {
  50. m.closeC <- false
  51. }
  52. }