Example #1
0
func StartChannelSenderStage1(channel_name string, stage1_channel chan *lib.PostMessage, stage2_channel_list []chan *lib.PostMessage) chan bool {
	close_chan := make(chan bool)
	go func() {
		defer func() {
			if err := recover(); err != nil {
				utils.Log.Println(err)
				debug.PrintStack()
			}
		}()

		var idx int
		var post_message *lib.PostMessage

		for {
			select {
			case <-close_chan:
				utils.Log.Printf("Channel [%s] SenderStage1 has quit...\n", channel_name)
				close_chan <- true
				return

			case post_message = <-stage1_channel:
				if post_message.ToUser == "" {
					// channel内广播消息
					for idx = range stage2_channel_list {
						select {
						case stage2_channel_list[idx] <- post_message:
						case _ = <-wheel_milliseconds.After(10 * time.Millisecond):
							utils.Log.Printf("ChannelSenderStage1: Stage2 channel is full, channel: %s!!!\n", channel_name)
						}
					}
				} else {
					// 发送给channel的指定用户
					user_id_hash := utils.GenKey(post_message.ToUser)
					hash_key := user_id_hash % uint32(CHANNEL_LOCKS)
					select {
					case stage2_channel_list[hash_key] <- post_message:
					case _ = <-wheel_milliseconds.After(10 * time.Millisecond):
						utils.Log.Printf("ChannelSenderStage1: Stage2 channel is full, channel: %s!!!\n", channel_name)
					}
				}
				if ServerDebug {
					utils.Log.Println("ChannelSenderStage1: send post_message to stage2_channel", post_message)
				}
			}
		}
	}()

	return close_chan
}
Example #2
0
func (this *Channel) getLock(user_id string) (*sync.RWMutex, uint32) {
	user_id_hash := utils.GenKey(user_id)
	user_lock_id := user_id_hash % uint32(CHANNEL_LOCKS)

	return this.UsersLock[user_lock_id], user_lock_id
}