github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/context.go (about) 1 package common 2 3 import ( 4 "context" 5 "reflect" 6 ) 7 8 func SelectContext(contextList []context.Context) (int, error) { 9 if len(contextList) == 1 { 10 <-contextList[0].Done() 11 return 0, contextList[0].Err() 12 } 13 chosen, _, _ := reflect.Select(Map(Filter(contextList, func(it context.Context) bool { 14 return it.Done() != nil 15 }), func(it context.Context) reflect.SelectCase { 16 return reflect.SelectCase{ 17 Dir: reflect.SelectRecv, 18 Chan: reflect.ValueOf(it.Done()), 19 } 20 })) 21 return chosen, contextList[chosen].Err() 22 }