Ejemplo n.º 1
0
func (this *Client) Close() {
	log.Debug("client channels: %s", this.Channels)
	log.Debug("pubsub channels: %s", PubsubChannels)

	UnsubscribeAllChannels(this)
	this.Client.Close()
}
Ejemplo n.º 2
0
func (this *S2sProxy) WaitMsg() {
	for {
		select {
		case tuple := <-this.PubMsgChan:
			this.Stats.pubCalls.Mark(1)
			for peerInterface := range tuple.peers.Iter() {
				log.Debug("peer was %s %s", peerInterface, reflect.TypeOf(peerInterface))
				peer, _ := peerInterface.(*Peer)
				log.Debug("peer is %s %s", peer, reflect.TypeOf(peer))
				go peer.writeMsg(fmt.Sprintf("%s %s %s %d\n", S2S_PUB_CMD, tuple.channel, tuple.msg, tuple.ts))
			}

		case channel := <-this.SubMsgChan:
			this.Stats.subCalls.Mark(1)
			this.Stats.outChannels.Mark(1)
			for _, peer := range this.Router.Peers {
				go peer.writeMsg(fmt.Sprintf("%s %s\n", S2S_SUB_CMD, channel))
			}

		case channel := <-this.UnsubMsgChan:
			this.Stats.unsubCalls.Mark(1)
			this.Stats.outChannels.Mark(-1)
			for _, peer := range this.Router.Peers {
				go peer.writeMsg(fmt.Sprintf("%s %s\n", S2S_UNSUB_CMD, channel))
			}
		}
	}
}
Ejemplo n.º 3
0
func (this *S2sClientProcessor) processCmd(cl *Cmdline, client *server.Client) error {
	switch cl.Cmd {
	case S2S_PUB_CMD:
		publish(cl.Params[0], cl.Params[1], true)

	case S2S_SUB_CMD:
		log.Debug("Remote addr %s sub: %s", client.RemoteAddr(), cl.Params[0])
		Proxy.Router.AddPeerToChannel(config.GetS2sAddr(client.RemoteAddr().String()), cl.Params[0])

	case S2S_UNSUB_CMD:
		log.Debug("Remote addr %s unsub: %s", client.RemoteAddr(), cl.Params[0])
		Proxy.Router.RemovePeerFromChannel(config.GetS2sAddr(client.RemoteAddr().String()), cl.Params[0])
	}

	return nil
}
Ejemplo n.º 4
0
func (this *S2sClientProcessor) OnAccept(client *server.Client) {
	for {
		if this.server.SessTimeout.Nanoseconds() > int64(0) {
			client.SetReadDeadline(time.Now().Add(this.server.SessTimeout))
		}
		input := make([]byte, 1460)
		n, err := client.Conn.Read(input)

		input = input[:n]

		if err != nil {
			if err == io.EOF {
				client.Close()
				return
			} else if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
				log.Info("client[%s] read timeout", client.RemoteAddr())
				client.Close()
				return
			} else if nerr, ok := err.(net.Error); !ok || !nerr.Temporary() {
				client.Close()
				return
			} else {
				log.Info("Unexpected error: %s", err.Error())
				client.Close()
				return
			}
		}

		strInput := string(input)
		log.Debug("input: %s", strInput)

		this.OnRead(client, strInput)
	}
}
Ejemplo n.º 5
0
func cleanup() {
	if options.lockFile != "" {
		locking.UnlockInstance(options.lockFile)
		log.Debug("Cleanup lock %s", options.lockFile)
	}
	UnregisterEtc()
}
Ejemplo n.º 6
0
func subscribe(cli *Client, channel string) string {
	log.Debug("%x", channel)
	_, exists := cli.Channels[channel]
	if exists {
		return fmt.Sprintf("%s %s", OUTPUT_ALREADY_SUBSCRIBED, channel)
	} else {
		cli.Channels[channel] = 1
		clients, exists := PubsubChannels.Get(channel)
		if exists {
			clients.Set(cli.RemoteAddr().String(), cli)
		} else {
			clients = cmap.New()
			clients.Set(cli.RemoteAddr().String(), cli)

			//s2s
			if config.PushdConf.IsDistMode() {
				Proxy.SubMsgChan <- channel
			}
		}
		PubsubChannels.Set(channel, clients)

		return fmt.Sprintf("%s %s", OUTPUT_SUBSCRIBED, channel)
	}

}
Ejemplo n.º 7
0
Archivo: main.go Proyecto: cmotc/pushd
func shutdown() {
	for i, conn := range conns {
		log.Debug("close connection %d", i)
		err := conn.Close()
		if err != nil {
			log.Info("close error: %s", err.Error())
		}
	}
}
Ejemplo n.º 8
0
func publish(channel, msg string, fromS2s bool) string {
	clients, exists := PubsubChannels.Get(channel)
	ts := time.Now().UnixNano()
	if exists {
		log.Debug("channel %s subscribed by %d clients", channel, clients.Count())
		for ele := range clients.Iter() {
			cli := ele.Val.(*Client)
			cli.Mutex.Lock()
			if cli.IsConnected() {
				if !fromS2s {
					go cli.WriteMsg(fmt.Sprintf("%s %d", msg, ts))
				} else {
					go cli.WriteMsg(msg)
				}
			}
			cli.Mutex.Unlock()
		}
	}

	storage.MsgCache.Store(&storage.MsgTuple{Channel: channel, Msg: msg, Ts: ts})
	if !fromS2s && config.PushdConf.EnableStorage() {
		storage.EnqueueMsg(channel, msg, ts)
	}

	if !fromS2s {
		//s2s
		if config.PushdConf.IsDistMode() {
			var peers set.Set
			peers, exists = Proxy.Router.LookupPeersByChannel(channel)
			log.Debug("now peers %s", peers)
			if exists {
				Proxy.PubMsgChan <- NewPubTuple(peers, msg, channel, ts)
			}
		}

		return OUTPUT_PUBLISHED
	} else {
		return ""
	}
}
Ejemplo n.º 9
0
func watchPeers(proxy *S2sProxy) {
	eventChan := make(chan []string)
	go func() {
		for {
			select {
			case servers := <-eventChan:
				log.Debug(servers)
				for _, server := range servers {
					proxy.Router.connectPeer(server)
				}
			}
		}
	}()
	etclib.WatchService(etclib.SERVICE_PUSHD, eventChan)
}
Ejemplo n.º 10
0
func (this *S2sClientProcessor) OnRead(client *server.Client, input string) {
	for _, inputUnit := range strings.Split(input, "\n") {
		cl := NewCmdline(inputUnit, nil)
		if cl.Cmd == "" {
			continue
		}

		err := this.processCmd(cl, client)

		if err != nil {
			log.Debug("Process peer cmd[%s %s] error: %s", cl.Cmd, cl.Params, err.Error())
			go client.WriteMsg(err.Error())
			continue
		}
	}
}
Ejemplo n.º 11
0
func (this *Parser) parse(txt, tp string) []string {
	re, exists := this.Regexps[tp]
	if !exists {
		log.Warn("regexp not found for %s", tp)
	}
	match := re.FindAllStringSubmatch(txt, -1)
	for _, r := range match {
		for _, rr := range r {
			log.Debug(rr)
		}
	}

	if len(match) < 1 || len(match[0]) < 2 {
		return []string{}
	}
	return match[0][1:]
}
Ejemplo n.º 12
0
func (this *PushdClientProcessor) OnRead(client *Client, input string) {
	var (
		t1      time.Time
		elapsed time.Duration
	)

	t1 = time.Now()

	for _, inputUnit := range strings.Split(input, "\n") {
		cl := NewCmdline(inputUnit, client)
		if cl.Cmd == "" {
			continue
		}

		if this.enableAclCheck {
			err := AclCheck(client, cl.Cmd)
			if err != nil {
				go client.WriteMsg(fmt.Sprintf("%s", err.Error()))
				continue
			}
		}

		ret, err := cl.Process()
		if err != nil {
			log.Debug("Process cmd[%s %s] error: %s", cl.Cmd, cl.Params, err.Error())
			go client.WriteMsg(fmt.Sprintf("%s\n", err.Error()))
			continue
		}

		go client.WriteMsg(ret)
	}
	elapsed = time.Since(t1)
	this.serverStats.CallLatencies.Update(elapsed.Nanoseconds() / 1e6)
	this.serverStats.CallPerSecond.Mark(1)

}