title: Using application context as queries' parent context
body: <p>I am currently passing application/main context from <code>main.go</code> to <code>repository.go</code> so that I can use it as "parent" context to go with query context. Is this valid/idiomatic usage or not? Also, shall I ditch the idea and just use <code>context.Background()</code> as "parent" context to go with query context instead?</p>
  
  <p><strong>main.go</strong></p>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      "context"
      "internal/user"
  )
  
  func main() {
      ctx, cancel := context.WithCancel(context.Background())
      defer cancel()
  
      repo := user.NewRepository(ctx, db)
  
      // HTTP server is running.
  }
  </code></pre>
  
  <p><strong>internal/user/repository.go</strong></p>
  
  <pre class="lang-golang prettyprint-override"><code>package user
  
  import (
      "context"
      "database/sql"
      "time"
  )
  
  type Repository struct {
      *sql.DB
  }
  
  var appCTX context.Context
  
  func NewRepository(ctx context.Context, db *sql.DB) Repository {
      appCTX = ctx
  
      return Repository{db}
  }
  
  func (r Repository) Insert(args ...interface{}) error {
      ctx, cancel := context.WithTimeout(appCTX, 5 * time.Millisecond)
      defer cancel()
  
      // Run query etc.
      res, err := r.ExecContext(ctx, `INSERT INTO .....`, args...)
  }
  </code></pre>