Example #1
0
func (hc *httpChannel) chunkPush() {
	u := hc.pushurl
	var ticker *time.Ticker
	closePush := func() {
		//ticker := time.NewTicker(10 * time.Second)
		select {
		case <-ticker.C:
			if hc.pushing {
				//force push channel close
				hc.chunkChan.offer(nil)
			}
		}
	}

	for {
		if len(hc.chunkChan.chunkChannel) == 0 {
			time.Sleep(10 * time.Millisecond)
			continue
		}
		hc.pushing = true
		req := buildHTTPReq(u, hc.chunkChan)
		req.ContentLength = -1
		wAuth := proxy.NewAuthEvent(u.Scheme == "https")
		wAuth.Index = int64(hc.idx)
		wAuth.IV = hc.cryptoCtx.EncryptIV
		var buf bytes.Buffer
		event.EncryptEvent(&buf, wAuth, &hc.cryptoCtx)
		hc.chunkChan.prepend(buf.Bytes())
		period := hc.getHTTPReconnectPeriod()
		ticker = time.NewTicker(time.Duration(period) * time.Second)
		go closePush()
		log.Printf("[%s:%d] chunk push channel start.", hc.addr, hc.idx)
		response, err := hc.paasClient.Do(req)
		if nil != err || response.StatusCode != 200 {
			log.Printf("Failed to write data to PAAS:%s for reason:%v or res:%v", u.String(), err, response)
		} else {
			log.Printf("[%s:%d] chunk push channel stop.", hc.addr, hc.idx)
		}
		ticker.Stop()
		hc.pushing = false
	}

}
Example #2
0
func (hc *httpChannel) pull() error {
	if nil == hc.pullurl {
		u, err := url.Parse(hc.addr)
		if nil != err {
			return err
		}
		u.Path = "/http/pull"
		hc.pullurl = u
	}
	if hc.pulling {
		return nil
	}
	readAuth := proxy.NewAuthEvent(hc.pullurl.Scheme == "https")
	readAuth.Index = int64(hc.idx)
	readAuth.IV = hc.cryptoCtx.EncryptIV
	var buf bytes.Buffer
	event.EncryptEvent(&buf, readAuth, &hc.cryptoCtx)
	hc.pulling = true
	log.Printf("[%s:%d] pull channel start.", hc.addr, hc.idx)
	_, err := hc.postURL(buf.Bytes(), hc.pullurl)
	hc.pulling = false
	return err
}