当前位置: 首页>>代码示例>>Golang>>正文


Golang Cluster.Peers方法代码示例

本文整理汇总了Golang中euphoria/io/heim/cluster.Cluster.Peers方法的典型用法代码示例。如果您正苦于以下问题:Golang Cluster.Peers方法的具体用法?Golang Cluster.Peers怎么用?Golang Cluster.Peers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在euphoria/io/heim/cluster.Cluster的用法示例。


在下文中一共展示了Cluster.Peers方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: NewBackend

func NewBackend(
	ctx scope.Context, dsn string, c cluster.Cluster, serverDesc *cluster.PeerDesc) (*Backend, error) {

	var version string

	if serverDesc == nil {
		version = "dev"
	} else {
		version = serverDesc.Version
	}

	parsedDSN, err := url.Parse(dsn)
	if err == nil {
		if parsedDSN.User != nil {
			parsedDSN.User = url.UserPassword(parsedDSN.User.Username(), "xxxxxx")
		}
		log.Printf("psql backend %s on %s", version, parsedDSN.String())
	} else {
		return nil, fmt.Errorf("url.Parse: %s", err)
	}

	db, err := sql.Open("postgres", dsn)
	if err != nil {
		return nil, fmt.Errorf("sql.Open: %s", err)
	}

	b := &Backend{
		DB:        db,
		dsn:       dsn,
		desc:      serverDesc,
		version:   version,
		cluster:   c,
		peers:     map[string]string{},
		listeners: map[string]ListenerMap{},
		ctx:       ctx,
	}
	b.logger = log.New(os.Stdout, fmt.Sprintf("[backend %p] ", b), log.LstdFlags)

	if serverDesc != nil {
		b.peers[serverDesc.ID] = serverDesc.Era
		for _, desc := range c.Peers() {
			b.peers[desc.ID] = desc.Era
		}
	}

	if err := b.start(); err != nil {
		return nil, err
	}

	return b, nil
}
开发者ID:ArkaneMoose,项目名称:heim,代码行数:51,代码来源:backend.go

示例2: scan

func scan(ctx scope.Context, c cluster.Cluster, pb *psql.Backend) error {
	type PresenceWithUserAgent struct {
		psql.Presence
		UserAgent string `db:"user_agent"`
	}

	rows, err := pb.DbMap.Select(
		PresenceWithUserAgent{},
		"SELECT p.room, p.session_id, p.server_id, p.server_era, p.updated, p.fact, s.user_agent"+
			" FROM presence p, session_log s WHERE p.session_id = s.session_id")
	if err != nil {
		return err
	}

	peers := map[string]string{}
	for _, desc := range c.Peers() {
		peers[desc.ID] = desc.Era
	}

	activeRows := 0
	activeRowsPerRoom := map[string]int{}
	activeSessionsPerAgent := map[string]int{}
	lurkingSessionsPerAgent := map[string]int{}
	webSessionsPerAgent := map[string]int{}

	lurkingRows := 0
	lurkingRowsPerRoom := map[string]int{}

	for _, row := range rows {
		presence, ok := row.(*PresenceWithUserAgent)
		if !ok {
			fmt.Printf("error: expected row of type *PresenceWithUserAgent, got %T\n", row)
			continue
		}

		if peers[presence.ServerID] == presence.ServerEra {
			activeRows++
			activeRowsPerRoom[presence.Room]++

			parts := strings.Split(presence.SessionID, "-")
			activeSessionsPerAgent[parts[0]]++

			// Check web-client status.
			// TODO: use positive fingerprint from web client instead of user-agent
			if presence.UserAgent != "" && !strings.HasPrefix(presence.UserAgent, "Python") {
				webSessionsPerAgent[parts[0]]++
			}

			// Check lurker status. Currently this is indicated by a blank name on the session.
			session, err := presence.SessionView(proto.General)
			if err != nil {
				fmt.Printf("error: failed to extract session from presence row: %s\n", err)
				continue
			}
			if session.Name == "" {
				lurkingRows++
				lurkingRowsPerRoom[presence.Room]++
				lurkingSessionsPerAgent[parts[0]]++
			}
		}
	}

	rowCount.Set(float64(len(rows)))
	activeRowCount.Set(float64(activeRows))
	lurkingRowCount.Set(float64(lurkingRows))
	uniqueAgentCount.Set(float64(len(activeSessionsPerAgent)))
	uniqueLurkingAgentCount.Set(float64(len(lurkingSessionsPerAgent)))
	uniqueWebAgentCount.Set(float64(len(webSessionsPerAgent)))

	for room, count := range activeRowsPerRoom {
		activeRowCountPerRoom.With(prometheus.Labels{"room": room}).Set(float64(count))
	}

	for room, count := range lurkingRowsPerRoom {
		lurkingRowCountPerRoom.With(prometheus.Labels{"room": room}).Set(float64(count))
	}

	for _, count := range activeSessionsPerAgent {
		sessionsPerAgent.Observe(float64(count))
	}

	return nil
}
开发者ID:logan,项目名称:heim,代码行数:83,代码来源:scanner.go


注:本文中的euphoria/io/heim/cluster.Cluster.Peers方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。