github.com/blend/go-sdk@v1.20220411.3/examples/db/prevent-deadlock/config.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package main 9 10 import ( 11 "log" 12 "strings" 13 "time" 14 15 "github.com/blend/go-sdk/env" 16 "github.com/blend/go-sdk/ex" 17 ) 18 19 const ( 20 envVarLockTimeout = "DB_LOCK_TIMEOUT" 21 ) 22 23 type config struct { 24 LockTimeout time.Duration 25 ContextTimeout time.Duration 26 TxSleep time.Duration 27 } 28 29 func (c *config) SetEnvironment() error { 30 existing := env.Env().String(envVarLockTimeout) 31 if existing != "" { 32 err := ex.New( 33 "Lock timeout will be set by the prevent-deadlock script", 34 ex.OptMessagef("Value set from the environment, %s=%q", envVarLockTimeout, existing), 35 ) 36 return err 37 } 38 39 env.Env().Set(envVarLockTimeout, c.LockTimeout.String()) 40 return nil 41 } 42 43 func (c *config) Print() { 44 if c == nil { 45 return 46 } 47 48 log.Printf("Configured lock timeout: %s\n", c.LockTimeout) 49 log.Printf("Configured context timeout: %s\n", c.ContextTimeout) 50 log.Printf("Configured transaction sleep: %s\n", c.TxSleep) 51 } 52 53 func getConfig() *config { 54 forceDeadlock := env.Env().String("FORCE_DEADLOCK") 55 if strings.EqualFold(forceDeadlock, "true") { 56 return &config{ 57 LockTimeout: 10 * time.Second, 58 ContextTimeout: 10 * time.Second, 59 TxSleep: 200 * time.Millisecond, 60 } 61 } 62 63 betweenQueries := env.Env().String("BETWEEN_QUERIES") 64 if strings.EqualFold(betweenQueries, "true") { 65 return &config{ 66 LockTimeout: 10 * time.Second, 67 ContextTimeout: 100 * time.Millisecond, 68 TxSleep: 200 * time.Millisecond, 69 } 70 } 71 72 disable := env.Env().String("DISABLE_LOCK_TIMEOUT") 73 if strings.EqualFold(disable, "true") { 74 return &config{ 75 LockTimeout: 10 * time.Second, 76 ContextTimeout: 600 * time.Millisecond, 77 TxSleep: 200 * time.Millisecond, 78 } 79 } 80 81 return &config{ 82 LockTimeout: 10 * time.Millisecond, 83 ContextTimeout: 600 * time.Millisecond, 84 TxSleep: 200 * time.Millisecond, 85 } 86 }