Skip to content
Snippets Groups Projects
Select Git revision
  • 5445d0bc5cda8a1740fe8cf08b2334d3acdf7bc3
  • release default protected
  • 11-22-implement-kv-interface-defined-in-collectiveversionedkvgo
  • hotfix/TestHostPool_UpdateNdf_AddFilter
  • XX-4719/announcementChannels
  • xx-4717/logLevel
  • jonah/noob-channel
  • master protected
  • XX-4707/tagDiskJson
  • xx-4698/notification-retry
  • hotfix/notifylockup
  • syncNodes
  • hotfix/localCB
  • XX-4677/NewChanManagerMobile
  • XX-4689/DmSync
  • duplicatePrefix
  • XX-4601/HavenInvites
  • finalizedUICallbacks
  • XX-4673/AdminKeySync
  • debugNotifID
  • anne/test
  • v4.7.5
  • v4.7.4
  • v4.7.3
  • v4.7.2
  • v4.7.1
  • v4.6.3
  • v4.6.1
  • v4.5.0
  • v4.4.4
  • v4.3.11
  • v4.3.8
  • v4.3.7
  • v4.3.6
  • v4.3.5
  • v4.2.0
  • v4.3.0
  • v4.3.4
  • v4.3.3
  • v4.3.2
  • v4.3.1
41 results

critical.go

Blame
  • single.go 1.32 KiB
    package stoppable
    
    import (
    	"github.com/pkg/errors"
    	jww "github.com/spf13/jwalterweatherman"
    	"sync"
    	"sync/atomic"
    	"time"
    )
    
    // Single allows stopping a single goroutine using a channel.
    // It adheres to the stoppable interface.
    type Single struct {
    	name    string
    	quit    chan struct{}
    	running uint32
    	once    sync.Once
    }
    
    // NewSingle returns a new single stoppable.
    func NewSingle(name string) *Single {
    	return &Single{
    		name:    name,
    		quit:    make(chan struct{}),
    		running: 1,
    	}
    }
    
    // IsRunning returns true if the thread is still running.
    func (s *Single) IsRunning() bool {
    	return atomic.LoadUint32(&s.running) == 1
    }
    
    // Quit returns the read only channel it will send the stop signal on.
    func (s *Single) Quit() chan<- struct{} {
    	return s.quit
    }
    
    // Name returns the name of the thread. This is designed to be
    func (s *Single) Name() string {
    	return s.name
    }
    
    // Close signals the thread to time out and closes if it is still running.
    func (s *Single) Close(timeout time.Duration) error {
    	var err error
    	s.once.Do(func() {
    		atomic.StoreUint32(&s.running, 0)
    		timer := time.NewTimer(timeout)
    		select {
    		case <-timer.C:
    			jww.ERROR.Printf("Stopper for %s failed to stop after "+
    				"timeout of %s", s.name, timeout)
    			err = errors.Errorf("%s failed to close", s.name)
    		case s.quit <- struct{}{}:
    		}
    	})
    	return err
    }