示例#1
0
// closeOrErr calls cl.Close() and sets err to the returned error value if
// itself is not yet set.
func closeOrErr(cl io.Closer, err *error) {
	e := cl.Close()
	if *err != nil {
		return
	}
	*err = e
}
示例#2
0
文件: log.go 项目: nilium/dagr
func logclose(c io.Closer) error {
	err := c.Close()
	if err != nil {
		logf("Error closing %T: %v", c, err)
	}
	return err
}
示例#3
0
func TestLockInChild(t *testing.T) {
	f := os.Getenv("TEST_LOCK_FILE")
	if f == "" {
		// not child
		return
	}
	lock := Lock
	if v, _ := strconv.ParseBool(os.Getenv("TEST_LOCK_PORTABLE")); v {
		lock = lockPortable
	}

	var lk io.Closer
	for scan := bufio.NewScanner(os.Stdin); scan.Scan(); {
		var err error
		switch scan.Text() {
		case "lock":
			lk, err = lock(f)
		case "unlock":
			err = lk.Close()
			lk = nil
		case "exit":
			// Simulate a crash, or at least not unlocking the lock.
			os.Exit(0)
		default:
			err = fmt.Errorf("unexpected child command %q", scan.Text())
		}
		if err != nil {
			fmt.Println(err)
		} else {
			fmt.Println("")
		}
	}
}
示例#4
0
文件: pool.go 项目: lijie/siriusdb
func (p *Pool) Put(c io.Closer) {
	select {
	case p.connch <- c:
	default:
		c.Close()
	}
}
示例#5
0
文件: main.go 项目: kotacc/ns_server
func stdinWatcher(cmd *exec.Cmd, childStdin io.Closer, graceful bool) {
	b := make([]byte, 1)

	for {
		_, err := os.Stdin.Read(b)
		if err != nil {
			log.Printf("got %s when reading stdin; terminating.", err.Error())
			break
		}

		if b[0] == '\n' || b[0] == '\r' {
			log.Printf("got new line on a stdin; terminating.")
			break
		}
	}

	if graceful {
		err := childStdin.Close()
		if err != nil {
			log.Fatalf("failed to close child's stdin: %s", err.Error())
		}
	} else {
		err := cmd.Process.Kill()
		if err != nil {
			log.Fatalf("failed to kill the child: %s", err.Error())
		}

		os.Exit(0)
	}
}
示例#6
0
func (c *contextGroup) AddChild(child io.Closer) {
	c.children.Add(1)
	go func(parent ContextGroup, child io.Closer) {
		<-parent.Closing()       // wait until parent is closing
		child.Close()            // signal child to close
		parent.Children().Done() // child signals it is done
	}(c, child)
}
示例#7
0
文件: main.go 项目: elblivion/s3gof3r
func checkClose(c io.Closer, err error) {
	if c != nil {
		cerr := c.Close()
		if err == nil {
			err = cerr
		}
	}
}
示例#8
0
func safeClose(obj io.Closer) (err error) {
	defer func() {
		if p := recover(); p != nil {
			err = errors.New(fmt.Sprintf("%v", p))
		}
	}()
	return obj.Close()
}
示例#9
0
文件: base.go 项目: influx6/proxies
//DoBroker provides the internal copy operation
func DoBroker(dest io.WriteCloser, src io.ReadCloser, end io.Closer, err NotifierError) {
	_, ex := io.Copy(dest, src)

	if ex != nil {
		go func() { err <- ex }()
	}

	end.Close()
}
示例#10
0
文件: main.go 项目: obivan/ord
func closeAndExit(db io.Closer, code int, message ...interface{}) {
	for _, m := range message {
		fmt.Fprintln(os.Stderr, m)
	}
	if err := db.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "db.Close() error: %s\n", err)
	}
	os.Exit(code)
}
示例#11
0
文件: cli.go 项目: hallyn/docker
func (cli *DockerCli) restoreTerminal(in io.Closer) error {
	if cli.state != nil {
		term.RestoreTerminal(cli.inFd, cli.state)
	}
	if in != nil {
		return in.Close()
	}
	return nil
}
示例#12
0
文件: issue4388.go 项目: danny8002/go
func f1() {
	// The 5 here and below depends on the number of internal runtime frames
	// that sit between a deferred function called during panic and
	// the original frame. If that changes, this test will start failing and
	// the number here will need to be updated.
	defer checkLine(5)
	var t *T
	var c io.Closer = t
	c.Close()
}
示例#13
0
// setClosed sets the close flag and returns the previous status.
func (s *Sniffer) setClosed(conn io.Closer) bool {
	s.lock.Lock()
	defer s.lock.Unlock()
	old := s.closed
	s.closed = true
	if !old && conn != nil {
		conn.Close()
	}
	return old
}
示例#14
0
func loggedClose(c io.Closer, name string) {
	err := c.Close()
	if err != nil {
		if name != "" {
			glog.Warningf("error closing %s: %v", name, err)
		} else {
			glog.Warningf("error closing %v: %v", c, err)
		}
	}
}
示例#15
0
func (p ping) wait(duration time.Duration, done io.Closer) {
	defer done.Close()
	for {
		select {
		case <-p:
		case <-time.After(duration):
			return
		}
	}
}
示例#16
0
func ensureEOFOnClose(t *testing.T, r io.Reader, w io.Closer) {
	b := make([]byte, 10)
	w.Close()
	n, err := r.Read(b)
	if n > 0 {
		t.Errorf("unexpected byte count %d", n)
	}
	if err != io.EOF {
		t.Errorf("expected EOF: %v", err)
	}
}
示例#17
0
func (this *ConnPool) Release(conn io.Closer) {
	if this.overMaxIdle() {
		this.decreActive()
		if conn != nil {
			conn.Close()
		}
	} else {
		this.Lock()
		defer this.Unlock()
		this.free = append(this.free, conn)
	}
}
示例#18
0
文件: doofus.go 项目: nf/doofus
func closeOnTerm(c io.Closer) {
	ch := make(chan os.Signal, 1)
	signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
	go func() {
		<-ch
		log.Printf("shutting down...")
		if err := c.Close(); err != nil {
			log.Fatalf("clean up error: %v", err)
		}
		os.Exit(1)
	}()
}
示例#19
0
文件: hijack.go 项目: alexmavr/docker
func restoreTerminal(streams streams, in io.Closer) error {
	streams.In().RestoreTerminal()
	streams.Out().RestoreTerminal()
	// WARNING: DO NOT REMOVE THE OS CHECK !!!
	// For some reason this Close call blocks on darwin..
	// As the client exists right after, simply discard the close
	// until we find a better solution.
	if in != nil && runtime.GOOS != "darwin" {
		return in.Close()
	}
	return nil
}
示例#20
0
func (p *ConnPool) CloseClean(conn io.Closer) error {
	if conn != nil {
		conn.Close()
	}
	p.Lock()
	if p.conns > 0 {
		p.conns -= 1
	}
	logger.Trace("%s close_clean conn: %v, pool: %+v", LOG_TAG, conn, p)
	p.Unlock()

	return nil
}
示例#21
0
func close_both(closer1, closer2 io.Closer) {
	var wg sync.WaitGroup
	wg.Add(2)
	go func() {
		defer wg.Done()
		closer1.Close()
	}()
	go func() {
		defer wg.Done()
		closer2.Close()
	}()
	wg.Wait()
}
示例#22
0
文件: cli.go 项目: RAMESHBABUK/docker
func (cli *DockerCli) restoreTerminal(in io.Closer) error {
	if cli.state != nil {
		term.RestoreTerminal(cli.inFd, cli.state)
	}
	// WARNING: DO NOT REMOVE THE OS CHECK !!!
	// For some reason this Close call blocks on darwin..
	// As the client exists right after, simply discard the close
	// until we find a better solution.
	if in != nil && runtime.GOOS != "darwin" {
		return in.Close()
	}
	return nil
}
示例#23
0
func testAutogeneratedFunction() {
	defer func() {
		if err := recover(); err == nil {
			panic("did not panic")
		}
		CallerInfo()
	}()
	t := struct {
		io.Closer
	}{}
	var c io.Closer
	c = t
	c.Close()
}
示例#24
0
func timeoutRead(body io.Closer, read func()) (ok bool) {
	done := make(chan struct{})
	go func() {
		read()
		done <- struct{}{}
	}()
	select {
	case <-done:
		return true
	case <-time.After(timeout):
		body.Close()
		return false
	}
}
示例#25
0
func TrapCloser(cl io.Closer, cleanups ...Cleanup) {
	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGHUP)

	go func() {
		for {
			rawSig := <-c
			sig, ok := rawSig.(syscall.Signal)

			if !ok {
				log.Fatal("not a unix signal")
			}

			switch {
			case sig == syscall.SIGHUP:
				log.Print("SIGHUP: restart process")
				err := RestartProcess()

				if err != nil {
					log.Fatal("failed to restart: " + err.Error())
				}
			case sig == syscall.SIGINT || sig == syscall.SIGKILL || sig == syscall.SIGTERM:
				log.Print("shutting down")
				donec := make(chan bool)
				go func() {
					for _, cb := range cleanups {
						if err := cb(); err != nil {
							log.Error(err)
						}
					}

					if err := cl.Close(); err != nil {
						log.Fatalf("Error shutting down: %v", err)
					}

					donec <- true
				}()
				select {
				case <-donec:
					log.Printf("shutdown")
					os.Exit(0)
				case <-time.After(5 * time.Second):
					log.Fatal("Timeout shutting down. Exiting uncleanly.")
				}
			default:
				log.Fatal("Received another signal, should not happen.")
			}
		}
	}()
}
示例#26
0
func fxImageProviderIoReader(arg interface{}) (img image.Image, err error) {
	var (
		ok     bool
		closer io.Closer
		reader io.Reader
	)
	if closer, ok = arg.(io.Closer); ok {
		defer closer.Close()
	}
	if reader, ok = arg.(io.Reader); ok {
		img, _, err = image.Decode(reader)

	}
	return
}
示例#27
0
文件: server.go 项目: sisatech/raft
func baseServerStop(baseServer *baseServer, closer io.Closer) {

	baseServer.statusLock.Lock()
	baseServer.serverStatus = stopped
	baseServer.statusLock.Unlock()

	// send quit to close channel
	quit := make(chan struct{})
	baseServer.stopCh <- quit
	closer.Close()

	// wait
	<-quit

}
示例#28
0
func (t T) close(c io.Closer) {
	if c != nil {
		if db, _ := c.(*Conn); db != nil {
			if path := db.Path("main"); path != "" {
				defer os.Remove(path)
			}
		}
		if err := c.Close(); err != nil {
			if !t.Failed() {
				t.Fatalf(cl("(%T).Close() unexpected error: %v"), c, err)
			}
			t.FailNow()
		}
	}
}
示例#29
0
func (h *FileWritingHandler) handleConn(index int, suffix string, r io.Reader, c io.Closer) {
	defer c.Close()

	out, err := h.FileCreater.Create(index, suffix)
	if err != nil {
		log.Printf("Error creating file: %v", err)
		return
	}
	defer out.Close()

	_, err = io.Copy(out, r)
	if err != nil {
		log.Printf("Error copying to file: %v", err)
	}
}
示例#30
0
func (p *ConnPool) Release(conn io.Closer) error {
	p.Lock()

	if len(p.free) >= p.MaxIdle {
		logger.Trace("%s auto close conn: %v, pool: %+v", LOG_TAG, conn, p)
		if conn != nil {
			conn.Close()
		}
		p.conns -= 1
	} else {
		p.free = append(p.free, conn)
	}
	logger.Trace("%s release conn: %v, pool: %+v", LOG_TAG, conn, p)

	p.Unlock()
	return nil
}