• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang lib.NewSemaphore函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang lib.Config类代码示例发布时间:2022-05-23
下一篇:
Golang gcp.GoogleCloudPrint类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap