-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsume_example_test.go
More file actions
59 lines (51 loc) · 1.83 KB
/
Copy pathconsume_example_test.go
File metadata and controls
59 lines (51 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package dgdao_test
import (
"context"
"fmt"
dg "github.com/dgraph-io/dgdao"
)
// Token is keyed by a unique jti predicate. The upsert+unique tags let
// GetOrInsert atomically insert-if-absent on that key.
type Token struct {
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
JTI string `json:"jti,omitempty" dgraph:"index=hash upsert unique"`
}
// ExampleClient_loadOrStore atomically inserts a node if no node with the same
// key exists, or reports that one already did. loaded is false when this call
// created the node and true when an existing node was found; on the loaded=true
// path the passed object is hydrated with the existing record.
//
// This is the building block for "claim a one-time token": the first caller
// stores and proceeds, every later caller sees loaded=true and is rejected.
func ExampleClient_GetOrInsert() {
client, _ := dg.NewClient("dgraph://localhost:9080")
defer client.Close()
ctx := context.Background()
loaded, err := client.GetOrInsert(ctx, &Token{JTI: "abc123"}, "jti")
if err != nil {
panic(err)
}
fmt.Println(loaded) // false the first time, true thereafter
}
// ExampleClient_loadAndDelete atomically reads a node and deletes it, electing a
// single winner under concurrency: exactly one caller gets loaded=true with the
// record hydrated, the rest get loaded=false. Use it to consume a one-shot
// value — a nonce, a pending job, a single-use code.
func ExampleClient_GetAndDelete() {
client, _ := dg.NewClient("dgraph://localhost:9080")
defer client.Close()
ctx := context.Background()
var got Token
loaded, err := client.GetAndDelete(ctx, &got, "abc123", "jti")
if err != nil {
panic(err)
}
if loaded {
fmt.Println("consumed", got.JTI)
}
}