当前位置: 首页>>代码示例>>Golang>>正文


Golang http.Flusher类代码示例

本文整理汇总了Golang中net/http.Flusher的典型用法代码示例。如果您正苦于以下问题:Golang Flusher类的具体用法?Golang Flusher怎么用?Golang Flusher使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Flusher类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: run

func (cw *streamWriter) run() {
	var msgc chan raftpb.Message
	var heartbeatc <-chan time.Time
	var t streamType
	var enc encoder
	var flusher http.Flusher
	tickc := time.Tick(ConnReadTimeout / 3)

	for {
		select {
		case <-heartbeatc:
			start := time.Now()
			if err := enc.encode(linkHeartbeatMessage); err != nil {
				reportSentFailure(string(t), linkHeartbeatMessage)

				cw.status.deactivate(failureType{source: t.String(), action: "heartbeat"}, err.Error())
				cw.close()
				heartbeatc, msgc = nil, nil
				continue
			}
			flusher.Flush()
			reportSentDuration(string(t), linkHeartbeatMessage, time.Since(start))
		case m := <-msgc:
			start := time.Now()
			if err := enc.encode(m); err != nil {
				reportSentFailure(string(t), m)

				cw.status.deactivate(failureType{source: t.String(), action: "write"}, err.Error())
				cw.close()
				heartbeatc, msgc = nil, nil
				cw.r.ReportUnreachable(m.To)
				continue
			}
			flusher.Flush()
			reportSentDuration(string(t), m, time.Since(start))
		case conn := <-cw.connc:
			cw.close()
			t = conn.t
			switch conn.t {
			case streamTypeMsgAppV2:
				enc = newMsgAppV2Encoder(conn.Writer, cw.fs)
			case streamTypeMessage:
				enc = &messageEncoder{w: conn.Writer}
			default:
				plog.Panicf("unhandled stream type %s", conn.t)
			}
			flusher = conn.Flusher
			cw.mu.Lock()
			cw.status.activate()
			cw.closer = conn.Closer
			cw.working = true
			cw.mu.Unlock()
			heartbeatc, msgc = tickc, cw.msgc
		case <-cw.stopc:
			cw.close()
			close(cw.done)
			return
		}
	}
}
开发者ID:CNDonny,项目名称:scope,代码行数:60,代码来源:stream.go

示例2: writeNewMessage

func (*messageWriter) writeNewMessage(newMessage *messages.MessageOutput, w *http.ResponseWriter, flusher http.Flusher) {
	_, err := fmt.Fprintf(*w, "id: %d\n", newMessage.Id)
	if err != nil {
		log.Printf("Error while pushing: %v", err)
	}
	fmt.Fprintf(*w, "event: %s\n", "msg")
	fmt.Fprintf(*w, "data: %s\n\n", newMessage.Data)
	flusher.Flush()
}
开发者ID:Zefas,项目名称:GoMessages,代码行数:9,代码来源:sseMessageWriter.go

示例3: flushLoop

func flushLoop(f http.Flusher, interval time.Duration, done <-chan struct{}) {
	for {
		select {
		case <-time.After(interval):
			f.Flush()
		case <-done:
			return
		}
	}
}
开发者ID:josephwinston,项目名称:flynn,代码行数:10,代码来源:api.go

示例4: writeStringAndFlush

//write a string and flush it to the http Flushwriter
func writeStringAndFlush(w http.ResponseWriter, flusher http.Flusher, line string) error {

	_, err := w.Write([]byte(line))

	if err != nil {
		return err
	}

	flusher.Flush()

	return nil
}
开发者ID:30x,项目名称:shipyard,代码行数:13,代码来源:server.go

示例5: sendEvents

func (b *Broker) sendEvents(w http.ResponseWriter, f http.Flusher, client chan string) {
	for {
		ev, open := <-client

		if !open {
			break
		}

		fmt.Fprintf(w, "data: Event: %s\n\n", ev)
		f.Flush()
	}
}
开发者ID:alvalea,项目名称:go-rest,代码行数:12,代码来源:sse.go

示例6: consumerHandler

func consumerHandler(w http.ResponseWriter, r *http.Request, es *eventSource) {
	if r.Header.Get("Accept") != "text/event-stream" {
		http.Error(w, "eventsource: should accept text/event-stream", http.StatusNotAcceptable)
		return
	}

	var (
		fl  http.Flusher
		con *consumer
		id  int
		err error
		ok  bool
	)

	id, err = lastEventId(r)
	if err != nil {
		http.Error(w, "eventsource: bad last-event-id header", http.StatusBadRequest)
		return
	}

	fl, ok = w.(http.Flusher)
	if !ok {
		http.Error(w, "eventsouce: response does not implement http.Flusher", http.StatusInternalServerError)
		return
	}

	h := w.Header()
	h.Set("Content-Type", "text/event-stream")
	h.Set("Cache-Control", "no-cache")
	h.Set("Connection", "keep-alive")

	con = es.Subscribe(id)
	defer es.Unsubscribe(con)

	for {
		select {
		case b := <-con.ch:
			if _, err := w.Write(b); err == nil {
				fl.Flush()
			} else {
				log.Print(err)
				return
			}
		case <-con.quit:
			close(con.ch)
			close(con.quit)
			return
		}
	}
}
开发者ID:jinroh,项目名称:eventsource,代码行数:50,代码来源:consumer.go

示例7: sendEvent

func (server *Server) sendEvent(w http.ResponseWriter, f http.Flusher, event es.Event) bool {
	_, err := fmt.Fprintf(w, "event: %s\n", event.Event)
	if nil != err {
		return false
	}
	if "" != event.Data {
		_, err = fmt.Fprintf(w, "data: %s\n", event.Data)
		if nil != err {
			return false
		}
	}
	_, err = fmt.Fprint(w, "\n")
	if nil != err {
		return false
	}
	f.Flush()
	return true
}
开发者ID:nicoolas25,项目名称:gorsse,代码行数:18,代码来源:server.go

示例8: run

func (cw *streamWriter) run() {
	var msgc chan raftpb.Message
	var heartbeatc <-chan time.Time
	var t streamType
	var msgAppTerm uint64
	var enc encoder
	var flusher http.Flusher
	tickc := time.Tick(ConnReadTimeout / 3)

	for {
		select {
		case <-heartbeatc:
			start := time.Now()
			if err := enc.encode(linkHeartbeatMessage); err != nil {
				reportSentFailure(string(t), linkHeartbeatMessage)

				cw.status.deactivate(failureType{source: t.String(), action: "heartbeat"}, err.Error())
				cw.close()
				heartbeatc, msgc = nil, nil
				continue
			}
			flusher.Flush()
			reportSentDuration(string(t), linkHeartbeatMessage, time.Since(start))
		case m := <-msgc:
			if t == streamTypeMsgApp && m.Term != msgAppTerm {
				// TODO: reasonable retry logic
				if m.Term > msgAppTerm {
					cw.close()
					heartbeatc, msgc = nil, nil
					// TODO: report to raft at peer level
					cw.r.ReportUnreachable(m.To)
				}
				continue
			}
			start := time.Now()
			if err := enc.encode(m); err != nil {
				reportSentFailure(string(t), m)

				cw.status.deactivate(failureType{source: t.String(), action: "write"}, err.Error())
				cw.close()
				heartbeatc, msgc = nil, nil
				cw.r.ReportUnreachable(m.To)
				continue
			}
			flusher.Flush()
			reportSentDuration(string(t), m, time.Since(start))
		case conn := <-cw.connc:
			cw.close()
			t = conn.t
			switch conn.t {
			case streamTypeMsgApp:
				var err error
				msgAppTerm, err = strconv.ParseUint(conn.termStr, 10, 64)
				if err != nil {
					plog.Panicf("could not parse term %s to uint (%v)", conn.termStr, err)
				}
				enc = &msgAppEncoder{w: conn.Writer, fs: cw.fs}
			case streamTypeMsgAppV2:
				enc = newMsgAppV2Encoder(conn.Writer, cw.fs)
			case streamTypeMessage:
				enc = &messageEncoder{w: conn.Writer}
			default:
				plog.Panicf("unhandled stream type %s", conn.t)
			}
			flusher = conn.Flusher
			cw.mu.Lock()
			cw.status.activate()
			cw.closer = conn.Closer
			cw.working = true
			cw.mu.Unlock()
			heartbeatc, msgc = tickc, cw.msgc
		case <-cw.stopc:
			cw.close()
			close(cw.done)
			return
		}
	}
}
开发者ID:Celluliodio,项目名称:flannel,代码行数:78,代码来源:stream.go

示例9: run

func (cw *streamWriter) run() {
	var (
		msgc       chan raftpb.Message
		heartbeatc <-chan time.Time
		t          streamType
		enc        encoder
		flusher    http.Flusher
		batched    int
	)
	tickc := time.Tick(ConnReadTimeout / 3)
	unflushed := 0

	plog.Infof("started streaming with peer %s (writer)", cw.peerID)

	for {
		select {
		case <-heartbeatc:
			err := enc.encode(linkHeartbeatMessage)
			unflushed += linkHeartbeatMessage.Size()
			if err == nil {
				flusher.Flush()
				batched = 0
				sentBytes.WithLabelValues(cw.peerID.String()).Add(float64(unflushed))
				unflushed = 0
				continue
			}

			cw.status.deactivate(failureType{source: t.String(), action: "heartbeat"}, err.Error())

			cw.close()
			plog.Warningf("lost the TCP streaming connection with peer %s (%s writer)", cw.peerID, t)
			heartbeatc, msgc = nil, nil

		case m := <-msgc:
			err := enc.encode(m)
			if err == nil {
				unflushed += m.Size()

				if len(msgc) == 0 || batched > streamBufSize/2 {
					flusher.Flush()
					sentBytes.WithLabelValues(cw.peerID.String()).Add(float64(unflushed))
					unflushed = 0
					batched = 0
				} else {
					batched++
				}

				continue
			}

			cw.status.deactivate(failureType{source: t.String(), action: "write"}, err.Error())
			cw.close()
			plog.Warningf("lost the TCP streaming connection with peer %s (%s writer)", cw.peerID, t)
			heartbeatc, msgc = nil, nil
			cw.r.ReportUnreachable(m.To)

		case conn := <-cw.connc:
			if cw.close() {
				plog.Warningf("closed an existing TCP streaming connection with peer %s (%s writer)", cw.peerID, t)
			}
			t = conn.t
			switch conn.t {
			case streamTypeMsgAppV2:
				enc = newMsgAppV2Encoder(conn.Writer, cw.fs)
			case streamTypeMessage:
				enc = &messageEncoder{w: conn.Writer}
			default:
				plog.Panicf("unhandled stream type %s", conn.t)
			}
			flusher = conn.Flusher
			unflushed = 0
			cw.mu.Lock()
			cw.status.activate()
			cw.closer = conn.Closer
			cw.working = true
			cw.mu.Unlock()
			plog.Infof("established a TCP streaming connection with peer %s (%s writer)", cw.peerID, t)
			heartbeatc, msgc = tickc, cw.msgc
		case <-cw.stopc:
			if cw.close() {
				plog.Infof("closed the TCP streaming connection with peer %s (%s writer)", cw.peerID, t)
			}
			close(cw.done)
			plog.Infof("stopped streaming with peer %s (writer)", cw.peerID)
			return
		}
	}
}
开发者ID:kubernetes,项目名称:kubernetes,代码行数:88,代码来源:stream.go

示例10: writeTimeout

func (*messageWriter) writeTimeout(w http.ResponseWriter, flusher http.Flusher) {
	fmt.Fprintf(w, "event: %s\n", "timeout")
	fmt.Fprintf(w, "data: %s\n\n", "30s")
	flusher.Flush()
}
开发者ID:Zefas,项目名称:GoMessages,代码行数:5,代码来源:sseMessageWriter.go

示例11: Sync

func Sync(mfuser, mfpass, ssuser, sspass string, o io.Writer) bool {

	useflush := false
	var flusher http.Flusher

	if strings.Contains(reflect.TypeOf(o).String(), "http") {
		flusher = o.(http.Flusher)
		useflush = true
	}

	// sendspace api key	4R6XNCX3EA
	// mediafire api key	5v1vtcl1k8285gtbpc891jtijjxnit5v1cgopla8
	// mediafire app id		47574

	mfapi := "5v1vtcl1k8285gtbpc891jtijjxnit5v1cgopla8"
	mfapp := "47574"
	ssapi := "4R6XNCX3EA"

	// create channels for goroutines
	c := make(chan string)
	f := make(chan string)
	numfilestosync := 0

	// sign in to Mediafire and get a file path list
	mfsessionkey, err := MfGetSessionToken(mfapp, mfapi, mfuser, mfpass)
	if err != nil {
		fmt.Fprintln(o, err)
		return false
	}
	go func() {
		MfFindFolders(mfsessionkey, "myfiles", "/", 0)
		c <- "Mediafire file list ok"
	}()

	// sign in to Sendspace and get a file path list
	sstoken, err := SsGetToken(ssapi)
	if err != nil {
		fmt.Fprintln(o, err)
		return false
	}
	sssessionkey, err := SsLogin(sstoken, ssuser, sspass)
	if err != nil {
		fmt.Fprintln(o, err)
		return false
	}
	go func() {
		SsFindFolders(sssessionkey, "0", "/")
		c <- "Sendspace file list ok"
	}()

	// wait for both file path lists before going further
	for i := 0; i < 2; i++ {
		msg := <-c
		fmt.Fprintln(o, msg)
		if useflush {
			flusher.Flush()
		}
	}

	// copy Sendspace data to the Mediafire structs, if exists. don't care about the rest
	for _, s := range SsPaths {
		for i, m := range MfPaths {
			if s.Path == m.Path {
				MfPaths[i].SsD = s.Descriptor
				MfPaths[i].SsP = s.Parent
			}
		}
	}

	// create folder tree on Sendspace as it is on Mediafire
	for i, m := range MfPaths {
		// (m.SsD == "") <==> folder does not exist on Sendspace
		if m.SsD == "" && isFolderPath(m.Path) {
			SsMkFolder(m.Filename, getSsD(MfPaths, m.Parent), sssessionkey, i, m.Path)
			fmt.Fprintln(o, m.Path+" -- CREATED")
		} else if isFolderPath(m.Path) {
			fmt.Fprintln(o, m.Path+" -- exists")
		}
		if useflush {
			flusher.Flush()
		}
	}

	// create files
	for _, m := range MfPaths {

		if !isFolderPath(m.Path) {

			numfilestosync += 1
			go func(m MfPath) {

				// (m.SsD == "") <==> file does not exist on Sendspace
				if m.SsD == "" {
					fmt.Fprintln(o, m.Path+" -- SYNCING")
					pr, pw := io.Pipe()

					go func() {
						// get a download link & download
						dlink := MfGetDownloadlink(mfsessionkey, m.Descriptor)
						dload, err := http.Get(dlink)
//.........这里部分代码省略.........
开发者ID:ttenkanen,项目名称:mfsslib,代码行数:101,代码来源:sync.go

示例12: run

func (cw *streamWriter) run() {
	var msgc chan raftpb.Message
	var heartbeatc <-chan time.Time
	var t streamType
	var msgAppTerm uint64
	var enc encoder
	var flusher http.Flusher
	tickc := time.Tick(ConnReadTimeout / 3)

	for {
		select {
		case <-heartbeatc:
			start := time.Now()
			if err := enc.encode(linkHeartbeatMessage); err != nil {
				reportSentFailure(string(t), linkHeartbeatMessage)

				log.Printf("rafthttp: failed to heartbeat on stream %s due to %v. waiting for a new stream to be established.", t, err)
				cw.resetCloser()
				heartbeatc, msgc = nil, nil
				continue
			}
			flusher.Flush()
			reportSentDuration(string(t), linkHeartbeatMessage, time.Since(start))
		case m := <-msgc:
			if t == streamTypeMsgApp && m.Term != msgAppTerm {
				// TODO: reasonable retry logic
				if m.Term > msgAppTerm {
					cw.resetCloser()
					heartbeatc, msgc = nil, nil
					// TODO: report to raft at peer level
					cw.r.ReportUnreachable(m.To)
				}
				continue
			}
			start := time.Now()
			if err := enc.encode(m); err != nil {
				reportSentFailure(string(t), m)

				log.Printf("rafthttp: failed to send message on stream %s due to %v. waiting for a new stream to be established.", t, err)
				cw.resetCloser()
				heartbeatc, msgc = nil, nil
				cw.r.ReportUnreachable(m.To)
				continue
			}
			flusher.Flush()
			reportSentDuration(string(t), m, time.Since(start))
		case conn := <-cw.connc:
			cw.resetCloser()
			t = conn.t
			switch conn.t {
			case streamTypeMsgApp:
				var err error
				msgAppTerm, err = strconv.ParseUint(conn.termStr, 10, 64)
				if err != nil {
					log.Panicf("rafthttp: unexpected parse term %s error: %v", conn.termStr, err)
				}
				enc = &msgAppEncoder{w: conn.Writer, fs: cw.fs}
			case streamTypeMessage:
				enc = &messageEncoder{w: conn.Writer}
			default:
				log.Panicf("rafthttp: unhandled stream type %s", conn.t)
			}
			flusher = conn.Flusher
			cw.mu.Lock()
			cw.closer = conn.Closer
			cw.working = true
			cw.mu.Unlock()
			heartbeatc, msgc = tickc, cw.msgc
		case <-cw.stopc:
			cw.resetCloser()
			close(cw.done)
			return
		}
	}
}
开发者ID:rfrangio,项目名称:etcd-starter,代码行数:75,代码来源:stream.go


注:本文中的net/http.Flusher类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。