Golang的消息队列(MQ)的实现【大部分已填完】

请注意,本文编写于 1907 天前,最后修改于 1403 天前,其中某些信息可能已经过时。

前沿

emmmm,全盘感染和机器学习咕了。因为咱要做一个多线程并发邮局给ero小说站。而且正好为了捡起golang,所以就来设计这个了。
机器学习的话,现在还在争取能否转计算机,能的话我就能全身心投入研究了。

一个学期过来我发现,确实,音乐与其说是专业,不如说是兴趣。而我真正想投入研究的,还是计算机

需求

很明显的一个推拉模型,讲究先入先出。

要求接口

  • Init(types interface{},max int) 初始化
  • Init 判断是否初始化
  • Length 获取当前队列长度
  • Save() 保存队列到....(到哪我还没想出来)
  • Load() 提取已保存的队列
  • Push(data interface{}) 压入任意类型的数据
  • Pop() 弹出数据(堵塞形)
  • Pop2() 弹出数据(非堵塞)
type Node struct{
    nextp *Node
    value interface{}
    lastp *Node
}
type MessageQueue struct{
    init bool
    length int
    now *Node//当前指针
    head *Node
    in chan interface{}// 传入数据通道
    out chan interface{} // 传出数据通道

}
func (this *MessageQueue)Init()error{
    this.in = make(chan interface{})
    this.out = make(chan interface{})
    this.now = nil
    this.length = 0
    go func(){
        for{
            select {
            case value:=<- this.in:
                fmt.Println("In")
                node := &Node{value:value,nextp:this.now,lastp:nil}
                if this.length == 0{
                    this.head = node
                    this.now = node
                }else{

                    this.now = node
                    this.now.nextp.lastp = this.now
                }
                this.length++

            default:
                //fmt.Println("Looping")
            }
        }
    }()
    this.init = true
    return nil
}
func (this *MessageQueue)Length()int{
    return this.length
}


func (this *MessageQueue)Push(date interface{})error{
    var errorz error
    go func() {
       select{
       case this.in<-date:
          errorz = nil
       default:
          errorz = errors.New("Have a error")
          //fmt.Println("Push error")
       }
       this.in<-date
    }()
    //this.in<-date
    return errorz

}
//非堵塞
func (this *MessageQueue)Pop()(interface{},error){
    var errorz error
    if this.length == 0{
        errorz = errors.New("queue is null")
        return nil,errorz
    }
    value:= this.head
    this.head = this.head.lastp
    this.length--
    return value.value,nil
}
//堵塞
func (this *MessageQueue)Pop2(timeoutSecs int)error{
    return nil
}

func (this *MessageQueue)Save(){

}
func (this *MessageQueue)Load() {

}

添加新评论

评论列表