當前位置: 首頁>>代碼示例>>Golang>>正文


Golang lib.NewSemaphore函數代碼示例

本文整理匯總了Golang中github.com/google/cups-connector/lib.NewSemaphore函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewSemaphore函數的具體用法?Golang NewSemaphore怎麽用?Golang NewSemaphore使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewSemaphore函數的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: NewPrinterManager

func NewPrinterManager(cups *cups.CUPS, gcp *gcp.GoogleCloudPrint, xmpp *xmpp.XMPP, snmp *snmp.SNMPManager, printerPollInterval string, gcpMaxConcurrentDownload, cupsQueueSize uint, jobFullUsername, ignoreRawPrinters bool, shareScope string) (*PrinterManager, error) {
	// Get the GCP printer list.
	gcpPrinters, queuedJobsCount, err := allGCPPrinters(gcp)
	if err != nil {
		return nil, err
	}
	// Organize the GCP printers into a map.
	for i := range gcpPrinters {
		gcpPrinters[i].CUPSJobSemaphore = lib.NewSemaphore(cupsQueueSize)
	}
	gcpPrintersByGCPID := lib.NewConcurrentPrinterMap(gcpPrinters)

	// Construct.
	pm := PrinterManager{
		cups: cups,
		gcp:  gcp,
		xmpp: xmpp,
		snmp: snmp,

		gcpPrintersByGCPID: gcpPrintersByGCPID,
		downloadSemaphore:  lib.NewSemaphore(gcpMaxConcurrentDownload),

		jobStatsMutex: sync.Mutex{},
		jobsDone:      0,
		jobsError:     0,

		jobsInFlightMutex: sync.Mutex{},
		jobsInFlight:      make(map[string]struct{}),

		cupsQueueSize:     cupsQueueSize,
		jobFullUsername:   jobFullUsername,
		ignoreRawPrinters: ignoreRawPrinters,
		shareScope:        shareScope,

		quit: make(chan struct{}),
	}

	// Sync once before returning, to make sure things are working.
	if err = pm.syncPrinters(); err != nil {
		return nil, err
	}

	ppi, err := time.ParseDuration(printerPollInterval)
	if err != nil {
		return nil, err
	}
	pm.syncPrintersPeriodically(ppi)
	pm.listenXMPPNotifications()

	for gcpID := range queuedJobsCount {
		go pm.handlePrinterNewJobs(gcpID)
	}

	return &pm, nil
}
開發者ID:nooyoo11,項目名稱:cups-connector,代碼行數:55,代碼來源:printermanager.go

示例2: NewGoogleCloudPrint

// NewGoogleCloudPrint establishes a connection with GCP, returns a new GoogleCloudPrint object.
func NewGoogleCloudPrint(baseURL, robotRefreshToken, userRefreshToken, proxyName, oauthClientID, oauthClientSecret, oauthAuthURL, oauthTokenURL string, maxConcurrentDownload uint, jobs chan<- *lib.Job) (*GoogleCloudPrint, error) {
	robotClient, err := newClient(oauthClientID, oauthClientSecret, oauthAuthURL, oauthTokenURL, robotRefreshToken, ScopeCloudPrint, ScopeGoogleTalk)
	if err != nil {
		return nil, err
	}

	var userClient *http.Client
	if userRefreshToken != "" {
		userClient, err = newClient(oauthClientID, oauthClientSecret, oauthAuthURL, oauthTokenURL, userRefreshToken, ScopeCloudPrint)
		if err != nil {
			return nil, err
		}
	}

	gcp := &GoogleCloudPrint{
		baseURL:           baseURL,
		robotClient:       robotClient,
		userClient:        userClient,
		proxyName:         proxyName,
		jobs:              jobs,
		downloadSemaphore: lib.NewSemaphore(maxConcurrentDownload),
	}

	return gcp, nil
}
開發者ID:Kasparas,項目名稱:cups-connector,代碼行數:26,代碼來源:gcp.go

示例3: NewSNMPManager

// NewSNMPManager creates a new SNMP manager.
func NewSNMPManager(community string, maxConnections uint) (*SNMPManager, error) {
	C.initialize()
	s := SNMPManager{
		inUse:          lib.NewSemaphore(1),
		community:      C.CString(community),
		maxConnections: maxConnections,
	}
	return &s, nil
}
開發者ID:nooyoo11,項目名稱:cups-connector,代碼行數:10,代碼來源:snmp.go

示例4: NewSNMPManager

// NewSNMPManager creates a new SNMP manager.
func NewSNMPManager(community string, maxConnections uint) (*SNMPManager, error) {
	if community == "" || maxConnections == 0 {
		return nil, errors.New(
			"SNMP values not set in config file; run connector-util -update-config-file")
	}

	C.initialize()
	s := SNMPManager{
		inUse:          lib.NewSemaphore(1),
		community:      C.CString(community),
		maxConnections: maxConnections,
	}
	return &s, nil
}
開發者ID:rufil75,項目名稱:cups-connector,代碼行數:15,代碼來源:snmp.go

示例5: getPrinters

// getPrinters gets all printer SNMP information for each hostname, concurrently.
func (s *SNMPManager) getPrinters(hostnames []string) (map[string]*oid.VariableSet, error) {
	if !s.inUse.TryAcquire() {
		return nil, errors.New("Tried to query printers via SNMP twice")
	}
	defer s.inUse.Release()

	wg := sync.WaitGroup{}
	wg.Add(len(hostnames))

	results := make(map[string]*oid.VariableSet, len(hostnames))
	for _, hostname := range hostnames {
		results[hostname] = &oid.VariableSet{}
	}

	semaphore := lib.NewSemaphore(s.maxConnections)

	for _, hostname := range hostnames {
		go func(hostname string) {
			r := results[hostname]
			h := C.CString(hostname)
			defer C.free(unsafe.Pointer(h))

			semaphore.Acquire()
			defer semaphore.Release()

			response := C.bulkwalk(h, s.community)
			for o := response.ov_root; o != nil; o = o.next {
				r.AddVariable(intArrayToOID((*o).name, (*o).name_length), C.GoString((*o).value))
				defer C.free(unsafe.Pointer((*o).name))
				defer C.free(unsafe.Pointer((*o).value))
				defer C.free(unsafe.Pointer(o))
			}
			if response.errors_len > 0 {
				for _, err := range charArrayToSlice(response.errors, response.errors_len) {
					// Ignore errors. Not all printers support SNMP, so this is best effort.
					C.free(unsafe.Pointer(err))
				}
				C.free(unsafe.Pointer(response.errors))
			}
			wg.Done()
		}(hostname)
	}

	wg.Wait()

	return results, nil
}
開發者ID:nooyoo11,項目名稱:cups-connector,代碼行數:48,代碼來源:snmp.go

示例6: applyDiff

func (pm *PrinterManager) applyDiff(diff *lib.PrinterDiff, ch chan<- lib.Printer) {
	switch diff.Operation {
	case lib.RegisterPrinter:
		if err := pm.gcp.Register(&diff.Printer); err != nil {
			glog.Errorf("Failed to register printer %s: %s", diff.Printer.Name, err)
			break
		}
		glog.Infof("Registered %s", diff.Printer.Name)

		if pm.gcp.CanShare() {
			if err := pm.gcp.Share(diff.Printer.GCPID, pm.shareScope); err != nil {
				glog.Errorf("Failed to share printer %s: %s", diff.Printer.Name, err)
			} else {
				glog.Infof("Shared %s", diff.Printer.Name)
			}
		}

		diff.Printer.CUPSJobSemaphore = lib.NewSemaphore(pm.cupsQueueSize)

		ch <- diff.Printer
		return

	case lib.UpdatePrinter:
		if err := pm.gcp.Update(diff); err != nil {
			glog.Errorf("Failed to update %s: %s", diff.Printer.Name, err)
		} else {
			glog.Infof("Updated %s", diff.Printer.Name)
		}

		ch <- diff.Printer
		return

	case lib.DeletePrinter:
		pm.cups.RemoveCachedPPD(diff.Printer.Name)
		if err := pm.gcp.Delete(diff.Printer.GCPID); err != nil {
			glog.Errorf("Failed to delete a printer %s: %s", diff.Printer.GCPID, err)
			break
		}
		glog.Infof("Deleted %s", diff.Printer.Name)

	case lib.NoChangeToPrinter:
		ch <- diff.Printer
		return
	}

	ch <- lib.Printer{}
}
開發者ID:nooyoo11,項目名稱:cups-connector,代碼行數:47,代碼來源:printermanager.go

示例7: newCUPSCore

func newCUPSCore(maxConnections uint, connectTimeout time.Duration) (*cupsCore, error) {
	host := C.cupsServer()
	port := C.ippPort()
	encryption := C.cupsEncryption()
	timeout := C.int(connectTimeout / time.Millisecond)

	var e string
	switch encryption {
	case C.HTTP_ENCRYPTION_ALWAYS:
		e = "encrypting ALWAYS"
	case C.HTTP_ENCRYPTION_IF_REQUESTED:
		e = "encrypting IF REQUESTED"
	case C.HTTP_ENCRYPTION_NEVER:
		e = "encrypting NEVER"
	case C.HTTP_ENCRYPTION_REQUIRED:
		e = "encryption REQUIRED"
	default:
		encryption = C.HTTP_ENCRYPTION_REQUIRED
		e = "encrypting REQUIRED"
	}

	var hostIsLocal bool
	if h := C.GoString(host); strings.HasPrefix(h, "/") || h == "localhost" {
		hostIsLocal = true
	}

	cs := lib.NewSemaphore(maxConnections)
	cp := make(chan *C.http_t)

	cc := &cupsCore{host, port, encryption, timeout, cs, cp, hostIsLocal}

	// This connection isn't used, just checks that a connection is possible
	// before returning from the constructor.
	http, err := cc.connect()
	if err != nil {
		return nil, err
	}
	cc.disconnect(http)

	log.Infof("connected to CUPS server %s:%d %s\n", C.GoString(host), int(port), e)

	return cc, nil
}
開發者ID:JohnOH,項目名稱:cups-connector,代碼行數:43,代碼來源:core.go

示例8: newClient

glibc < 2.20 and OSX 10.10 have problems when C.getaddrinfo is called many
times concurrently. When the connector shares more than about 230 printers, and
GCP is called once per printer in concurrent goroutines, http.Client.Do starts
to fail with a lookup error.

This solution, a semaphore, limits the quantity of concurrent HTTP requests,
which also limits the quantity of concurrent calls to net.LookupHost (which
calls C.getaddrinfo()).

I would rather wait for the Go compiler to solve this problem than make this a
configurable option, hence this long-winded comment.

https://github.com/golang/go/issues/3575
https://github.com/golang/go/issues/6336
*/
var lock *lib.Semaphore = lib.NewSemaphore(100)

// newClient creates an instance of http.Client, wrapped with OAuth
// credentials.
func newClient(oauthClientID, oauthClientSecret, oauthAuthURL, oauthTokenURL, refreshToken string, scopes ...string) (*http.Client, error) {
	config := &oauth2.Config{
		ClientID:     oauthClientID,
		ClientSecret: oauthClientSecret,
		Endpoint: oauth2.Endpoint{
			AuthURL:  oauthAuthURL,
			TokenURL: oauthTokenURL,
		},
		RedirectURL: RedirectURL,
		Scopes:      scopes,
	}
開發者ID:rufil75,項目名稱:cups-connector,代碼行數:30,代碼來源:http.go

示例9: NewPrinterManager

func NewPrinterManager(cups *cups.CUPS, gcp *gcp.GoogleCloudPrint, privet *privet.Privet, snmp *snmp.SNMPManager, printerPollInterval string, cupsQueueSize uint, jobFullUsername, ignoreRawPrinters bool, shareScope string, jobs <-chan *lib.Job, xmppNotifications <-chan xmpp.PrinterNotification) (*PrinterManager, error) {
	var printers *lib.ConcurrentPrinterMap
	var queuedJobsCount map[string]uint

	var err error
	if gcp != nil {
		// Get all GCP printers.
		var gcpPrinters []lib.Printer
		gcpPrinters, queuedJobsCount, err = gcp.ListPrinters()
		if err != nil {
			return nil, err
		}
		// Organize the GCP printers into a map.
		for i := range gcpPrinters {
			gcpPrinters[i].CUPSJobSemaphore = lib.NewSemaphore(cupsQueueSize)
		}
		printers = lib.NewConcurrentPrinterMap(gcpPrinters)
	} else {
		printers = lib.NewConcurrentPrinterMap(nil)
	}

	// Construct.
	pm := PrinterManager{
		cups:   cups,
		gcp:    gcp,
		privet: privet,
		snmp:   snmp,

		printers: printers,

		jobStatsMutex: sync.Mutex{},
		jobsDone:      0,
		jobsError:     0,

		jobsInFlightMutex: sync.Mutex{},
		jobsInFlight:      make(map[string]struct{}),

		cupsQueueSize:     cupsQueueSize,
		jobFullUsername:   jobFullUsername,
		ignoreRawPrinters: ignoreRawPrinters,
		shareScope:        shareScope,

		quit: make(chan struct{}),
	}

	// Sync once before returning, to make sure things are working.
	// Ignore privet updates this first time because Privet always starts
	// with zero printers.
	if err = pm.syncPrinters(true); err != nil {
		return nil, err
	}

	// Initialize Privet printers.
	if privet != nil {
		for _, printer := range pm.printers.GetAll() {
			err := privet.AddPrinter(printer, pm.printers.GetByCUPSName)
			if err != nil {
				glog.Warningf("Failed to register %s locally: %s", printer.Name, err)
			} else {
				glog.Infof("Registered %s locally", printer.Name)
			}
		}
	}

	ppi, err := time.ParseDuration(printerPollInterval)
	if err != nil {
		return nil, err
	}
	pm.syncPrintersPeriodically(ppi)
	pm.listenNotifications(jobs, xmppNotifications)

	if gcp != nil {
		for gcpPrinterID := range queuedJobsCount {
			p, _ := printers.GetByGCPID(gcpPrinterID)
			go gcp.HandleJobs(&p, func() { pm.incrementJobsProcessed(false) })
		}
	}

	return &pm, nil
}
開發者ID:kleopatra999,項目名稱:cups-connector,代碼行數:80,代碼來源:printermanager.go

示例10: applyDiff

func (pm *PrinterManager) applyDiff(diff *lib.PrinterDiff, ch chan<- lib.Printer, ignorePrivet bool) {
	switch diff.Operation {
	case lib.RegisterPrinter:
		if pm.gcp != nil {
			if err := pm.gcp.Register(&diff.Printer); err != nil {
				glog.Errorf("Failed to register printer %s: %s", diff.Printer.Name, err)
				break
			}
			glog.Infof("Registered %s in the cloud", diff.Printer.Name)

			if pm.gcp.CanShare() {
				if err := pm.gcp.Share(diff.Printer.GCPID, pm.shareScope); err != nil {
					glog.Errorf("Failed to share printer %s: %s", diff.Printer.Name, err)
				} else {
					glog.Infof("Shared %s", diff.Printer.Name)
				}
			}
		}

		diff.Printer.CUPSJobSemaphore = lib.NewSemaphore(pm.cupsQueueSize)

		if pm.privet != nil && !ignorePrivet {
			err := pm.privet.AddPrinter(diff.Printer, pm.printers.GetByCUPSName)
			if err != nil {
				glog.Warningf("Failed to register %s locally: %s", diff.Printer.Name, err)
			} else {
				glog.Infof("Registered %s locally", diff.Printer.Name)
			}
		}

		ch <- diff.Printer
		return

	case lib.UpdatePrinter:
		if pm.gcp != nil {
			if err := pm.gcp.Update(diff); err != nil {
				glog.Errorf("Failed to update %s: %s", diff.Printer.Name, err)
			} else {
				glog.Infof("Updated %s in the cloud", diff.Printer.Name)
			}
		}

		if pm.privet != nil && !ignorePrivet && diff.DefaultDisplayNameChanged {
			err := pm.privet.UpdatePrinter(diff)
			if err != nil {
				glog.Warningf("Failed to update %s locally: %s", diff.Printer.Name, err)
			} else {
				glog.Infof("Updated %s locally", diff.Printer.Name)
			}
		}

		ch <- diff.Printer
		return

	case lib.DeletePrinter:
		pm.cups.RemoveCachedPPD(diff.Printer.Name)

		if pm.gcp != nil {
			if err := pm.gcp.Delete(diff.Printer.GCPID); err != nil {
				glog.Errorf("Failed to delete a printer %s: %s", diff.Printer.GCPID, err)
				break
			}
			glog.Infof("Deleted %s in the cloud", diff.Printer.Name)
		}

		if pm.privet != nil && !ignorePrivet {
			err := pm.privet.DeletePrinter(diff.Printer.Name)
			if err != nil {
				glog.Warningf("Failed to delete %s locally: %s", diff.Printer.Name, err)
			} else {
				glog.Infof("Deleted %s locally", diff.Printer.Name)
			}
		}

	case lib.NoChangeToPrinter:
		ch <- diff.Printer
		return
	}

	ch <- lib.Printer{}
}
開發者ID:kleopatra999,項目名稱:cups-connector,代碼行數:81,代碼來源:printermanager.go

示例11: applyDiff

func (pm *PrinterManager) applyDiff(diff *lib.PrinterDiff, ch chan<- lib.Printer, ignorePrivet bool) {
	switch diff.Operation {
	case lib.RegisterPrinter:
		if pm.gcp != nil {
			if err := pm.gcp.Register(&diff.Printer); err != nil {
				log.ErrorPrinterf(diff.Printer.Name, "Failed to register: %s", err)
				break
			}
			log.InfoPrinterf(diff.Printer.Name+" "+diff.Printer.GCPID, "Registered in the cloud")

			if pm.gcp.CanShare() {
				if err := pm.gcp.Share(diff.Printer.GCPID, pm.shareScope, gcp.User, true); err != nil {
					log.ErrorPrinterf(diff.Printer.Name, "Failed to share: %s", err)
				} else {
					log.InfoPrinterf(diff.Printer.Name, "Shared")
				}
			}
		}

		diff.Printer.NativeJobSemaphore = lib.NewSemaphore(pm.nativeJobQueueSize)

		if pm.privet != nil && !ignorePrivet {
			err := pm.privet.AddPrinter(diff.Printer, pm.printers.GetByNativeName)
			if err != nil {
				log.WarningPrinterf(diff.Printer.Name, "Failed to register locally: %s", err)
			} else {
				log.InfoPrinterf(diff.Printer.Name, "Registered locally")
			}
		}

		ch <- diff.Printer
		return

	case lib.UpdatePrinter:
		if pm.gcp != nil {
			if err := pm.gcp.Update(diff); err != nil {
				log.ErrorPrinterf(diff.Printer.Name+" "+diff.Printer.GCPID, "Failed to update: %s", err)
			} else {
				log.InfoPrinterf(diff.Printer.Name+" "+diff.Printer.GCPID, "Updated in the cloud")
			}
		}

		if pm.privet != nil && !ignorePrivet && diff.DefaultDisplayNameChanged {
			err := pm.privet.UpdatePrinter(diff)
			if err != nil {
				log.WarningPrinterf(diff.Printer.Name, "Failed to update locally: %s", err)
			} else {
				log.InfoPrinterf(diff.Printer.Name, "Updated locally")
			}
		}

		ch <- diff.Printer
		return

	case lib.DeletePrinter:
		pm.native.RemoveCachedPPD(diff.Printer.Name)

		if pm.gcp != nil {
			if err := pm.gcp.Delete(diff.Printer.GCPID); err != nil {
				log.ErrorPrinterf(diff.Printer.Name+" "+diff.Printer.GCPID, "Failed to delete from the cloud: %s", err)
				break
			}
			log.InfoPrinterf(diff.Printer.Name+" "+diff.Printer.GCPID, "Deleted from the cloud")
		}

		if pm.privet != nil && !ignorePrivet {
			err := pm.privet.DeletePrinter(diff.Printer.Name)
			if err != nil {
				log.WarningPrinterf(diff.Printer.Name, "Failed to delete: %s", err)
			} else {
				log.InfoPrinterf(diff.Printer.Name, "Deleted locally")
			}
		}

	case lib.NoChangeToPrinter:
		ch <- diff.Printer
		return
	}

	ch <- lib.Printer{}
}
開發者ID:tryandbuy,項目名稱:cups-connector,代碼行數:81,代碼來源:printermanager.go

示例12: NewPrinterManager

func NewPrinterManager(cups *cups.CUPS, gcp *gcp.GoogleCloudPrint, xmpp *xmpp.XMPP, privet *privet.Privet, snmp *snmp.SNMPManager, printerPollInterval string, gcpMaxConcurrentDownload, cupsQueueSize uint, jobFullUsername, ignoreRawPrinters bool, shareScope string) (*PrinterManager, error) {
	// Get the GCP printer list.
	gcpPrinters, queuedJobsCount, err := allGCPPrinters(gcp)
	if err != nil {
		return nil, err
	}
	// Organize the GCP printers into a map.
	for i := range gcpPrinters {
		gcpPrinters[i].CUPSJobSemaphore = lib.NewSemaphore(cupsQueueSize)
	}
	gcpPrintersByGCPID := lib.NewConcurrentPrinterMap(gcpPrinters)

	// Construct.
	pm := PrinterManager{
		cups:   cups,
		gcp:    gcp,
		xmpp:   xmpp,
		privet: privet,
		snmp:   snmp,

		gcpPrintersByGCPID: gcpPrintersByGCPID,
		downloadSemaphore:  lib.NewSemaphore(gcpMaxConcurrentDownload),

		jobStatsMutex: sync.Mutex{},
		jobsDone:      0,
		jobsError:     0,

		gcpJobsInFlightMutex: sync.Mutex{},
		gcpJobsInFlight:      make(map[string]struct{}),

		cupsQueueSize:     cupsQueueSize,
		jobFullUsername:   jobFullUsername,
		ignoreRawPrinters: ignoreRawPrinters,
		shareScope:        shareScope,

		quit: make(chan struct{}),
	}

	// Sync once before returning, to make sure things are working.
	// Ignore privet updates this first time because Privet always starts
	// with zero printers.
	if err = pm.syncPrinters(true); err != nil {
		return nil, err
	}

	// Initialize Privet printers.
	if privet != nil {
		for _, printer := range pm.gcpPrintersByGCPID.GetAll() {
			getPrinter := func() (lib.Printer, bool) { return pm.gcpPrintersByGCPID.Get(printer.GCPID) }
			err := privet.AddPrinter(printer, getPrinter)
			if err != nil {
				glog.Warningf("Failed to register %s locally: %s", printer.Name, err)
			} else {
				glog.Infof("Registered %s locally", printer.Name)
			}
		}
	}

	ppi, err := time.ParseDuration(printerPollInterval)
	if err != nil {
		return nil, err
	}
	pm.syncPrintersPeriodically(ppi)
	if privet == nil {
		pm.listenNotifications(xmpp.Notifications(), make(chan *lib.Job))
	} else {
		pm.listenNotifications(xmpp.Notifications(), privet.Jobs())
	}

	for gcpID := range queuedJobsCount {
		go pm.handleNewGCPJobs(gcpID)
	}

	return &pm, nil
}
開發者ID:rufil75,項目名稱:cups-connector,代碼行數:75,代碼來源:printermanager.go


注:本文中的github.com/google/cups-connector/lib.NewSemaphore函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。