Exemple #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
}
Exemple #2
0
func logclose(c io.Closer) error {
	err := c.Close()
	if err != nil {
		logf("Error closing %T: %v", c, err)
	}
	return err
}
Exemple #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("")
		}
	}
}
Exemple #4
0
func (p *Pool) Put(c io.Closer) {
	select {
	case p.connch <- c:
	default:
		c.Close()
	}
}
Exemple #5
0
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)
	}
}
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)
}
Exemple #7
0
func checkClose(c io.Closer, err error) {
	if c != nil {
		cerr := c.Close()
		if err == nil {
			err = cerr
		}
	}
}
Exemple #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()
}
Exemple #9
0
//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()
}
Exemple #10
0
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)
}
Exemple #11
0
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
}
Exemple #12
0
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()
}
Exemple #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
}
Exemple #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)
		}
	}
}
Exemple #15
0
func (p ping) wait(duration time.Duration, done io.Closer) {
	defer done.Close()
	for {
		select {
		case <-p:
		case <-time.After(duration):
			return
		}
	}
}
Exemple #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)
	}
}
Exemple #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)
	}
}
Exemple #18
0
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)
	}()
}
Exemple #19
0
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
}
Exemple #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
}
Exemple #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()
}
Exemple #22
0
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
}
Exemple #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()
}
Exemple #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
	}
}
Exemple #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.")
			}
		}
	}()
}
Exemple #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
}
Exemple #27
0
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

}
Exemple #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()
		}
	}
}
Exemple #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)
	}
}
Exemple #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
}