這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
訊息佇列由生產者和消費者共同協作,生產者產生訊息放入隊列中,消費者從隊列中取出訊息。
1.消費者type Producer struct { id int64 addr string conn producerConn config Config logger logger logLvl LogLevel logGuard sync.RWMutex responseChan chan []byte errorChan chan []byte closeChan chan int transactionChan chan *ProducerTransaction transactions []*ProducerTransaction state int32 concurrentProducers int32 stopFlag int32 exitChan chan int wg sync.WaitGroup guard sync.Mutex}func TestProducerConnection(t *testing.T) { config := NewConfig() laddr := "127.0.0.1" config.LocalAddr, _ = net.ResolveTCPAddr("tcp", laddr+":0") w, _ := NewProducer("127.0.0.1:4150", config) w.SetLogger(nullLogger, LogLevelInfo) err := w.Publish("write_test", []byte("test")) if err != nil { t.Fatalf("should lazily connect - %s", err) } w.Stop() err = w.Publish("write_test", []byte("fail test")) if err != ErrStopped { t.Fatalf("should not be able to write after Stop()") }}