GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — locks/client.go at master · asciimoth/locks Expand file tree/ Copy pathclient.goMore file actions 265 lines (246 loc) · 8.82 KB / Copy pathclient.goFile metadata and controls265 lines (246 loc) · 8.82 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 package locks import ( "context" "fmt" "os" "path/filepath" "runtime" "strings" ) // ClientOptions configures a Client. Its zero value uses the current environment, // blake2 keys, First reads and WriteFirst writes. type ClientOptions struct { // Prepend adds explicit locations before Locations or discovery results. // Relative local paths are resolved when the client opens. Flags are retained. Prepend []Location // Locations replaces discovery when non-nil. An empty slice disables it. // These locations must be absolute, as in LOCK_PATH. Locations []Location // LookupEnv defaults to os.LookupEnv. It must distinguish unset and empty values. LookupEnv func(string) (string, bool) Algorithm string ReadPolicy ReadPolicy WritePolicy WritePolicy // RawKeyBytes limits raw keys before hashing. Zero selects 1 MiB. RawKeyBytes int Local LocalOptions HTTP HTTPOptions // ExistingParents disables creation of missing local parent directories. // By default, writes create parents through checked directory handles. // Reads and read-only positions never create directories. ExistingParents bool } // Client hashes string keys and operates on an ordered chain. Use Open or // OpenWithOptions to construct one. A Client is safe for concurrent use if its // HTTP transport is safe for concurrent use. It needs no Close call. type Client struct { chain Chain locations []Location algorithm string readPolicy ReadPolicy writePolicy WritePolicy rawKeyBytes int } // Open uses LOCK_PATH, including an explicitly empty value, or OS defaults. // Each argument is one local path or HTTP(S) base to prepend, in argument order. // Relative paths are resolved against the current directory. Arguments are not // LOCK_PATH expressions and have no shell or environment expansion. // Open does not access stores or create directories. It snapshots configuration; // later changes to the environment or working directory do not change the client. func Open(prepend ...string) (*Client, error) { options := ClientOptions{} for _, address := range prepend { options.Prepend = append(options.Prepend, Location{Address: address}) } return OpenWithOptions(options) } // OpenWithOptions validates all locations and constructs local and HTTP stores. // It removes only identical locations with identical flags, keeping the first. // Configuration errors fail the whole setup. Backend access errors occur during // operations and follow Chain fallback rules. No project root is inferred. func OpenWithOptions(options ClientOptions) (*Client, error) { if _, err := Hash(nil, options.Algorithm); err != nil { return nil, err } limits, err := (Limits{RawKeyBytes: options.RawKeyBytes}).normalized() if err != nil { return nil, err } if options.ReadPolicy == "" { options.ReadPolicy = First } if options.WritePolicy == "" { options.WritePolicy = WriteFirst } if options.ReadPolicy != First && options.ReadPolicy != Verify || options.WritePolicy != WriteFirst && options.WritePolicy != WriteAll { return nil, fmt.Errorf("%w: client policy", ErrFormat) } locations := options.Locations if locations == nil { getenv := options.LookupEnv if getenv == nil { getenv = os.LookupEnv } locations, err = Discover("", getenv) if err != nil { return nil, err } } all := make([]Location, 0, len(options.Prepend)+len(locations)) for _, loc := range options.Prepend { if loc.Address == "" || strings.ContainsRune(loc.Address, 0) { return nil, fmt.Errorf("%w: invalid location", ErrFormat) } if !httpLocation(loc.Address) { loc.Address, err = filepath.Abs(loc.Address) if err != nil { return nil, err } } all = append(all, loc) } all = append(all, locations...) client := &Client{algorithm: options.Algorithm, readPolicy: options.ReadPolicy, writePolicy: options.WritePolicy, rawKeyBytes: limits.RawKeyBytes} seen := map[Location]bool{} for _, loc := range all { if !validLocation(loc.Address, runtime.GOOS) { return nil, fmt.Errorf("%w: location %q", ErrFormat, loc.Address) } if seen[loc] { continue } seen[loc] = true var store Store if httpLocation(loc.Address) { store, err = OpenHTTP(loc.Address, options.HTTP) } else { localOptions := options.Local localOptions.ReadOnly = localOptions.ReadOnly || loc.ReadOnly var local *Local local, err = OpenLocal(loc.Address, localOptions) store = local if err == nil && !options.ExistingParents && !localOptions.ReadOnly { store = &parentStore{local} } } if err != nil { return nil, fmt.Errorf("location %q: %w", loc.Address, err) } client.locations = append(client.locations, loc) client.chain = append(client.chain, Position{Store: store, CheckOnly: loc.CheckOnly, ReadOnly: loc.ReadOnly}) } return client, nil } // Locations returns a copy in report-attempt order. func (c *Client) Locations() []Location { if c == nil { return nil } return append([]Location(nil), c.locations...) } // Chain returns a copy of the positions for prehashed IDs, per-call policies and // explicit WriteBack. Changes to the returned slice do not change the client. func (c *Client) Chain() Chain { if c == nil { return nil } return append(Chain(nil), c.chain...) } // Get hashes the exact bytes of key and reads with the configured policy. // Empty keys and arbitrary bytes in strings are valid. Missing and Conflict are // semantic results, not errors. Inspect Attempts for backend failures. func (c *Client) Get(ctx context.Context, key string) Report { if c == nil { return Report{Err: ErrFormat} } return c.GetWithPolicy(ctx, key, c.readPolicy) } // GetWithPolicy reads one key using First or Verify for this call. // Verify visits all positions, including check-only positions, and reports // Conflict if found values differ. It does not change the client's default. func (c *Client) GetWithPolicy(ctx context.Context, key string, policy ReadPolicy) Report { id, err := c.keyID(key) if err != nil { return Report{Err: err} } return c.chain.Get(ctx, id, policy) } // Put hashes key and writes with the configured policy. Create preserves an // existing different value and reports Conflict. Replace permits overwrite. // Success can be partial with WriteAll; inspect Attempts for all outcomes. func (c *Client) Put(ctx context.Context, key, value string, mode Mode) Report { if c == nil { return Report{Err: ErrFormat} } return c.PutWithPolicy(ctx, key, value, mode, c.writePolicy) } // PutWithPolicy writes one key using WriteFirst or WriteAll for this call. // WriteAll visits every writable position unless Create finds a conflict. // Inspect Attempts: an acknowledgement does not mean every store succeeded. // This method does not change the client's default. func (c *Client) PutWithPolicy(ctx context.Context, key, value string, mode Mode, policy WritePolicy) Report { id, err := c.keyID(key) if err != nil { return Report{Err: err} } return c.chain.Put(ctx, id, value, mode, policy) } // Create records a first observation. It is equivalent to Put with Create mode. func (c *Client) Create(ctx context.Context, key, value string) Report { return c.Put(ctx, key, value, Create) } // Replace inserts or overwrites a value. It is equivalent to Put with Replace mode. func (c *Client) Replace(ctx context.Context, key, value string) Report { return c.Put(ctx, key, value, Replace) } func (c *Client) keyID(key string) (ID, error) { if c == nil { return "", ErrFormat } if len(key) > c.rawKeyBytes { return "", ErrLimit } return Hash([]byte(key), c.algorithm) } // parentStore prepares parents only for valid, writable operations. type parentStore struct{ local *Local } func (s *parentStore) Get(ctx context.Context, id ID) (Result, error) { return s.local.Get(ctx, id) } func (s *parentStore) GetBatch(ctx context.Context, ids []ID) ([]Outcome, error) { return s.local.GetBatch(ctx, ids) } func (s *parentStore) Put(ctx context.Context, id ID, value string, mode Mode) (Result, error) { if err := contextError(ctx); err != nil { return Result{}, err } if _, err := validateLocalWrite(Write{id, value, mode}, s.local.limits); err != nil { return Result{}, err } ctx, cancel := context.WithTimeout(ctx, s.local.options.LockTimeout) defer cancel() if err := prepareParent(ctx, s.local.parent); err != nil { return Result{}, err } return s.local.Put(ctx, id, value, mode) } func (s *parentStore) PutBatch(ctx context.Context, items []Write) ([]Outcome, error) { if err := contextError(ctx); err != nil { return nil, err } if s == nil || s.local == nil || s.local.path == "" { return nil, ErrFormat } if s.local.options.ReadOnly { return nil, ErrDenied } if _, err := s.local.validateLocalBatch(items); err != nil { return nil, err } ctx, cancel := context.WithTimeout(ctx, s.local.options.LockTimeout) defer cancel() if err := prepareParent(ctx, s.local.parent); err != nil { return nil, err } return s.local.PutBatch(ctx, items) } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |