is_black.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. roomServeice "go-common/app/service/live/room/api/liverpc/v1"
  6. "go-common/library/conf/env"
  7. "go-common/library/log"
  8. "go-common/library/net/metadata"
  9. )
  10. func (s *Service) isBlackRoomID(roomID int64) bool {
  11. bl := s.indexBlackList.Load()
  12. backList, ok := bl.(map[int64]bool)
  13. if !ok {
  14. log.Warn("[isBlackRoomID] cache err: %+v", bl)
  15. backList = s.getBlackList()
  16. }
  17. if _, ok := backList[roomID]; !ok {
  18. return false
  19. }
  20. return true
  21. }
  22. func (s *Service) getBlackList() (res map[int64]bool) {
  23. cCtx := metadata.NewContext(context.TODO(), metadata.MD{metadata.Color: env.Color})
  24. ctx, _ := context.WithTimeout(cCtx, time.Duration(200*time.Millisecond))
  25. res = make(map[int64]bool)
  26. req := &roomServeice.RoomMngIsBlackReq{}
  27. resp, err := s.roomService.V1RoomMng.IsBlack(ctx, req)
  28. if err != nil {
  29. log.Error("[getBlackList] rpc V1RoomMng.IsBlack err:%v", err)
  30. return
  31. }
  32. if resp.Code != 0 {
  33. log.Error("[getBlackList] rpc V1RoomMng.IsBlack err code:%d", resp.Code)
  34. return
  35. }
  36. for roomID := range resp.Data {
  37. res[roomID] = true
  38. }
  39. return
  40. }
  41. func (s *Service) loadBlackList() {
  42. m := s.getBlackList()
  43. s.indexBlackList.Store(m)
  44. }
  45. func (s *Service) blackListProc() {
  46. time.Sleep(time.Second * 60)
  47. s.loadBlackList()
  48. }