examples/panic: Make an example to show off aforementioned feature
This commit is contained in:
		
							parent
							
								
									4f4c7a0627
								
							
						
					
					
						commit
						735d314a19
					
				
							
								
								
									
										35
									
								
								examples/panic/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								examples/panic/main.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,35 @@ | |||||||
|  | // Example panic demonstrates how the environment can restart actors if they | ||||||
|  | // fail. | ||||||
|  | package main | ||||||
|  | 
 | ||||||
|  | import "log" | ||||||
|  | import "time" | ||||||
|  | import "errors" | ||||||
|  | import "context" | ||||||
|  | import "math/rand" | ||||||
|  | import "git.tebibyte.media/sashakoshka/camfish" | ||||||
|  | 
 | ||||||
|  | func main() { | ||||||
|  | 	camfish.Run("panic", | ||||||
|  | 		"Example panic demonstrates how the environment can restart " + | ||||||
|  | 		"actors if they fail", | ||||||
|  | 		new(actor)) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // actor is an incorrectly implemented actor that panics and errs randomly. | ||||||
|  | type actor struct { } | ||||||
|  | var _ camfish.Runnable = new(actor) | ||||||
|  | func (this *actor) Type() string { return "panic" } | ||||||
|  | 
 | ||||||
|  | func (this *actor) Run(ctx context.Context) error { | ||||||
|  | 	log.Println("(i) [panic] panicking in 10 seconds") | ||||||
|  | 	select { | ||||||
|  | 	case <- ctx.Done(): return ctx.Err() | ||||||
|  | 	case <- time.After(time.Second * 10): | ||||||
|  | 		if rand.Int() % 2 == 0 { | ||||||
|  | 			panic("this is a panic") | ||||||
|  | 		} else { | ||||||
|  | 			return errors.New("this is an error") | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user