How to use mgrKey method of main Package

Best Syzkaller code snippet using main.mgrKey

entities.go

Source:entities.go Github

copy

Full Screen

...235 BisectPending236 BisectError237 BisectYes238)239func mgrKey(c context.Context, ns, name string) *db.Key {240 return db.NewKey(c, "Manager", fmt.Sprintf("%v-%v", ns, name), 0, nil)241}242func (mgr *Manager) key(c context.Context) *db.Key {243 return mgrKey(c, mgr.Namespace, mgr.Name)244}245func loadManager(c context.Context, ns, name string) (*Manager, error) {246 mgr := new(Manager)247 if err := db.Get(c, mgrKey(c, ns, name), mgr); err != nil {248 if err != db.ErrNoSuchEntity {249 return nil, fmt.Errorf("failed to get manager %v/%v: %v", ns, name, err)250 }251 mgr = &Manager{252 Namespace: ns,253 Name: name,254 }255 }256 return mgr, nil257}258// updateManager does transactional compare-and-swap on the manager and its current stats.259func updateManager(c context.Context, ns, name string, fn func(mgr *Manager, stats *ManagerStats) error) error {260 date := timeDate(timeNow(c))261 tx := func(c context.Context) error {262 mgr, err := loadManager(c, ns, name)263 if err != nil {264 return err265 }266 mgrKey := mgr.key(c)267 stats := new(ManagerStats)268 statsKey := db.NewKey(c, "ManagerStats", "", int64(date), mgrKey)269 if err := db.Get(c, statsKey, stats); err != nil {270 if err != db.ErrNoSuchEntity {271 return fmt.Errorf("failed to get stats %v/%v/%v: %v", ns, name, date, err)272 }273 stats = &ManagerStats{274 Date: date,275 }276 }277 if err := fn(mgr, stats); err != nil {278 return err279 }280 if _, err := db.Put(c, mgrKey, mgr); err != nil {281 return fmt.Errorf("failed to put manager: %v", err)282 }283 if _, err := db.Put(c, statsKey, stats); err != nil {284 return fmt.Errorf("failed to put manager stats: %v", err)285 }286 return nil287 }288 return db.RunInTransaction(c, tx, &db.TransactionOptions{Attempts: 10})289}290func loadAllManagers(c context.Context, ns string) ([]*Manager, []*db.Key, error) {291 var managers []*Manager292 query := db.NewQuery("Manager")293 if ns != "" {294 query = query.Filter("Namespace=", ns)...

Full Screen

Full Screen

main.go

Source:main.go Github

copy

Full Screen

1package main2import (3 "context"4 "fmt"5 "sync"6 "log-sample/logconfig"7 "log-sample/logtailf"8 "github.com/spf13/viper"9)10var mainOnce sync.Once11var configMgr map[string]*logconfig.ConfigData12// ConstructMgr 控制著协成资源析构,13// 并且通过ConstructMgr全局函数构造configMgr这样的map记录最新的配置信息。14func ConstructMgr(configPaths interface{}, keyChan chan string) {15 configDatas := configPaths.(map[string]interface{})16 for conkey, confval := range configDatas {17 fmt.Println("conkey:", conkey)18 fmt.Println("conval:", confval)19 configData := new(logconfig.ConfigData)20 configData.ConfigKey = conkey21 configData.ConfigValue = confval.(string)22 ctx, cancel := context.WithCancel(context.Background())23 configData.ConfigCancel = cancel24 configMgr[conkey] = configData25 // 添加协程启动逻辑,将协程的ctx保存在map中,这样住协程可以根据热更新26 // 启动和关闭这个协程27 go logtailf.WatchLogFile(conkey, configData.ConfigValue, ctx, keyChan)28 }29}30func main() {31 v := viper.New()32 configPaths, confres := logconfig.ReadConfig(v)33 if configPaths == nil || !confres {34 fmt.Println("read config failed")35 return36 }37 KEYCHANSIZE := 138 keyChan := make(chan string, KEYCHANSIZE)39 configMgr = make(map[string]*logconfig.ConfigData)40 ConstructMgr(configPaths, keyChan)41 ctx, cancel := context.WithCancel(context.Background())42 pathChan := make(chan interface{})43 go logconfig.WatchConfig(ctx, v, pathChan)44 defer func() { //析构函数45 mainOnce.Do(func() {46 if err := recover(); err != nil {47 fmt.Println("main gorroutine panic ", err)48 }49 cancel()50 for _, oldval := range configMgr {51 oldval.ConfigCancel()52 }53 configMgr = nil54 })55 }()56 for {57 select {58 case pathData, ok := <-pathChan:59 if !ok {60 return61 }62 fmt.Println("main goroutine reveive pathData")63 fmt.Println(pathData)64 pathDataNew := pathData.(map[string]interface{}) // golang Type Assertion, 类型断言65 for oldkey, oldval := range configMgr {66 _, ok := pathDataNew[oldkey]67 if ok {68 continue69 }70 oldval.ConfigCancel()71 delete(configMgr, oldkey)72 }73 for conkey, conval := range pathDataNew {74 oldval, ok := configMgr[conkey]75 if !ok {76 configData := new(logconfig.ConfigData)77 configData.ConfigKey = conkey78 configData.ConfigValue = conval.(string)79 ctx, cancel := context.WithCancel(context.Background())80 configData.ConfigCancel = cancel81 configMgr[conkey] = configData82 fmt.Println(conval.(string))83 go logtailf.WatchLogFile(conkey, configData.ConfigValue, ctx, keyChan)84 continue85 }86 if oldval.ConfigValue != conval.(string) {87 oldval.ConfigValue = conval.(string)88 oldval.ConfigCancel()89 ctx, cancel := context.WithCancel(context.Background())90 oldval.ConfigCancel = cancel91 go logtailf.WatchLogFile(conkey, conval.(string), ctx, keyChan)92 continue93 }94 }95 for mgrkey, mgrval := range configMgr {96 fmt.Println(mgrkey)97 fmt.Println(mgrval)98 }99 case keystr := <-keyChan:100 val, ok := configMgr[keystr]101 if !ok {102 print("get keystr filed")103 continue104 }105 fmt.Println("recover goroutine watch ", keystr)106 var ctxcover context.Context107 ctxcover, val.ConfigCancel = context.WithCancel(context.Background())108 go logtailf.WatchLogFile(keystr, val.ConfigValue, ctxcover, keyChan)109 }110 }111}...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful