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


Golang AllocDirFS.ChangeEvents方法代码示例

本文整理汇总了Golang中github.com/hashicorp/nomad/client/allocdir.AllocDirFS.ChangeEvents方法的典型用法代码示例。如果您正苦于以下问题:Golang AllocDirFS.ChangeEvents方法的具体用法?Golang AllocDirFS.ChangeEvents怎么用?Golang AllocDirFS.ChangeEvents使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/hashicorp/nomad/client/allocdir.AllocDirFS的用法示例。


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

示例1: stream

// stream is the internal method to stream the content of a file. eofCancelCh is
// used to cancel the stream if triggered while at EOF. If the connection is
// broken an EPIPE error is returned
func (s *HTTPServer) stream(offset int64, path string,
	fs allocdir.AllocDirFS, framer *StreamFramer,
	eofCancelCh chan error) error {

	// Get the reader
	f, err := fs.ReadAt(path, offset)
	if err != nil {
		return err
	}
	defer f.Close()

	// Create a tomb to cancel watch events
	t := tomb.Tomb{}
	defer func() {
		t.Kill(nil)
		t.Done()
	}()

	// Create a variable to allow setting the last event
	var lastEvent string

	// Only create the file change watcher once. But we need to do it after we
	// read and reach EOF.
	var changes *watch.FileChanges

	// Start streaming the data
	data := make([]byte, streamFrameSize)
OUTER:
	for {
		// Read up to the max frame size
		n, readErr := f.Read(data)

		// Update the offset
		offset += int64(n)

		// Return non-EOF errors
		if readErr != nil && readErr != io.EOF {
			return readErr
		}

		// Send the frame
		if n != 0 {
			if err := framer.Send(path, lastEvent, data[:n], offset); err != nil {

				// Check if the connection has been closed
				if err == io.ErrClosedPipe {
					// The pipe check is for tests
					return syscall.EPIPE
				}

				operr, ok := err.(*net.OpError)
				if ok {
					// The connection was closed by our peer
					e := operr.Err.Error()
					if strings.Contains(e, syscall.EPIPE.Error()) || strings.Contains(e, syscall.ECONNRESET.Error()) {
						return syscall.EPIPE
					}
				}

				return err
			}
		}

		// Clear the last event
		if lastEvent != "" {
			lastEvent = ""
		}

		// Just keep reading
		if readErr == nil {
			continue
		}

		// If EOF is hit, wait for a change to the file
		if changes == nil {
			changes, err = fs.ChangeEvents(path, offset, &t)
			if err != nil {
				return err
			}
		}

		for {
			select {
			case <-changes.Modified:
				continue OUTER
			case <-changes.Deleted:
				return framer.Send(path, deleteEvent, nil, offset)
			case <-changes.Truncated:
				// Close the current reader
				if err := f.Close(); err != nil {
					return err
				}

				// Get a new reader at offset zero
				offset = 0
				var err error
				f, err = fs.ReadAt(path, offset)
//.........这里部分代码省略.........
开发者ID:nak3,项目名称:nomad,代码行数:101,代码来源:fs_endpoint.go


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