func SizeCheck(id string, size int64) { ckInfo, ok := items[id] if !ok { items[id] = &serviceCheckPoint{ Id: id, LastCheck: time.Now(), LastSize: size, Size: size, Status: UNBLOCK} ckInfo = items[id] } ckInfo.Size = size config := daylimit.Config() if time.Since(ckInfo.LastCheck) > time.Duration(config.LimitWindow)*time.Second { tw := time.Duration(config.LimitWindow) ckInfo.LastSize = ckInfo.Size ckInfo.LastCheck = ckInfo.LastCheck.Add(time.Since(ckInfo.LastCheck) / time.Second / tw * tw * time.Second) if ckInfo.Status == BLOCK { // Unblock connection if ok := warden.Unblock(ckInfo.Id); ok { daylimit.Logger().Infof("Unblock container [%s]", ckInfo.Id) } else { daylimit.Logger().Errorf("Unblock container failed [%s]", ckInfo.Id) } ckInfo.Status = UNBLOCK } } else if ckInfo.Size-ckInfo.LastSize > config.LimitSize && ckInfo.Status == UNBLOCK { // Block connection ckInfo.Status = BLOCK if ok := warden.Block(ckInfo.Id); ok { daylimit.Logger().Infof("Block container [%s]", ckInfo.Id) } else { daylimit.Logger().Errorf("Block container failed [%s]", ckInfo.Id) } } }
func runDaemon() { var errNum int8 ticker := time.Tick(time.Duration(daylimit.Config().FetchInterval) * time.Second) for _ = range ticker { info, err := daylimit.GetList() if err != nil { daylimit.Logger().Errorf("Get throughput size error:[%s]", err) errNum++ if errNum >= MAXERR { os.Exit(2) } continue } errNum = 0 // Check limit match for id, size := range info { SizeCheck(id, size) } } }